-
Notifications
You must be signed in to change notification settings - Fork 1
/
grpc.go
98 lines (91 loc) · 1.94 KB
/
grpc.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
package grpctl
import (
"os"
"path"
"time"
"github.com/joshcarp/grpctl/internal/grpc"
"github.com/spf13/cobra"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/descriptorpb"
)
func reflectFileDesc(flags []string) ([]protoreflect.FileDescriptor, error) {
cmd := cobra.Command{
FParseErrWhitelist: cobra.FParseErrWhitelist{
UnknownFlags: true,
},
}
err := persistentFlags(&cmd, "")
if err != nil {
return nil, err
}
if len(flags) > 0 && flags[0] == "__complete" {
flags = flags[1:]
}
cmd.SetArgs(flags)
var fds []protoreflect.FileDescriptor
cmd.RunE = func(cmd *cobra.Command, args []string) error {
addr, err := cmd.Flags().GetString("address")
if err != nil {
return err
}
if addr == "" {
return nil
}
cfgFile, err := cmd.Flags().GetString("config")
if err != nil {
return err
}
if cfgFile == "" {
home, err := os.UserHomeDir()
if err != nil {
return err
}
cfgFile = path.Join(home, ".grpctl.yaml")
if _, err := os.Stat(cfgFile); os.IsNotExist(err) {
err = config{}.save(cfgFile)
if err != nil {
return err
}
}
}
cfg, err := loadConfig(cfgFile)
if err != nil {
return err
}
desc, err := cfg.Entries[addr].decodeDescriptor()
if err != nil {
return err
}
if len(desc) != 0 {
spb := &descriptorpb.FileDescriptorSet{}
err = proto.Unmarshal(desc, spb)
if err != nil {
return err
}
fds, err = grpc.ConvertToProtoReflectDesc(spb)
if err != nil {
return err
}
return nil
}
fdset, err := grpc.Reflect(cmd.Root().Context(), addr)
if err != nil {
return err
}
fds, err = grpc.ConvertToProtoReflectDesc(fdset)
if err != nil {
return err
}
b, err := proto.Marshal(fdset)
if err != nil {
return err
}
if err := cfg.add(cfgFile, addr, b, time.Minute*15); err != nil {
return err
}
return nil
}
err = cmd.Execute()
return fds, err
}