forked from kazegusuri/grpcurl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list_services.go
110 lines (94 loc) · 2.39 KB
/
list_services.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
package main
import (
"context"
"fmt"
"github.com/jhump/protoreflect/grpcreflect"
"github.com/spf13/cobra"
)
type ListServicesCommand struct {
cmd *cobra.Command
opts *GlobalOptions
addr string
rcli *grpcreflect.Client
long bool
full bool
}
func NewListServicesCommand(opts *GlobalOptions) *ListServicesCommand {
c := &ListServicesCommand{
cmd: &cobra.Command{
Use: "list_services ADDR [FULL_SERVICE_NAME]",
Short: "List services and methods provided by gRPC server",
Example: `
* List services
grpcurl ls localhost:8888
* List methods of the service
grpcurl ls localhost:8888 test.TestService
`,
Aliases: []string{"ls"},
Args: cobra.RangeArgs(1, 2),
SilenceUsage: true,
},
opts: opts,
}
c.cmd.RunE = c.Run
c.cmd.Flags().BoolVarP(&c.long, "long", "l", false, "list long")
c.cmd.Flags().BoolVarP(&c.full, "full", "F", false, "fully qualified")
return c
}
func (c *ListServicesCommand) Command() *cobra.Command {
return c.cmd
}
func (c *ListServicesCommand) Run(cmd *cobra.Command, args []string) error {
nargs := len(args)
ctx := context.Background()
c.addr = args[0]
conn, err := NewGRPCConnection(ctx, c.addr, c.opts.userAgent, c.opts.Insecure)
if err != nil {
return err
}
defer conn.Close()
c.rcli = NewServerReflectionClient(ctx, conn)
if nargs == 1 {
return c.listServices(ctx)
} else if len(args) == 2 {
return c.listMethods(ctx, args[1])
}
return nil
}
func (c *ListServicesCommand) listServices(ctx context.Context) error {
svcs, err := c.rcli.ListServices()
if err != nil {
return err
}
for i := range svcs {
fmt.Fprintf(c.opts.Output, "%s\n", svcs[i])
}
return nil
}
func (c *ListServicesCommand) listMethods(ctx context.Context, serviceName string) error {
sdesc, err := c.rcli.ResolveService(serviceName)
if err != nil {
return err
}
for _, mdesc := range sdesc.GetMethods() {
if c.long {
inType := mdesc.GetInputType()
outType := mdesc.GetOutputType()
inRPCType := ""
outRPCType := ""
if mdesc.IsClientStreaming() {
inRPCType = "streaming "
}
if mdesc.IsServerStreaming() {
outRPCType = "streaming "
}
fmt.Fprintf(c.opts.Output, "%s(%s%s) return (%s%s)\n",
mdesc.GetFullyQualifiedName(),
inRPCType, inType.GetFullyQualifiedName(),
outRPCType, outType.GetFullyQualifiedName())
} else {
fmt.Fprintf(c.opts.Output, "%s\n", mdesc.GetFullyQualifiedName())
}
}
return nil
}