-
Notifications
You must be signed in to change notification settings - Fork 2
/
device.go
82 lines (67 loc) · 1.5 KB
/
device.go
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
package main
import (
"net"
"os"
"strings"
)
// var device model.Device = model.Device{}
// ServiceHost can only be set using an environment variable
var ServiceHost = false
var cfg struct {
sourceAddr *net.TCPAddr
init bool
loaded bool
path *string
quiet bool
debug bool
Server string
DeviceID string
ApiKey string
UpdateKeys bool
}
func loadConfig() error {
if cfg.loaded {
return nil
}
if !cfg.init {
cfg.init = true
_, ServiceHost = os.LookupEnv("NETTICA_SERVICE_HOST")
// configure defaults
cfg.debug = false
cfg.quiet = false
cfg.UpdateKeys = true
// load defaults from environment
cfg.Server = os.Getenv("NETTICA_SERVER")
cfg.DeviceID = os.Getenv("NETTICA_DEVICE_ID")
cfg.ApiKey = os.Getenv("NETTICA_API_KEY")
value, qpresent := os.LookupEnv("NETTICA_QUIET")
if qpresent {
if value == "" || strings.ToLower(value) == "true" || value == "1" {
cfg.quiet = true
} else {
cfg.quiet = false
}
}
value, dpresent := os.LookupEnv("NETTICA_DEBUG")
if dpresent {
if value == "" || strings.ToLower(value) == "true" || value == "1" {
cfg.debug = true
} else {
cfg.debug = false
}
}
if strings.ToLower(os.Getenv("NETTICA_UPDATE_KEYS")) == "false" {
cfg.UpdateKeys = false
}
if cfg.Server == "" {
cfg.Server = "https://my.nettica.com"
}
var err error
cfg.sourceAddr, err = net.ResolveTCPAddr("tcp", "0.0.0.0:0")
if err != nil {
return err
}
cfg.loaded = true
}
return nil
}