forked from fullstorydev/grpcui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiles.go
47 lines (43 loc) · 1.62 KB
/
files.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
package grpcui
import (
"github.com/jhump/protoreflect/desc"
"github.com/jhump/protoreflect/grpcreflect"
"golang.org/x/net/context"
"google.golang.org/grpc"
rpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/reflect/protoregistry"
"github.com/fullstorydev/grpcurl"
)
// AllFilesViaReflection returns a slice that contains the file descriptors
// for all methods exposed by the server on the other end of the given
// connection. This returns an error if the server does not support service
// reflection. (See "google.golang.org/grpc/reflection" for more on service
// reflection.)
func AllFilesViaReflection(ctx context.Context, cc grpc.ClientConnInterface) ([]*desc.FileDescriptor, error) {
stub := rpb.NewServerReflectionClient(cc)
cli := grpcreflect.NewClientV1Alpha(ctx, stub)
source := grpcurl.DescriptorSourceFromServer(ctx, cli)
return grpcurl.GetAllFiles(source)
}
// AllFilesViaInProcess returns a slice that contains all file descriptors
// known to this server process. This collects descriptors for all files
// registered with protoregistry.GlobalFiles, which includes all compiled
// proto files linked into the current program.
func AllFilesViaInProcess() ([]*desc.FileDescriptor, error) {
var fds []*desc.FileDescriptor
var err error
protoregistry.GlobalFiles.RangeFiles(func(d protoreflect.FileDescriptor) bool {
var fd *desc.FileDescriptor
fd, err = desc.LoadFileDescriptor(d.Path())
if err != nil {
return false
}
fds = append(fds, fd)
return true
})
if err != nil {
return nil, err
}
return fds, nil
}