Skip to content

Commit

Permalink
*: environment variables
Browse files Browse the repository at this point in the history
Signed-off-by: Máximo Cuadros <[email protected]>
  • Loading branch information
mcuadros committed Jan 2, 2018
1 parent d55f169 commit 50b13a4
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 9 deletions.
15 changes: 14 additions & 1 deletion debian/local/octoprint-tft-environment
Original file line number Diff line number Diff line change
@@ -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/
77 changes: 70 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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 ""
}
2 changes: 1 addition & 1 deletion ui/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down

0 comments on commit 50b13a4

Please sign in to comment.