-
Notifications
You must be signed in to change notification settings - Fork 1k
/
helpers.go
135 lines (111 loc) · 3.17 KB
/
helpers.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
package cli
import (
"context"
"encoding/json"
"errors"
"fmt"
"os"
"strings"
"time"
"github.com/micro/cli"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/config/cmd"
clic "github.com/micro/micro/internal/command/cli"
)
type exec func(*cli.Context, []string) ([]byte, error)
func printer(e exec) func(*cli.Context) {
return func(c *cli.Context) {
rsp, err := e(c, c.Args())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Printf("%s\n", string(rsp))
}
}
func list(c *cli.Context, args []string) ([]byte, error) {
// no args
if len(args) == 0 {
return clic.ListServices(c)
}
// check first arg
switch args[0] {
case "services":
return clic.ListServices(c)
case "nodes":
return clic.NetworkNodes(c)
case "routes":
return clic.NetworkRoutes(c)
}
return nil, errors.New("unknown command")
}
func networkGraph(c *cli.Context, args []string) ([]byte, error) {
return clic.NetworkGraph(c)
}
func netNodes(c *cli.Context, args []string) ([]byte, error) {
return clic.NetworkNodes(c)
}
func netRoutes(c *cli.Context, args []string) ([]byte, error) {
return clic.NetworkRoutes(c)
}
func netServices(c *cli.Context, args []string) ([]byte, error) {
return clic.NetworkServices(c)
}
func listServices(c *cli.Context, args []string) ([]byte, error) {
return clic.ListServices(c)
}
func registerService(c *cli.Context, args []string) ([]byte, error) {
return clic.RegisterService(c, args)
}
func deregisterService(c *cli.Context, args []string) ([]byte, error) {
return clic.DeregisterService(c, args)
}
func getService(c *cli.Context, args []string) ([]byte, error) {
return clic.GetService(c, args)
}
func callService(c *cli.Context, args []string) ([]byte, error) {
return clic.CallService(c, args)
}
// TODO: stream via HTTP
func streamService(c *cli.Context, args []string) ([]byte, error) {
if len(args) < 2 {
return nil, errors.New("require service and endpoint")
}
service := args[0]
endpoint := args[1]
var request map[string]interface{}
err := json.Unmarshal([]byte(strings.Join(args[2:], " ")), &request)
if err != nil {
return nil, err
}
req := (*cmd.DefaultOptions().Client).NewRequest(service, endpoint, request, client.WithContentType("application/json"))
stream, err := (*cmd.DefaultOptions().Client).Stream(context.Background(), req)
if err != nil {
return nil, fmt.Errorf("error calling %s.%s: %v", service, endpoint, err)
}
if err := stream.Send(request); err != nil {
return nil, fmt.Errorf("error sending to %s.%s: %v", service, endpoint, err)
}
for {
var response map[string]interface{}
if err := stream.Recv(&response); err != nil {
return nil, fmt.Errorf("error receiving from %s.%s: %v", service, endpoint, err)
}
b, _ := json.MarshalIndent(response, "", "\t")
fmt.Print(string(b))
// artificial delay
time.Sleep(time.Millisecond * 10)
}
}
func publish(c *cli.Context, args []string) ([]byte, error) {
if err := clic.Publish(c, args); err != nil {
return nil, err
}
return []byte(`ok`), nil
}
func queryHealth(c *cli.Context, args []string) ([]byte, error) {
return clic.QueryHealth(c, args)
}
func queryStats(c *cli.Context, args []string) ([]byte, error) {
return clic.QueryStats(c, args)
}