func getSettingsFromFile(p string, opts *MQTT.ClientOptions) error { confPath := "" home := UserHomeDir() // replace home to ~ in order to match p = strings.Replace(p, home, "~", 1) if p == "~/.mqttcli.cfg" || p == "" { confPath = path.Join(home, DefaultConfigFile) _, err := os.Stat(confPath) if os.IsNotExist(err) { return err } } else { confPath = p } ret, err := readFromConfigFile(confPath) if err != nil { log.Error(err) return err } tlsConfig, ok, err := makeTlsConfig(ret.CaCert, ret.ClientCert, ret.PrivateKey, false) if err != nil { return err } if ok { opts.SetTLSConfig(tlsConfig) } if ret.Host != "" { if ret.Port == 0 { ret.Port = 1883 } scheme := "tcp" if ret.Port == 8883 { scheme = "ssl" } brokerUri := fmt.Sprintf("%s://%s:%d", scheme, ret.Host, ret.Port) log.Infof("Broker URI: %s", brokerUri) opts.AddBroker(brokerUri) } if ret.UserName != "" { opts.SetUsername(ret.UserName) } if ret.Password != "" { opts.SetPassword(ret.Password) } return nil }
func getSettingsFromFile(p string, opts *MQTT.ClientOptions) error { confPath := "" if p == "~/.mqtt.cfg" || p == "" { home := UserHomeDir() confPath = path.Join(home, DefaultConfigFile) _, err := os.Stat(confPath) if os.IsNotExist(err) { return err } } else { confPath = p } ret, err := readFromConfigFile(confPath) if err != nil { return err } if ret.Host != "" { if ret.Port == "" { ret.Port = "1883" } scheme := "tcp" // FIXME: brokerUri := fmt.Sprintf("%s://%s:%s", scheme, ret.Host, ret.Port) log.Infof("Broker URI: %s", brokerUri) opts.AddBroker(brokerUri) } if ret.UserName != "" { opts.SetUsername(ret.UserName) } if ret.Password != "" { opts.SetPassword(ret.Password) } return nil }