Adding common flags

drew/add-droneio
Drew Bednar 4 months ago
parent 245f99d333
commit 44810ce030

@ -1,7 +1,13 @@
package main
import "fmt"
import (
"flag"
"fmt"
"git.runcible.io/learning/learn_mqtt_go/common"
)
func main() {
fmt.Println("MQTT Publisher")
flag.Parse()
fmt.Printf("MQTT Host: %s\nMQTT Port: %s\n", *common.BrokerHost, *common.BrokerPort)
}

@ -1,7 +1,13 @@
package main
import "fmt"
import (
"flag"
"fmt"
"git.runcible.io/learning/learn_mqtt_go/common"
)
func main() {
fmt.Println("MQTT Subscriber")
flag.Parse()
fmt.Printf("MQTT Host: %s\nMQTT Port: %s\n", *common.BrokerHost, *common.BrokerPort)
}

@ -1,6 +1,9 @@
package common
import "os"
import (
"flag"
"os"
)
// GetenvDefault retrieves an environment variable if set, or a provided default
func GetenvDefault(key string, defaultValue string) string {
@ -11,3 +14,9 @@ func GetenvDefault(key string, defaultValue string) string {
return defaultValue
}
}
// For now just assume all values source from flags are strings
var (
BrokerHost = flag.String("host", GetenvDefault("RUN_MQTT_HOST", "localhost"), "Hostname or IP address to an MQTT broker")
BrokerPort = flag.String("port", GetenvDefault("RUN_MQTT_PORT", "1883"), "Port to an MQTT broker")
)

Loading…
Cancel
Save