/
publish.go
49 lines (37 loc) · 914 Bytes
/
publish.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
package mqtt
import (
"flag"
"log"
)
type publish struct {
cli *client
topic string
qos uint
payload string
retain bool
}
func NewPublishCommand(cli *client, args []string) Command {
c := &publish{}
fs := flag.NewFlagSet("publish", flag.ExitOnError)
fs.StringVar(&c.topic, "topic", "/fperf/topic", "topic to publish")
fs.StringVar(&c.payload, "payload", "hello world", "what you want to publish")
fs.BoolVar(&c.retain, "retain", false, "retain messgae")
fs.UintVar(&c.qos, "qos", 1, "qos should be 0, 1, 2")
setOpt(fs, &cli.opt)
if err := fs.Parse(args[1:]); err != nil {
log.Fatal(err)
}
c.cli = cli
return c
}
func (c *publish) Exec() error {
qos := c.qos
payload := c.payload
topic := c.topic
mq := c.cli.cli
retain := c.retain
if token := mq.Publish(topic, byte(qos), retain, payload); token.Wait() && token.Error() != nil {
return token.Error()
}
return nil
}