Debian Codegraph @

autopaho/examples/docker/subscriber/config.go

raw symbols SCIP
1 /*
2 * Copyright (c) 2024 Contributors to the Eclipse Foundation
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v2.0
6 * and Eclipse Distribution License v1.0 which accompany this distribution.
7 *
8 * The Eclipse Public License is available at
9 * https://www.eclipse.org/legal/epl-2.0/
10 * and the Eclipse Distribution License is available at
11 * http://www.eclipse.org/org/documents/edl-v10.php.
12 *
13 * SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
14 */
15
16 package main
17
18 import (
19 "fmt"
20 "net/url"
21 "os"
22 "strconv"
23 "strings"
24 "time"
25 )
26
27 // Retrieve config from environmental variables
28
29 // Configuration will be pulled from the environment using the following keys
30 const (
31 envServerURL = "subdemo_serverURL" // server URL
32 envClientID = "subdemo_clientID" // client id to connect with
33 envTopic = "subdemo_topic" // topic to publish on
34 envQos = "subdemo_qos" // qos to utilise when publishing
35
36 envKeepAlive = "subdemo_keepAlive" // seconds between keepalive packets
37 envConnectRetryDelay = "subdemo_connectRetryDelay" // milliseconds to delay between connection attempts
38
39 envSessionFolder = "subdemo_sessionfolder" // folder used to persist the session state (if empty state will be held in RAM)
40
41 envWriteToStdOut = "subdemo_writeToStdout" // if "true" then received packets will be written stdout
42 envWriteToDisk = "subdemo_writeToDisk" // if "true" then received packets will be written to file
43 envOutputFile = "subdemo_OutputFile" // name of file to use if above is true
44
45 envDebug = "subdemo_debug" // if "true" then the libraries will be instructed to print debug info
46 )
47
48 // config holds the configuration
49 type config struct {
50 serverURL *url.URL // MQTT server URL
51 clientID string // Client ID to use when connecting to server
52 topic string // Topic on which to publish messaged
53 qos byte // QOS to use when publishing
54
55 keepAlive uint16 // seconds between keepalive packets
56 connectRetryDelay time.Duration // Period between connection attempts
57
58 sessionFolder string // path where session state should be stored (if blank this will be held in RAM)
59
60 writeToStdOut bool // If true received messages will be written to stdout
61 writeToDisk bool // if true received messages will be written to below file
62 outputFileName string // filename to save messages to
63
64 debug bool // autopaho and paho debug output requested
65 }
66
67 // getConfig - Retrieves the configuration from the environment
68 func getConfig() (config, error) {
69 var cfg config
70 var err error
71
72 srvURL, err := stringFromEnv(envServerURL)
73 if err != nil {
74 return config{}, err
75 }
76 cfg.serverURL, err = url.Parse(srvURL)
77 if err != nil {
78 return config{}, fmt.Errorf("environmental variable %s must be a valid URL (%w)", envServerURL, err)
79 }
80
81 if cfg.clientID, err = stringFromEnv(envClientID); err != nil {
82 return config{}, err
83 }
84 if cfg.topic, err = stringFromEnv(envTopic); err != nil {
85 return config{}, err
86 }
87
88 iQos, err := intFromEnv(envQos)
89 if err != nil {
90 return config{}, err
91 }
92 cfg.qos = byte(iQos)
93
94 iKa, err := intFromEnv(envKeepAlive)
95 if err != nil {
96 return config{}, err
97 }
98 cfg.keepAlive = uint16(iKa)
99
100 if cfg.connectRetryDelay, err = milliSecondsFromEnv(envConnectRetryDelay); err != nil {
101 return config{}, err
102 }
103
104 cfg.sessionFolder = os.Getenv(envSessionFolder)
105
106 if cfg.writeToStdOut, err = booleanFromEnv(envWriteToStdOut); err != nil {
107 return config{}, err
108 }
109 if cfg.writeToDisk, err = booleanFromEnv(envWriteToDisk); err != nil {
110 return config{}, err
111 }
112 if cfg.outputFileName, err = stringFromEnv(envOutputFile); cfg.writeToDisk && err != nil {
113 return config{}, err
114 }
115
116 if cfg.debug, err = booleanFromEnv(envDebug); err != nil {
117 return config{}, err
118 }
119
120 return cfg, nil
121 }
122
123 // stringFromEnv - Retrieves a string from the environment and ensures it is not blank (or non-existent)
124 func stringFromEnv(key string) (string, error) {
125 s := os.Getenv(key)
126 if len(s) == 0 {
127 return "", fmt.Errorf("environmental variable %s must not be blank", key)
128 }
129 return s, nil
130 }
131
132 // intFromEnv - Retrieves an integer from the environment (must be present and valid)
133 func intFromEnv(key string) (int, error) {
134 s := os.Getenv(key)
135 if len(s) == 0 {
136 return 0, fmt.Errorf("environmental variable %s must not be blank", key)
137 }
138 i, err := strconv.Atoi(s)
139 if err != nil {
140 return 0, fmt.Errorf("environmental variable %s must be an integer", key)
141 }
142 return i, nil
143 }
144
145 // milliSecondsFromEnv - Retrieves milliseconds (as time.Duration) from the environment (must be present and valid)
146 func milliSecondsFromEnv(key string) (time.Duration, error) {
147 s := os.Getenv(key)
148 if len(s) == 0 {
149 return 0, fmt.Errorf("environmental variable %s must not be blank", key)
150 }
151 i, err := strconv.Atoi(s)
152 if err != nil {
153 return 0, fmt.Errorf("environmental variable %s must be an integer", key)
154 }
155 return time.Duration(i) * time.Millisecond, nil
156 }
157
158 // booleanFromEnv - Retrieves boolean from the environment (must be present and valid)
159 func booleanFromEnv(key string) (bool, error) {
160 s := os.Getenv(key)
161 if len(s) == 0 {
162 return false, fmt.Errorf("environmental variable %s must not be blank", key)
163 }
164 switch strings.ToUpper(s) {
165 case "TRUE", "T", "1":
166 return true, nil
167 case "FALSE", "F", "0":
168 return false, nil
169 default:
170 return false, fmt.Errorf("environmental variable %s be a valid boolean option (is %s)", key, s)
171 }
172 }