forked from micro/micro
/
helpers.go
100 lines (83 loc) · 2.43 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
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/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 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)
}