Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(22)

Side by Side Diff: client/cmd/rpc/show.go

Issue 1587323003: client/cmd/rpc: RPC CLI (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@discovery
Patch Set: fix Resolve, do not print package Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package main
6
7 import (
8 "bytes"
9 "fmt"
10 "net/url"
11 "os"
12 "strings"
13
14 "github.com/golang/protobuf/proto"
15 "github.com/maruel/subcommands"
16 "golang.org/x/net/context"
17
18 "github.com/luci/luci-go/common/proto/google/descriptor"
19 "github.com/luci/luci-go/server/discovery"
20 )
21
22 var cmdShow = &subcommands.Command{
23 UsageLine: `show [flags] <server> [name]
24
25 server: scheme (http or https) and hostname, e.g. "https://example.com".
26 A short syntax for "http://localhost:port" is ":port", e.g. ":8080".
27 name: a full name of service, method or type. If not specified prints names
28 of all available services.
29
30 Flags:`,
31 ShortDesc: "lists services and prints a definition of a referenced objec t.",
32 LongDesc: "Lists services and prints a definition of a referenced objec t.",
33 CommandRun: func() subcommands.CommandRun {
34 c := &showRun{}
35 c.registerBaseFlags()
36 return c
37 },
38 }
39
40 type showRun struct {
41 cmdRun
42 }
43
44 func (r *showRun) Run(a subcommands.Application, args []string) int {
45 if len(args) == 0 || len(args) > 2 {
46 return r.argErr("")
47 }
48
49 server, err := parseServer(args[0])
50 if err != nil {
51 return r.argErr("server: %s", err)
52 }
53 var name string
54 if len(args) > 1 {
55 name = args[1]
56 }
57
58 return r.done(show(r.initContext(), server, name))
59 }
60
61 // show prints a definition of an object referenced by name in proto3 style.
62 func show(c context.Context, server *url.URL, name string) error {
63 desc, err := loadDescription(c, server)
64 if err != nil {
65 return fmt.Errorf("could not load server description: %s", err)
66 }
67
68 if name == "" {
69 for _, s := range desc.services {
70 fmt.Println(s)
71 }
72 return nil
73 }
74
75 file, obj, path := desc.descriptor.Resolve(name)
76 if obj == nil {
77 return fmt.Errorf("name %q could not resolved against %s", name, server)
78 }
79
80 print := newPrinter(os.Stdout)
81 print.File = file
82
83 switch obj := obj.(type) {
84
85 case *descriptor.ServiceDescriptorProto:
86 print.Service(obj, path[1], -1)
87
88 case *descriptor.MethodDescriptorProto:
89 serviceIndex, methodIndex := path[1], path[3]
90 print.Service(file.Service[serviceIndex], serviceIndex, methodIn dex)
91
92 printMsg := func(name string) {
93 name = strings.TrimPrefix(name, ".")
94 file, msg, path := desc.descriptor.Resolve(name)
95 if msg == nil {
96 print.Printf("// Message %q is not found\n", nam e)
97 return
98 }
99 print.File = file
100 print.Message(msg.(*descriptor.DescriptorProto), path)
101 }
102
103 print.Printf("\n")
104 printMsg(obj.GetInputType())
105 print.Printf("\n")
106 printMsg(obj.GetOutputType())
107
108 case *descriptor.DescriptorProto:
109 print.Message(obj, path)
110
111 case *descriptor.FieldDescriptorProto:
112 print.Field(obj, path)
113
114 case *descriptor.EnumDescriptorProto:
115 print.Enum(obj, path)
116
117 case *descriptor.EnumValueDescriptorProto:
118 print.EnumValue(obj, path)
119
120 default:
121 return fmt.Errorf("object of type %T is not supported", obj)
122 }
123
124 return print.Err
125 }
126
127 type serverDescription struct {
128 services []string
129 descriptor *descriptor.FileDescriptorSet
130 }
131
132 func loadDescription(c context.Context, server *url.URL) (*serverDescription, er ror) {
133 // TODO(nodir): cache description on the file system.
134 req := &request{
135 server: server,
136 service: "discovery.Discovery",
137 method: "Describe",
138 format: formatBinary,
139 }
140 var buf bytes.Buffer
141 if err := call(c, req, &buf); err != nil {
142 return nil, fmt.Errorf("could not load server description: %s", err)
143 }
144 var res discovery.DescribeResponse
145 if err := proto.Unmarshal(buf.Bytes(), &res); err != nil {
146 return nil, fmt.Errorf("could not unmarshal response: %s", err)
147 }
148 result := &serverDescription{
149 services: res.Services,
150 descriptor: &descriptor.FileDescriptorSet{},
151 }
152 if err := proto.Unmarshal(res.FileDescriptionSet, result.descriptor); er r != nil {
153 return nil, fmt.Errorf("could not unmarshal FileDescriptionSet: %s", err)
154 }
155 return result, nil
156 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698