MQTTBrokerstring`flag:"mqtt-broker,b" default:"tcp://localhost:1883" description:"Broker URI to connect to scheme://host:port (scheme is one of 'tcp', 'ssl', or 'ws')"`
MQTTClientIDstring`flag:"mqtt-client-id" vardefault:"client-id" description:"Client ID to use when connecting, must be unique"`
MQTTUserstring`flag:"mqtt-user,u" default:"" description:"Username to identify against the broker" validate:"nonzero"`
MQTTPassstring`flag:"mqtt-pass,p" default:"" description:"Password to identify against the broker" validate:"nonzero"`
MQTTTimeouttime.Duration`flag:"mqtt-timeout" default:"10s" description:"How long to wait for the client to complete operations"`
OutputFormatstring`flag:"output-format,o" default:"log" description:"How to ouptut received messages (One of 'log', 'csv', 'jsonl')"`
QOSint`flag:"qos" default:"1" description:"QOS to use (0 - Only Once, 1 - At Least Once, 2 - Only Once)"`
Retainbool`flag:"retain" default:"false" description:"Retain message on topic"`
Topics[]string`flag:"topic,t" default:"" description:"Topic to subscribe / publish to"`
VersionAndExitbool`flag:"version" default:"false" description:"Prints current version and exits"`
}{}
version="dev"
)
funcinit(){
rconfig.AutoEnv(true)
rconfig.SetVariableDefaults(map[string]string{
"client-id":uuid.Must(uuid.NewV4()).String(),
})
iferr:=rconfig.ParseAndValidate(&cfg);err!=nil{
log.Fatalf("Unable to parse commandline options: %s",err)
}
ifcfg.VersionAndExit{
fmt.Printf("mqttcli %s\n",version)
os.Exit(0)
}
ifl,err:=log.ParseLevel(cfg.LogLevel);err!=nil{
log.WithError(err).Fatal("Unable to parse log level")