From 44810ce030ee65ac5d7793a851f9f45a25062952 Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Wed, 18 Sep 2024 18:29:54 -0400 Subject: [PATCH] Adding common flags --- cmd/publisher/main.go | 10 ++++++++-- cmd/subscriber/main.go | 10 ++++++++-- common/common.go | 11 ++++++++++- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/cmd/publisher/main.go b/cmd/publisher/main.go index 33cb039..223ce44 100644 --- a/cmd/publisher/main.go +++ b/cmd/publisher/main.go @@ -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) } diff --git a/cmd/subscriber/main.go b/cmd/subscriber/main.go index fb1df88..223ce44 100644 --- a/cmd/subscriber/main.go +++ b/cmd/subscriber/main.go @@ -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) } diff --git a/common/common.go b/common/common.go index 7a73e89..15c3c84 100644 --- a/common/common.go +++ b/common/common.go @@ -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") +)