/
node.go
61 lines (57 loc) · 1.12 KB
/
node.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
package main
import (
"fmt"
"github.com/urfave/cli/v2"
"github.com/veypi/utils/log"
"github.com/veypi/webds/message"
)
var Node = &cli.Command{
Name: "node",
Usage: "webds node list/stop",
Description: "command about node",
Subcommands: []*cli.Command{
{
Name: "list",
Usage: "webds node list",
Action: runNodeList,
Flags: nil,
},
{
Name: "stop",
Usage: "webds node stop node1 node2 ...",
Action: runStopNode,
},
},
Flags: nil,
}
func runNodeList(c *cli.Context) error {
conn, err := newConn()
if err != nil {
return err
}
conn.OnConnect(func() {
conn.Echo(message.TopicGetAllNodes, "")
})
conn.Subscribe(message.TopicGetAllNodes, func(data interface{}) {
fmt.Print(data)
log.HandlerErrs(conn.Close())
})
return conn.Wait()
}
func runStopNode(c *cli.Context) error {
if c.Args().Len() == 0 {
log.Warn().Msg("missing node_id")
return nil
}
conn, err := newConn()
if err != nil {
return err
}
conn.OnConnect(func() {
for _, v := range c.Args().Slice() {
conn.Echo(message.TopicStopNode, v)
}
log.HandlerErrs(conn.Close())
})
return conn.Wait()
}