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

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: rebased 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
« no previous file with comments | « client/cmd/rpc/printer_test.proto ('k') | common/indented/writer.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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: host ("example.com") or port for localhost (":8080").
26 name: a full name of service, method or type. If not specified prints names
27 of all available services.
28
29 Flags:`,
30 ShortDesc: "lists services and prints a definition of a referenced objec t.",
31 LongDesc: "Lists services and prints a definition of a referenced objec t.",
32 CommandRun: func() subcommands.CommandRun {
33 c := &showRun{}
34 c.registerBaseFlags()
35 return c
36 },
37 }
38
39 type showRun struct {
40 cmdRun
41 }
42
43 func (r *showRun) Run(a subcommands.Application, args []string) int {
44 if len(args) == 0 || len(args) > 2 {
45 return r.argErr("")
46 }
47
48 server, err := parseServer(args[0])
49 if err != nil {
50 return r.argErr("server: %s", err)
51 }
52 var name string
53 if len(args) > 1 {
54 name = args[1]
55 }
56
57 return r.done(show(r.initContext(), server, name))
58 }
59
60 // show prints a definition of an object referenced by name in proto3 style.
61 func show(c context.Context, server *url.URL, name string) error {
62 desc, err := loadDescription(c, server)
63 if err != nil {
64 return fmt.Errorf("could not load server description: %s", err)
65 }
66
67 if name == "" {
68 for _, s := range desc.services {
69 fmt.Println(s)
70 }
71 return nil
72 }
73
74 file, obj, path := desc.descriptor.Resolve(name)
75 if obj == nil {
76 return fmt.Errorf("name %q could not resolved against %s", name, server)
77 }
78
79 print := newPrinter(os.Stdout)
80 print.File = file
81
82 switch obj := obj.(type) {
83
84 case *descriptor.ServiceDescriptorProto:
85 print.Service(obj, path[1], -1)
86
87 case *descriptor.MethodDescriptorProto:
88 serviceIndex, methodIndex := path[1], path[3]
89 print.Service(file.Service[serviceIndex], serviceIndex, methodIn dex)
90
91 printMsg := func(name string) {
92 name = strings.TrimPrefix(name, ".")
93 file, msg, path := desc.descriptor.Resolve(name)
94 if msg == nil {
95 print.Printf("// Message %q is not found\n", nam e)
96 return
97 }
98 print.File = file
99 print.Message(msg.(*descriptor.DescriptorProto), path)
100 }
101
102 print.Printf("\n")
103 printMsg(obj.GetInputType())
104 print.Printf("\n")
105 printMsg(obj.GetOutputType())
106
107 case *descriptor.DescriptorProto:
108 print.Message(obj, path)
109
110 case *descriptor.FieldDescriptorProto:
111 print.Field(obj, path)
112
113 case *descriptor.EnumDescriptorProto:
114 print.Enum(obj, path)
115
116 case *descriptor.EnumValueDescriptorProto:
117 print.EnumValue(obj, path)
118
119 default:
120 return fmt.Errorf("object of type %T is not supported", obj)
121 }
122
123 return print.Err
124 }
125
126 type serverDescription struct {
127 services []string
128 descriptor *descriptor.FileDescriptorSet
129 }
130
131 func loadDescription(c context.Context, server *url.URL) (*serverDescription, er ror) {
132 // TODO(nodir): cache description on the file system.
133 req := &request{
134 server: server,
135 service: "discovery.Discovery",
136 method: "Describe",
137 format: formatBinary,
138 }
139 var buf bytes.Buffer
140 if err := call(c, req, &buf); err != nil {
141 return nil, fmt.Errorf("could not load server description: %s", err)
142 }
143 var res discovery.DescribeResponse
144 if err := proto.Unmarshal(buf.Bytes(), &res); err != nil {
145 return nil, fmt.Errorf("could not unmarshal response: %s", err)
146 }
147 result := &serverDescription{
148 services: res.Services,
149 descriptor: &descriptor.FileDescriptorSet{},
150 }
151 if err := proto.Unmarshal(res.FileDescriptionSet, result.descriptor); er r != nil {
152 return nil, fmt.Errorf("could not unmarshal FileDescriptionSet: %s", err)
153 }
154 return result, nil
155 }
OLDNEW
« no previous file with comments | « client/cmd/rpc/printer_test.proto ('k') | common/indented/writer.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698