Debian Codegraph @

autopaho/examples/docker/publisher/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 = "pubdemo_serverURL" // server URL
32 envClientID = "pubdemo_clientID" // client id to connect with
33 envTopic = "pubdemo_topic" // topic to publish on
34 envQos = "pubdemo_qos" // qos to utilise when publishing
35
36 envKeepAlive = "pubdemo_keepAlive" // seconds between keep alive packets
37 envConnectRetryDelay = "pubdemo_connectRetryDelay" // milliseconds to delay between connection attempts
38 envDelayBetweenMessages = "pubdemo_delayBetweenMessages" // milliseconds delay between published messages
39
40 envSessionFolder = "pubdemo_sessionfolder" // folder used to persist the session state (if empty state will be held in RAM)
41
42 envPrintMessages = "pubdemo_printMessages" // If "true" then published messages will be written to the console
43 envDebug = "pubdemo_debug" // if "true" then the libraries will be instructed to print debug info
44 )
45
46 // config holds the configuration
47 type config struct {
48 serverURL *url.URL // MQTT server URL
49 clientID string // Client ID to use when connecting to server
50 topic string // Topic to subscribe to
51 qos byte // QOS to use when subscribing
52
53 keepAlive uint16 // seconds between keepalive packets
54 connectRetryDelay time.Duration // Period between connection attempts
55 delayBetweenMessages time.Duration // Period between publishing messages
56
57 sessionFolder string // path where session state should be stored (if blank this will be held in RAM)
58
59 printMessages bool // If true then published messages will be written to the console
60 debug bool // autopaho and paho debug output requested
61 }
62
63 // getConfig - Retrieves the configuration from the environment
64 func getConfig() (config, error) {
65 var cfg config
66 var err error
67
68 srvURL, err := stringFromEnv(envServerURL)
69 if err != nil {
70 return config{}, err
71 }
72 cfg.serverURL, err = url.Parse(srvURL)
73 if err != nil {
74 return config{}, fmt.Errorf("environmental variable %s must be a valid URL (%w)", envServerURL, err)
75 }
76
77 if cfg.clientID, err = stringFromEnv(envClientID); err != nil {
78 return config{}, err
79 }
80 if cfg.topic, err = stringFromEnv(envTopic); err != nil {
81 return config{}, err
82 }
83
84 iQos, err := intFromEnv(envQos)
85 if err != nil {
86 return config{}, err
87 }
88 cfg.qos = byte(iQos)
89
90 iKa, err := intFromEnv(envKeepAlive)
91 if err != nil {
92 return config{}, err
93 }
94 cfg.keepAlive = uint16(iKa)
95
96 cfg.sessionFolder = os.Getenv(envSessionFolder)
97
98 if cfg.connectRetryDelay, err = milliSecondsFromEnv(envConnectRetryDelay); err != nil {
99 return config{}, err
100 }
101 if cfg.delayBetweenMessages, err = milliSecondsFromEnv(envDelayBetweenMessages); err != nil {
102 return config{}, err
103 }
104
105 if cfg.printMessages, err = booleanFromEnv(envPrintMessages); err != nil {
106 return config{}, err
107 }
108 if cfg.debug, err = booleanFromEnv(envDebug); err != nil {
109 return config{}, err
110 }
111
112 return cfg, nil
113 }
114
115 // stringFromEnv - Retrieves a string from the environment and ensures it is not blank (ort non-existent)
116 func stringFromEnv(key string) (string, error) {
117 s := os.Getenv(key)
118 if len(s) == 0 {
119 return "", fmt.Errorf("environmental variable %s must not be blank", key)
120 }
121 return s, nil
122 }
123
124 // intFromEnv - Retrieves an integer from the environment (must be present and valid)
125 func intFromEnv(key string) (int, error) {
126 s := os.Getenv(key)
127 if len(s) == 0 {
128 return 0, fmt.Errorf("environmental variable %s must not be blank", key)
129 }
130 i, err := strconv.Atoi(s)
131 if err != nil {
132 return 0, fmt.Errorf("environmental variable %s must be an integer", key)
133 }
134 return i, nil
135 }
136
137 // milliSecondsFromEnv - Retrieves milliseconds (as time.Duration) from the environment (must be present and valid)
138 func milliSecondsFromEnv(key string) (time.Duration, error) {
139 s := os.Getenv(key)
140 if len(s) == 0 {
141 return 0, fmt.Errorf("environmental variable %s must not be blank", key)
142 }
143 i, err := strconv.Atoi(s)
144 if err != nil {
145 return 0, fmt.Errorf("environmental variable %s must be an integer", key)
146 }
147 return time.Duration(i) * time.Millisecond, nil
148 }
149
150 // booleanFromEnv - Retrieves boolean from the environment (must be present and valid)
151 func booleanFromEnv(key string) (bool, error) {
152 s := os.Getenv(key)
153 if len(s) == 0 {
154 return false, fmt.Errorf("environmental variable %s must not be blank", key)
155 }
156 switch strings.ToUpper(s) {
157 case "TRUE", "T", "1":
158 return true, nil
159 case "FALSE", "F", "0":
160 return false, nil
161 default:
162 return false, fmt.Errorf("environmental variable %s be a valid boolean option (is %s)", key, s)
163 }
164 }