forked from eclipse/paho.golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
151 lines (128 loc) · 4.57 KB
/
config.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
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
package main
import (
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"
)
// Retrieve config from environmental variables
// Configuration will be pulled from the environment using the following keys
const (
envServerURL = "subdemo_serverURL" // server URL
envClientID = "subdemo_clientID" // client id to connect with
envTopic = "subdemo_topic" // topic to publish on
envQos = "subdemo_qos" // qos to utilise when publishing
envKeepAlive = "subdemo_keepAlive" // seconds between keepalive packets
envConnectRetryDelay = "subdemo_connectRetryDelay" // milliseconds to delay between connection attempts
envWriteToStdOut = "subdemo_writeToStdout" // if "true" then received packets will be written stdout
envWriteToDisk = "subdemo_writeToDisk" // if "true" then received packets will be written to file
envOutputFile = "subdemo_OutputFile" // name of file to use if above is true
envDebug = "subdemo_debug" // if "true" then the libraries will be instructed to print debug info
)
// config holds the configuration
type config struct {
serverURL *url.URL // MQTT server URL
clientID string // Client ID to use when connecting to server
topic string // Topic on which to publish messaged
qos byte // QOS to use when publishing
keepAlive uint16 // seconds between keepalive packets
connectRetryDelay time.Duration // Period between connection attempts
writeToStdOut bool // If true received messages will be written to stdout
writeToDisk bool // if true received messages will be written to below file
outputFileName string // filename to save messages to
debug bool // autopaho and paho debug output requested
}
// getConfig - Retrieves the configuration from the environment
func getConfig() (config, error) {
var cfg config
var err error
srvURL, err := stringFromEnv(envServerURL)
if err != nil {
return config{}, err
}
cfg.serverURL, err = url.Parse(srvURL)
if err != nil {
return config{}, fmt.Errorf("environmental variable %s must be a valid URL (%w)", envServerURL, err)
}
if cfg.clientID, err = stringFromEnv(envClientID); err != nil {
return config{}, err
}
if cfg.topic, err = stringFromEnv(envTopic); err != nil {
return config{}, err
}
iQos, err := intFromEnv(envQos)
if err != nil {
return config{}, err
}
cfg.qos = byte(iQos)
iKa, err := intFromEnv(envKeepAlive)
if err != nil {
return config{}, err
}
cfg.keepAlive = uint16(iKa)
if cfg.connectRetryDelay, err = milliSecondsFromEnv(envConnectRetryDelay); err != nil {
return config{}, err
}
if cfg.writeToStdOut, err = booleanFromEnv(envWriteToStdOut); err != nil {
return config{}, err
}
if cfg.writeToDisk, err = booleanFromEnv(envWriteToDisk); err != nil {
return config{}, err
}
if cfg.outputFileName, err = stringFromEnv(envOutputFile); cfg.writeToDisk && err != nil {
return config{}, err
}
if cfg.debug, err = booleanFromEnv(envDebug); err != nil {
return config{}, err
}
return cfg, nil
}
// stringFromEnv - Retrieves a string from the environment and ensures it is not blank (ort non-existent)
func stringFromEnv(key string) (string, error) {
s := os.Getenv(key)
if len(s) == 0 {
return "", fmt.Errorf("environmental variable %s must not be blank", key)
}
return s, nil
}
// intFromEnv - Retrieves an integer from the environment (must be present and valid)
func intFromEnv(key string) (int, error) {
s := os.Getenv(key)
if len(s) == 0 {
return 0, fmt.Errorf("environmental variable %s must not be blank", key)
}
i, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("environmental variable %s must be an integer", key)
}
return i, nil
}
// milliSecondsFromEnv - Retrieves milliseconds (as time.Duration) from the environment (must be present and valid)
func milliSecondsFromEnv(key string) (time.Duration, error) {
s := os.Getenv(key)
if len(s) == 0 {
return 0, fmt.Errorf("environmental variable %s must not be blank", key)
}
i, err := strconv.Atoi(s)
if err != nil {
return 0, fmt.Errorf("environmental variable %s must be an integer", key)
}
return time.Duration(i) * time.Millisecond, nil
}
// booleanFromEnv - Retrieves boolean from the environment (must be present and valid)
func booleanFromEnv(key string) (bool, error) {
s := os.Getenv(key)
if len(s) == 0 {
return false, fmt.Errorf("environmental variable %s must not be blank", key)
}
switch strings.ToUpper(s) {
case "TRUE", "T", "1":
return true, nil
case "FALSE", "F", "0":
return false, nil
default:
return false, fmt.Errorf("environmental variable %s be a valid boolean option (is %s)", key, s)
}
}