Skip to content
This repository was archived by the owner on Nov 24, 2019. It is now read-only.

Commit 50b13a4

Browse files
committed
*: environment variables
Signed-off-by: Máximo Cuadros <mcuadros@gmail.com>
1 parent d55f169 commit 50b13a4

File tree

3 files changed

+85
-9
lines changed

3 files changed

+85
-9
lines changed
Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,15 @@
1-
OCTOPRINT_TFT_STYLE_PATH=/opt/octoprint-tft/styles/default/
1+
# OctoPrint HTTP address, default http://localhost
2+
OCTOPRINT_HOST=
3+
4+
# OctoPrint-TFT expects an API key to be supplied. This API key can be either
5+
# the globally configured one or a user specific one if “Access Control”.
6+
# http://docs.octoprint.org/en/master/api/general.html
27
OCTOPRINT_APIKEY=
8+
9+
# Location of the OctoPrint's config.yaml file, if OCTOPRINT_APIKEY is empty
10+
# a the gobal API will be read from the config file. If empty the file will
11+
# be search at the `pi` home folder or the current user.
12+
OCTOPRINT_CONFIG_FILE=
13+
14+
# Location of the application theme.
15+
OCTOPRINT_TFT_STYLE_PATH=/opt/octoprint-tft/styles/default/

main.go

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,50 @@
11
package main
22

33
import (
4+
"io/ioutil"
45
"os"
6+
"os/user"
7+
"path/filepath"
8+
9+
yaml "gopkg.in/yaml.v1"
510

611
"github.com/gotk3/gotk3/gtk"
712
"github.com/mcuadros/OctoPrint-TFT/ui"
813
)
914

1015
const (
11-
EnvStylePath = "OCTOPRINT_TFT_STYLE_PATH"
12-
EnvBaseURL = "OCTOPRINT_HOST"
13-
EnvAPIKey = "OCTOPRINT_APIKEY"
16+
EnvStylePath = "OCTOPRINT_TFT_STYLE_PATH"
17+
EnvBaseURL = "OCTOPRINT_HOST"
18+
EnvAPIKey = "OCTOPRINT_APIKEY"
19+
EnvConfigFile = "OCTOPRINT_CONFIG_FILE"
1420

15-
DefaultBaseURL = "http://127.0.0.1"
21+
DefaultBaseURL = "http://localhost"
1622
)
1723

1824
var (
19-
BaseURL string
20-
APIKey string
25+
BaseURL string
26+
APIKey string
27+
ConfigFile string
2128
)
2229

2330
func init() {
2431
ui.StylePath = os.Getenv(EnvStylePath)
25-
BaseURL = os.Getenv(EnvBaseURL)
2632
APIKey = os.Getenv(EnvAPIKey)
33+
34+
BaseURL = os.Getenv(EnvBaseURL)
2735
if BaseURL == "" {
2836
BaseURL = DefaultBaseURL
2937
}
38+
39+
ConfigFile = os.Getenv(EnvConfigFile)
40+
if ConfigFile == "" {
41+
ConfigFile = findConfigFile()
42+
}
43+
44+
if APIKey != "" && ConfigFile != "" {
45+
APIKey = readAPIKey(ConfigFile)
46+
ui.Logger.Infof("Found API key at %q file", ConfigFile)
47+
}
3048
}
3149

3250
func main() {
@@ -38,3 +56,48 @@ func main() {
3856
ui.New(BaseURL, APIKey)
3957
gtk.Main()
4058
}
59+
60+
var (
61+
configLocation = ".octoprint/config.yaml"
62+
homeOctoPi = "/home/pi/"
63+
)
64+
65+
func readAPIKey(config string) string {
66+
var cfg struct{ API struct{ Key string } }
67+
68+
data, err := ioutil.ReadFile(config)
69+
if err != nil {
70+
ui.Logger.Fatal(err)
71+
return ""
72+
}
73+
74+
if err := yaml.Unmarshal([]byte(data), &cfg); err != nil {
75+
ui.Logger.Fatalf("Error decoding YAML config file %q: %s", config, err)
76+
return ""
77+
}
78+
79+
return cfg.API.Key
80+
}
81+
82+
func findConfigFile() string {
83+
if file := doFindConfigFile(homeOctoPi); file != "" {
84+
return file
85+
}
86+
87+
usr, err := user.Current()
88+
if err != nil {
89+
return ""
90+
}
91+
92+
return doFindConfigFile(usr.HomeDir)
93+
}
94+
95+
func doFindConfigFile(home string) string {
96+
path := filepath.Join(home, configLocation)
97+
98+
if _, err := os.Stat(path); err == nil {
99+
return path
100+
}
101+
102+
return ""
103+
}

ui/system.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (m *systemPanel) createRestartButton() gtk.IWidget {
6363

6464
var cmd *octoprint.CommandDefinition
6565
for _, c := range r.Core {
66-
if c.Action == "restart" {
66+
if c.Action == "reboot" {
6767
cmd = c
6868
}
6969
}

0 commit comments

Comments
 (0)