forked from shirou/mqttcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
139 lines (125 loc) · 2.91 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
"strconv"
"strings"
MQTT "git.eclipse.org/gitroot/paho/org.eclipse.paho.mqtt.golang.git"
log "github.com/Sirupsen/logrus"
simpleJson "github.com/bitly/go-simplejson"
)
const DefaultConfigFile = ".mqttcli.cfg" // Under HOME
type Config struct {
Host string `json:"host"`
Port int `json:"port"`
UserName string `json:"username"`
Password string `json:"password"`
CaCert string `json:"caCert"`
ClientCert string `json:"clientCert"`
PrivateKey string `json:"privateKey"`
}
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 {
c.Host = ""
}
// 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 {
c.Port = 0
}
}
if c.UserName, err = js.Get("username").String(); err != nil {
c.UserName = ""
}
if c.Password, err = js.Get("password").String(); err != nil {
c.Password = ""
}
if c.CaCert, err = js.Get("caCert").String(); err != nil {
c.CaCert = ""
}
if c.ClientCert, err = js.Get("clientCert").String(); err != nil {
c.ClientCert = ""
}
if c.PrivateKey, err = js.Get("privateKey").String(); err != nil {
c.PrivateKey = ""
}
return nil
}
func readFromConfigFile(path string) (Config, error) {
ret := Config{}
b, err := ioutil.ReadFile(path)
if err != nil {
return ret, err
}
err = json.Unmarshal(b, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
// func UserHomeDir() string {
// if runtime.GOOS == "windows" {
// home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
// if home == "" {
// home = os.Getenv("USERPROFILE")
// }
// return home
// }
// return os.Getenv("HOME")
// }
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
}