From f948a31c0326b18ecf5520747b7d6e0f1f0a3aa1 Mon Sep 17 00:00:00 2001 From: shirou Date: Sun, 14 Aug 2022 02:36:39 +0000 Subject: [PATCH] feat: remove h flag for help --- config.go | 2 +- main.go | 13 +++++++++---- mqtt.go | 11 +++++++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/config.go b/config.go index 4822818..2032f49 100644 --- a/config.go +++ b/config.go @@ -120,7 +120,7 @@ func getSettingsFromFile(confPath string, opts *MQTT.ClientOptions) error { scheme = "ssl" } brokerUri := fmt.Sprintf("%s://%s:%d", scheme, ret.Host, ret.Port) - log.Infof("Broker URI: %s", brokerUri) + log.Infof("Broker URI(from config): %s", brokerUri) opts.AddBroker(brokerUri) } diff --git a/main.go b/main.go index 7023618..601e083 100644 --- a/main.go +++ b/main.go @@ -106,16 +106,21 @@ func main() { app.Usage = usage app.Version = version + cli.HelpFlag = &cli.BoolFlag{ + Name: "help", + Usage: usage, + } + commonFlags := []cli.Flag{ &cli.StringFlag{ - Name: "host", - Value: "localhost", - Usage: "mqtt host to connect to. Defaults to localhost", + Name: "h,host", + Value: "", + Usage: "mqtt host to connect to. Defaults is localhost", EnvVars: []string{"MQTT_HOST"}}, &cli.IntFlag{ Name: "p, port", Value: 1883, - Usage: "network port to connect to. Defaults to 1883", + Usage: "network port to connect to. Defaults is 1883", EnvVars: []string{"MQTT_PORT"}}, &cli.StringFlag{ Name: "u,user", diff --git a/mqtt.go b/mqtt.go index 6c62fcd..e4f9ed8 100644 --- a/mqtt.go +++ b/mqtt.go @@ -30,7 +30,7 @@ func (m *MQTTClient) Connect() (MQTT.Client, error) { m.Client = MQTT.NewClient(m.Opts) - log.Info("connecting...") + log.Infof("connecting...") if token := m.Client.Connect(); token.Wait() && token.Error() != nil { return nil, token.Error() @@ -130,6 +130,10 @@ func NewOption(c *cli.Context) (*MQTT.ClientOptions, error) { opts.SetClientID(clientId) scheme := "tcp" + if port == 8883 { + scheme = "ssl" + } + cafile := c.String("cafile") key := c.String("key") cert := c.String("cert") @@ -155,7 +159,10 @@ func NewOption(c *cli.Context) (*MQTT.ClientOptions, error) { opts.SetPassword(password) } - if host != "" { + if host == "" { + host = "localhost" + } + if len(opts.Servers) == 0 { brokerUri := fmt.Sprintf("%s://%s:%d", scheme, host, port) log.Infof("Broker URI: %s", brokerUri)