-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmain.go
286 lines (265 loc) · 5.59 KB
/
main.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package main
import (
"bufio"
stdlog "log"
"os"
"sync"
"time"
log "github.com/Sirupsen/logrus"
MQTT "github.com/eclipse/paho.mqtt.golang"
colorable "github.com/mattn/go-colorable"
"github.com/urfave/cli/v2"
)
var usage = `
Usage here
`
var version string
func init() {
log.SetLevel(log.WarnLevel)
log.SetOutput(colorable.NewColorableStdout())
}
// connects MQTT broker
func connect(c *cli.Context, opts *MQTT.ClientOptions, subscribed map[string]byte) (*MQTTClient, error) {
willPayload := c.String("will-payload")
willQoS := c.Int("will-qos")
willRetain := c.Bool("will-retain")
willTopic := c.String("will-topic")
if willPayload != "" && willTopic != "" {
opts.SetWill(willTopic, willPayload, byte(willQoS), willRetain)
}
client := &MQTTClient{Opts: opts}
client.lock = new(sync.Mutex)
client.Subscribed = subscribed
opts.SetOnConnectHandler(client.SubscribeOnConnect)
opts.SetConnectionLostHandler(client.ConnectionLost)
_, err := client.Connect()
if err != nil {
return nil, err
}
return client, nil
}
func pubsub(c *cli.Context) error {
setDebugLevel(c)
opts, err := NewOption(c)
if err != nil {
log.Error(err)
os.Exit(1)
}
qos := c.Int("q")
subtopic := c.String("sub")
if subtopic == "" {
log.Errorf("Please specify sub topic")
os.Exit(1)
}
log.Infof("Sub Topic: %s", subtopic)
pubtopic := c.String("pub")
if pubtopic == "" {
log.Errorf("Please specify pub topic")
os.Exit(1)
}
log.Infof("Pub Topic: %s", pubtopic)
retain := c.Bool("r")
subscribed := map[string]byte{
subtopic: byte(0),
}
client, err := connect(c, opts, subscribed)
if err != nil {
log.Error(err)
os.Exit(1)
}
go func() {
// Read from Stdin and publish
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
err = client.Publish(pubtopic, []byte(scanner.Text()), qos, retain, false)
if err != nil {
log.Error(err)
}
}
}()
// while loop
for {
time.Sleep(1 * time.Second)
}
return nil
}
func main() {
app := cli.NewApp()
app.Name = "mqttcli"
app.Usage = usage
app.Version = version
cli.HelpFlag = &cli.BoolFlag{
Name: "help",
Usage: usage,
}
commonFlags := []cli.Flag{
&cli.StringFlag{
Name: "host",
Aliases: []string{"h"},
Value: "",
Usage: "mqtt host to connect to. Defaults is localhost",
EnvVars: []string{"MQTT_HOST"}},
&cli.IntFlag{
Name: "port",
Aliases: []string{"p"},
Value: 1883,
Usage: "network port to connect to. Defaults is 1883",
EnvVars: []string{"MQTT_PORT"}},
&cli.StringFlag{
Name: "user",
Aliases: []string{"u"},
Value: "",
Usage: "provide a username",
EnvVars: []string{"MQTT_USERNAME"}},
&cli.StringFlag{
Name: "password",
Aliases: []string{"P"},
Value: "",
Usage: "provide a password",
EnvVars: []string{"MQTT_PASSWORD"}},
&cli.StringFlag{
Name: "t",
Value: "",
Usage: "mqtt topic to publish to.",
},
&cli.IntFlag{
Name: "q",
Value: 0,
Usage: "QoS",
},
&cli.StringFlag{
Name: "cafile",
Value: "",
Usage: "CA certificates",
},
&cli.StringFlag{
Name: "cert",
Value: "",
Usage: "Client certificates",
},
&cli.StringFlag{
Name: "key",
Value: "",
Usage: "Client private key",
},
&cli.StringFlag{
Name: "i",
Value: "",
Usage: "ClientiId. Defaults random.",
},
&cli.StringFlag{
Name: "m",
Value: "test message",
Usage: "Message body",
},
&cli.BoolFlag{
Name: "r",
Usage: "message should be retained.",
},
&cli.BoolFlag{
Name: "d",
Usage: "enable debug messages",
},
&cli.BoolFlag{
Name: "dd",
Usage: "enable debug messages",
},
&cli.BoolFlag{
Name: "ddd",
Usage: "enable debug messages",
},
&cli.BoolFlag{
Name: "dddd",
Usage: "enable debug messages",
},
&cli.BoolFlag{
Name: "ddddd",
Usage: "enable debug messages",
},
&cli.BoolFlag{
Name: "insecure",
Usage: "do not check that the server certificate",
},
&cli.StringFlag{
Name: "conf",
Value: DefaultConfigFilePath,
Usage: "config file path",
EnvVars: []string{"MQTTCLI_CONFPATH"}},
&cli.StringFlag{
Name: "will-payload",
Value: "",
Usage: "payload for the client Will",
},
&cli.IntFlag{
Name: "will-qos",
Value: 0,
Usage: "QoS level for the client Will",
},
&cli.BoolFlag{
Name: "will-retain",
Usage: "if given, make the client Will retained",
},
&cli.StringFlag{
Name: "will-topic",
Value: "",
Usage: "the topic on which to publish the client Will",
},
}
pubFlags := append(commonFlags,
&cli.BoolFlag{
Name: "s",
Usage: "read message from stdin, sending line by line as a message",
},
)
subFlags := append(commonFlags,
&cli.BoolFlag{
Name: "c",
Usage: "disable 'clean session'",
},
)
pubsubFlags := append(commonFlags,
&cli.StringFlag{
Name: "pub",
Usage: "publish topic",
},
&cli.StringFlag{
Name: "sub",
Usage: "subscribe topic",
},
)
app.Commands = []*cli.Command{
{
Name: "pub",
Usage: "publish",
Flags: pubFlags,
Action: publish,
},
{
Name: "sub",
Usage: "subscribe",
Flags: subFlags,
Action: subscribe,
},
{
Name: "pubsub",
Usage: "subscribe and publish",
Flags: pubsubFlags,
Action: pubsub,
},
}
err := app.Run(os.Args)
if err != nil {
log.Error(err)
}
}
func setDebugLevel(c *cli.Context) {
if c.Bool("d") {
log.SetLevel(log.DebugLevel)
} else if c.Bool("dd") {
log.SetLevel(log.DebugLevel)
MQTT.WARN = stdlog.New(os.Stdout, "", stdlog.LstdFlags)
} else if c.Bool("ddd") {
log.SetLevel(log.DebugLevel)
MQTT.DEBUG = stdlog.New(os.Stdout, "", stdlog.LstdFlags)
}
}