Welcome to mirror list, hosted at ThFree Co, Russian Federation.

environment.go « utils - github.com/Z-Bolt/OctoScreen.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b35d9a929bac8c700fc818c352989f7598cd32af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package utils

import (
	"fmt"
	"os"
	//"strings"


	"github.com/Z-Bolt/OctoScreen/logger"
)


// OctoScreenVersion is set during compilation.
var OctoScreenVersion = "2.7.4"

const MISSING_ENV_TOKEN = ">>MISSING<<"
const INVALID_ENV_TOKEN = "!!!INVALID!!!"

// Required environment variables
const (
	EnvStylePath   = "OCTOSCREEN_STYLE_PATH"
	EnvBaseURL     = "OCTOPRINT_HOST"
	EnvAPIKey      = "OCTOPRINT_APIKEY"
)

// Optional (but good to have) environment variables
const (
	EnvLogLevel       = "OCTOSCREEN_LOG_LEVEL"
	EnvLogFilePath    = "OCTOSCREEN_LOG_FILE_PATH"
	EnvResolution     = "OCTOSCREEN_RESOLUTION"
	EnvConfigFile     = "OCTOPRINT_CONFIG_FILE"
	EnvDisplayCursor  = "DISPLAY_CURSOR"
)

func RequiredEnvironmentVariablesAreSet(apiKey string) bool {
	if( !environmentVariableIsSet(EnvStylePath) ) {
		return false
	}

	if( !environmentVariableIsSet(EnvBaseURL) ) {
		return false
	}

	// APIKey/OCTOPRINT_APIKEY can be set in either OctoScreen's config file,
	// or in OctoPrint's config file.  In main.init(), APIKey is initialized to whatever
	// it can find first.
	//
	// APIKey is global to the "main" namespace, but the "utils" namespace is a child,
	// and due to GoLang's rules, /main/utils doesn't have access to globals in /main,
	// so APIKey has to be passed into RequiredEnvironmentVariablesAreSet().
	//
	// if( !environmentVariableIsSet(EnvAPIKey) ) {
	// 	return false
	// }
	if apiKey == "" {
		return false
	}

	return true
}

func environmentVariableIsSet(environmentVariable string) bool {
	return os.Getenv(environmentVariable) != ""
}

func NameOfMissingRequiredEnvironmentVariable(apiKey string) string {
	if( !environmentVariableIsSet(EnvStylePath) ) {
		return EnvStylePath
	}

	if( !environmentVariableIsSet(EnvBaseURL) ) {
		return EnvBaseURL
	}

	// Similar comment as to the one that's in RequiredEnvironmentVariablesAreSet()...
	// Since the runtime value of APIKey is set in main.init(), and can be set by either
	// being defined in OctoScreen's config file or in OctoPrint's config file,
	// the value needs to be passed into NameOfMissingRequiredEnvironmentVariable().
	// if( !environmentVariableIsSet(EnvAPIKey) ) {
	// 	return EnvAPIKey
	// }
	if apiKey == "" {
		return EnvAPIKey
	}

	return "UNKNOWN"
}

func DumpSystemInformation() {
	logger.Info("System Information...")
	logger.Infof("OctoScreen version: %q", OctoScreenVersion)
	// More system stats to come...
	logger.Info("")
}

func DumpEnvironmentVariables() {
	logger.Info("Environment variables...")

	// Required environment variables
	logger.Info("Required environment variables:")
	dumpEnvironmentVariable(EnvBaseURL)

	// TODO: revisit this!
	// 1. remove OCTOPRINT_APIKEY from option settings
	// 2. make the octoprint config path required
	// 3. update code... use only one path to get the api key octoprint)
	// 4. update code... make octoprint config path required
	// 5. update code... read api key from octoprint config
	// 6. dump api key (obfuscated though)
	// 7. update docs
	// 8. make sure what's dumped to the log is correct, for both when present and when missing.
	dumpObfuscatedEnvironmentVariable(EnvAPIKey)

	dumpEnvironmentVariable(EnvStylePath)
	logger.Info("")

	// Optional environment variables
	logger.Info("Optional environment variables:")
	dumpEnvironmentVariable(EnvConfigFile)
	dumpEnvironmentVariable(EnvLogFilePath)
	dumpEnvironmentVariable(EnvLogLevel)

	dumpEnvironmentVariable(EnvResolution)
	// EnvResolution is optional.  If not set, the window size will
	// default to the values defined in globalVars.go.

	dumpEnvironmentVariable(EnvDisplayCursor)
	logger.Info("")
}

func dumpEnvironmentVariable(key string) {
	value := os.Getenv(key)
	if value == "" {
		value = MISSING_ENV_TOKEN
	}

	logger.Infof("key: %q, value: %q", key, value)
}

func dumpObfuscatedEnvironmentVariable(key string) {
	value := os.Getenv(key)
	if value == "" {
		value = MISSING_ENV_TOKEN
	} else {
		value = GetObfuscatedValue(value)
	}

	logger.Infof("key: %q, value: %q", key, value)
}

func GetObfuscatedValue(value string) string {
	length := len(value)

	obfuscatedValue := ""
	if length < 6 {
		obfuscatedValue = INVALID_ENV_TOKEN
	} else {
		if value == MISSING_ENV_TOKEN {
			return value
		} else {
			obfuscatedValue = fmt.Sprintf("%c%c%c---%c%c%c",
				value[0],
				value[1],
				value[2],
				value[length - 3],
				value[length - 2],
				value[length - 1],
			)
		}
	}

	return obfuscatedValue
}