mirror of
https://github.com/shirou/mqttcli.git
synced 2025-05-02 22:17:09 +08:00
port in a config file can be string either int.
This commit is contained in:
parent
2af1028bc8
commit
e138636529
27
config.go
27
config.go
@ -7,10 +7,12 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
|
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
|
||||||
log "github.com/Sirupsen/logrus"
|
log "github.com/Sirupsen/logrus"
|
||||||
|
simpleJson "github.com/bitly/go-simplejson"
|
||||||
)
|
)
|
||||||
|
|
||||||
const DefaultConfigFile = ".mqttcli.cfg" // Under HOME
|
const DefaultConfigFile = ".mqttcli.cfg" // Under HOME
|
||||||
@ -22,6 +24,31 @@ type Config struct {
|
|||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Config) UnmarshalJSON(data []byte) error {
|
||||||
|
js, err := simpleJson.NewJson(data)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if c.Host, err = js.Get("host").String(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Port can be string either int
|
||||||
|
if c.Port, err = js.Get("port").Int(); err != nil {
|
||||||
|
p, err := js.Get("port").String()
|
||||||
|
c.Port, err = strconv.Atoi(p)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if c.UserName, err = js.Get("username").String(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if c.Password, err = js.Get("password").String(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func readFromConfigFile(path string) (Config, error) {
|
func readFromConfigFile(path string) (Config, error) {
|
||||||
ret := Config{}
|
ret := Config{}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user