/
main.go
152 lines (129 loc) · 3.84 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2016 The LUCI Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"os"
"github.com/maruel/subcommands"
"github.com/TriggerMail/luci-go/auth"
"github.com/TriggerMail/luci-go/auth/client/authcli"
"github.com/TriggerMail/luci-go/common/cli"
"github.com/TriggerMail/luci-go/common/data/rand/mathrand"
"github.com/TriggerMail/luci-go/common/lhttp"
"github.com/TriggerMail/luci-go/common/logging"
"github.com/TriggerMail/luci-go/common/logging/gologger"
"github.com/TriggerMail/luci-go/grpc/prpc"
"github.com/TriggerMail/luci-go/hardcoded/chromeinfra"
)
const (
userAgent = "luci-prpc"
)
var logCfg = gologger.LoggerConfig{
Format: `%{message}`,
Out: os.Stderr,
}
// exit codes:
const (
ecInvalidCommandLine = -iota
ecAuthenticatedClientError
ecOtherError
)
type exitCode struct {
err error
code int
}
func (e *exitCode) Error() string { return e.err.Error() }
// cmdRun is a base of all prpc subcommands.
// It defines some common flags, such as logging and auth, and useful methods.
type cmdRun struct {
subcommands.CommandRunBase
verbose bool
auth authcli.Flags
}
// ModifyContext implements cli.ContextModificator.
func (r *cmdRun) ModifyContext(ctx context.Context) context.Context {
if r.verbose {
ctx = logging.SetLevel(ctx, logging.Debug)
}
return ctx
}
// registerBaseFlags registers common flags used by all subcommands.
func (r *cmdRun) registerBaseFlags(defaultAuthOpts auth.Options) {
r.Flags.BoolVar(&r.verbose, "verbose", false, "Enable more logging.")
r.auth.Register(&r.Flags, defaultAuthOpts)
}
func (r *cmdRun) authenticatedClient(ctx context.Context, host string) (*prpc.Client, error) {
authOpts, err := r.auth.Options()
if err != nil {
return nil, err
}
a := auth.NewAuthenticator(ctx, auth.OptionalLogin, authOpts)
httpClient, err := a.Client()
if err != nil {
return nil, err
}
client := prpc.Client{
C: httpClient,
Host: host,
Options: prpc.DefaultOptions(),
}
client.Options.Insecure = lhttp.IsLocalHost(host)
return &client, nil
}
// argErr prints an err and usage to stderr and returns an exit code.
func (r *cmdRun) argErr(shortDesc, usageLine, format string, a ...interface{}) int {
if format != "" {
fmt.Fprintf(os.Stderr, format+"\n", a...)
}
fmt.Fprintln(os.Stderr, shortDesc)
fmt.Fprintln(os.Stderr, usageLine)
fmt.Fprintln(os.Stderr, "\nFlags:")
r.Flags.PrintDefaults()
return ecInvalidCommandLine
}
// done prints err to stderr if it is not nil and returns an exit code.
func (r *cmdRun) done(err error) int {
if err != nil {
fmt.Fprintln(os.Stderr, err)
if err, ok := err.(*exitCode); ok {
return err.code
}
return ecOtherError
}
return 0
}
func getApplication(defaultAuthOpts auth.Options) *cli.Application {
return &cli.Application{
Name: "prpc",
Title: "Provisional Remote Procedure Call CLI",
Context: func(ctx context.Context) context.Context {
return logCfg.Use(ctx)
},
Commands: []*subcommands.Command{
cmdCall(defaultAuthOpts),
cmdShow(defaultAuthOpts),
{ /* spacer */ },
authcli.SubcommandLogin(defaultAuthOpts, "login", false),
authcli.SubcommandLogout(defaultAuthOpts, "logout", false),
{ /* spacer */ },
subcommands.CmdHelp,
},
}
}
func main() {
mathrand.SeedRandomly()
app := getApplication(chromeinfra.DefaultAuthOptions())
os.Exit(subcommands.Run(app, os.Args[1:]))
}