diff options
author | Máximo Cuadros <mcuadros@gmail.com> | 2018-01-03 02:43:35 +0300 |
---|---|---|
committer | Máximo Cuadros <mcuadros@gmail.com> | 2018-01-03 02:43:35 +0300 |
commit | 50b13a42fe3bf56517bc8dbab35075415939849e (patch) | |
tree | e36f82f2eb4808fb801001298e7e7b02ba7a777b | |
parent | d55f16957623efe44328357686181ed844505010 (diff) |
*: environment variablesv0.1.0
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
-rw-r--r-- | debian/local/octoprint-tft-environment | 15 | ||||
-rw-r--r-- | main.go | 77 | ||||
-rw-r--r-- | ui/system.go | 2 |
3 files changed, 85 insertions, 9 deletions
diff --git a/debian/local/octoprint-tft-environment b/debian/local/octoprint-tft-environment index ce73645..31851a4 100644 --- a/debian/local/octoprint-tft-environment +++ b/debian/local/octoprint-tft-environment @@ -1,2 +1,15 @@ -OCTOPRINT_TFT_STYLE_PATH=/opt/octoprint-tft/styles/default/ +# OctoPrint HTTP address, default http://localhost +OCTOPRINT_HOST= + +# OctoPrint-TFT expects an API key to be supplied. This API key can be either +# the globally configured one or a user specific one if “Access Control”. +# http://docs.octoprint.org/en/master/api/general.html OCTOPRINT_APIKEY= + +# Location of the OctoPrint's config.yaml file, if OCTOPRINT_APIKEY is empty +# a the gobal API will be read from the config file. If empty the file will +# be search at the `pi` home folder or the current user. +OCTOPRINT_CONFIG_FILE= + +# Location of the application theme. +OCTOPRINT_TFT_STYLE_PATH=/opt/octoprint-tft/styles/default/ @@ -1,32 +1,50 @@ package main import ( + "io/ioutil" "os" + "os/user" + "path/filepath" + + yaml "gopkg.in/yaml.v1" "github.com/gotk3/gotk3/gtk" "github.com/mcuadros/OctoPrint-TFT/ui" ) const ( - EnvStylePath = "OCTOPRINT_TFT_STYLE_PATH" - EnvBaseURL = "OCTOPRINT_HOST" - EnvAPIKey = "OCTOPRINT_APIKEY" + EnvStylePath = "OCTOPRINT_TFT_STYLE_PATH" + EnvBaseURL = "OCTOPRINT_HOST" + EnvAPIKey = "OCTOPRINT_APIKEY" + EnvConfigFile = "OCTOPRINT_CONFIG_FILE" - DefaultBaseURL = "http://127.0.0.1" + DefaultBaseURL = "http://localhost" ) var ( - BaseURL string - APIKey string + BaseURL string + APIKey string + ConfigFile string ) func init() { ui.StylePath = os.Getenv(EnvStylePath) - BaseURL = os.Getenv(EnvBaseURL) APIKey = os.Getenv(EnvAPIKey) + + BaseURL = os.Getenv(EnvBaseURL) if BaseURL == "" { BaseURL = DefaultBaseURL } + + ConfigFile = os.Getenv(EnvConfigFile) + if ConfigFile == "" { + ConfigFile = findConfigFile() + } + + if APIKey != "" && ConfigFile != "" { + APIKey = readAPIKey(ConfigFile) + ui.Logger.Infof("Found API key at %q file", ConfigFile) + } } func main() { @@ -38,3 +56,48 @@ func main() { ui.New(BaseURL, APIKey) gtk.Main() } + +var ( + configLocation = ".octoprint/config.yaml" + homeOctoPi = "/home/pi/" +) + +func readAPIKey(config string) string { + var cfg struct{ API struct{ Key string } } + + data, err := ioutil.ReadFile(config) + if err != nil { + ui.Logger.Fatal(err) + return "" + } + + if err := yaml.Unmarshal([]byte(data), &cfg); err != nil { + ui.Logger.Fatalf("Error decoding YAML config file %q: %s", config, err) + return "" + } + + return cfg.API.Key +} + +func findConfigFile() string { + if file := doFindConfigFile(homeOctoPi); file != "" { + return file + } + + usr, err := user.Current() + if err != nil { + return "" + } + + return doFindConfigFile(usr.HomeDir) +} + +func doFindConfigFile(home string) string { + path := filepath.Join(home, configLocation) + + if _, err := os.Stat(path); err == nil { + return path + } + + return "" +} diff --git a/ui/system.go b/ui/system.go index defa605..6f51367 100644 --- a/ui/system.go +++ b/ui/system.go @@ -63,7 +63,7 @@ func (m *systemPanel) createRestartButton() gtk.IWidget { var cmd *octoprint.CommandDefinition for _, c := range r.Core { - if c.Action == "restart" { + if c.Action == "reboot" { cmd = c } } |