-
Notifications
You must be signed in to change notification settings - Fork 22
/
publish.go
55 lines (46 loc) · 927 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
50
51
52
53
54
55
package main
import (
"bufio"
"os"
log "github.com/Sirupsen/logrus"
"github.com/urfave/cli/v2"
)
func publish(c *cli.Context) error {
setDebugLevel(c)
opts, err := NewOption(c)
if err != nil {
log.Error(err)
os.Exit(1)
}
client, err := connect(c, opts, map[string]byte{})
if err != nil {
log.Error(err)
os.Exit(1)
}
qos := c.Int("q")
topic := c.String("t")
if topic == "" {
log.Errorf("Please specify topic")
os.Exit(1)
}
log.Infof("Topic: %s", topic)
retain := c.Bool("r")
if c.Bool("s") {
// Read from Stdin
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
err = client.Publish(topic, scanner.Bytes(), qos, retain, true)
if err != nil {
log.Error(err)
}
}
} else {
payload := c.String("m")
err = client.Publish(topic, []byte(payload), qos, retain, true)
if err != nil {
log.Error(err)
}
}
log.Info("Published")
return client.Disconnect()
}