-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnatsCli.go
More file actions
59 lines (51 loc) · 1.5 KB
/
natsCli.go
File metadata and controls
59 lines (51 loc) · 1.5 KB
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
package main
import (
"bufio"
"flag"
"fmt"
"log"
"os"
"github.com/nats-io/nats"
)
func main() {
host := flag.String("host", nats.DefaultURL, "Host Address")
runType := flag.String("type", "", "Type of command ( publish, subscribe )")
topic := flag.String("topic", "", "Topic to publish / subscribe")
data := flag.String("data", "", "Data to publish")
queuGroup := flag.String("queueGroup", "", "Queue group for subscription")
flag.Parse()
nc, err := nats.Connect(*host)
if err != nil {
log.Fatal("Failed connecting to nats server")
} else {
log.Print("Connected to nats at url: " + *host)
}
if *runType == "publish" {
if *topic == "" || *data == "" {
log.Fatal("Topic or data missing")
}
log.Print("Publishing to topic: " + *topic + " - with data: " + *data)
nc.Publish(*topic, []byte(*data))
log.Print("Publishing press enter to finish")
reader := bufio.NewReader(os.Stdin)
cont, _ := reader.ReadString('\n')
fmt.Print(cont)
} else if *runType == "subscribe" {
if *topic == "" {
log.Fatal("Topic must be supplied to subscribe")
}
if *queuGroup == "" {
log.Print("Subscribing to topic: " + *topic)
nc.Subscribe(*topic, func(m *nats.Msg) {
log.Print("Topic::" + *topic + " message::" + string(m.Data))
})
} else {
log.Print("subscribing to topic: " + *topic + " under queue group: " + *queuGroup)
nc.QueueSubscribe(*topic, *queuGroup, func(m *nats.Msg) {
log.Print("Topic::" + *topic + " message::" + string(m.Data))
})
}
for {
}
}
}