| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "fmt" | 8 "fmt" |
| 9 "os" | 9 "os" |
| 10 "strings" | |
| 11 | 10 |
| 12 "github.com/maruel/subcommands" | 11 "github.com/maruel/subcommands" |
| 13 "golang.org/x/net/context" | 12 "golang.org/x/net/context" |
| 14 | 13 |
| 15 "github.com/luci/luci-go/client/authcli" | 14 "github.com/luci/luci-go/client/authcli" |
| 16 "github.com/luci/luci-go/common/auth" | 15 "github.com/luci/luci-go/common/auth" |
| 17 "github.com/luci/luci-go/common/cli" | 16 "github.com/luci/luci-go/common/cli" |
| 17 "github.com/luci/luci-go/common/lhttp" |
| 18 "github.com/luci/luci-go/common/logging" | 18 "github.com/luci/luci-go/common/logging" |
| 19 "github.com/luci/luci-go/common/logging/gologger" | 19 "github.com/luci/luci-go/common/logging/gologger" |
| 20 "github.com/luci/luci-go/grpc/prpc" | 20 "github.com/luci/luci-go/grpc/prpc" |
| 21 ) | 21 ) |
| 22 | 22 |
| 23 const ( | 23 const ( |
| 24 userAgent = "luci-rpc" | 24 userAgent = "luci-rpc" |
| 25 ) | 25 ) |
| 26 | 26 |
| 27 var logCfg = gologger.LoggerConfig{ | 27 var logCfg = gologger.LoggerConfig{ |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 httpClient, err := a.Client() | 75 httpClient, err := a.Client() |
| 76 if err != nil { | 76 if err != nil { |
| 77 return nil, err | 77 return nil, err |
| 78 } | 78 } |
| 79 | 79 |
| 80 client := prpc.Client{ | 80 client := prpc.Client{ |
| 81 C: httpClient, | 81 C: httpClient, |
| 82 Host: host, | 82 Host: host, |
| 83 Options: prpc.DefaultOptions(), | 83 Options: prpc.DefaultOptions(), |
| 84 } | 84 } |
| 85 » client.Options.Insecure = isLocalHost(host) | 85 » client.Options.Insecure = lhttp.IsLocalHost(host) |
| 86 return &client, nil | 86 return &client, nil |
| 87 } | 87 } |
| 88 | 88 |
| 89 // argErr prints an err and usage to stderr and returns an exit code. | 89 // argErr prints an err and usage to stderr and returns an exit code. |
| 90 func (r *cmdRun) argErr(format string, a ...interface{}) int { | 90 func (r *cmdRun) argErr(format string, a ...interface{}) int { |
| 91 if format != "" { | 91 if format != "" { |
| 92 fmt.Fprintf(os.Stderr, format+"\n", a...) | 92 fmt.Fprintf(os.Stderr, format+"\n", a...) |
| 93 } | 93 } |
| 94 fmt.Fprintln(os.Stderr, r.cmd.ShortDesc) | 94 fmt.Fprintln(os.Stderr, r.cmd.ShortDesc) |
| 95 fmt.Fprintln(os.Stderr, r.cmd.UsageLine) | 95 fmt.Fprintln(os.Stderr, r.cmd.UsageLine) |
| 96 fmt.Fprintln(os.Stderr, "\nFlags:") | 96 fmt.Fprintln(os.Stderr, "\nFlags:") |
| 97 r.Flags.PrintDefaults() | 97 r.Flags.PrintDefaults() |
| 98 return ecInvalidCommandLine | 98 return ecInvalidCommandLine |
| 99 } | 99 } |
| 100 | 100 |
| 101 // done prints err to stderr if it is not nil and returns an exit code. | 101 // done prints err to stderr if it is not nil and returns an exit code. |
| 102 func (r *cmdRun) done(err error) int { | 102 func (r *cmdRun) done(err error) int { |
| 103 if err != nil { | 103 if err != nil { |
| 104 fmt.Fprintln(os.Stderr, err) | 104 fmt.Fprintln(os.Stderr, err) |
| 105 if err, ok := err.(*exitCode); ok { | 105 if err, ok := err.(*exitCode); ok { |
| 106 return err.code | 106 return err.code |
| 107 } | 107 } |
| 108 return ecOtherError | 108 return ecOtherError |
| 109 } | 109 } |
| 110 return 0 | 110 return 0 |
| 111 } | 111 } |
| 112 | 112 |
| 113 func isLocalHost(host string) bool { | |
| 114 switch { | |
| 115 case host == "localhost", strings.HasPrefix(host, "localhost:"): | |
| 116 case host == "127.0.0.1", strings.HasPrefix(host, "127.0.0.1:"): | |
| 117 case host == "[::1]", strings.HasPrefix(host, "[::1]:"): | |
| 118 case strings.HasPrefix(host, ":"): | |
| 119 | |
| 120 default: | |
| 121 return false | |
| 122 } | |
| 123 return true | |
| 124 } | |
| 125 | |
| 126 var application = &cli.Application{ | 113 var application = &cli.Application{ |
| 127 Name: "rpc", | 114 Name: "rpc", |
| 128 Title: "Remote Procedure Call CLI", | 115 Title: "Remote Procedure Call CLI", |
| 129 Context: func(ctx context.Context) context.Context { | 116 Context: func(ctx context.Context) context.Context { |
| 130 return logCfg.Use(ctx) | 117 return logCfg.Use(ctx) |
| 131 }, | 118 }, |
| 132 Commands: []*subcommands.Command{ | 119 Commands: []*subcommands.Command{ |
| 133 cmdCall, | 120 cmdCall, |
| 134 cmdShow, | 121 cmdShow, |
| 135 cmdFmt, | 122 cmdFmt, |
| 136 authcli.SubcommandLogin(auth.Options{}, "login"), | 123 authcli.SubcommandLogin(auth.Options{}, "login"), |
| 137 authcli.SubcommandLogout(auth.Options{}, "logout"), | 124 authcli.SubcommandLogout(auth.Options{}, "logout"), |
| 138 subcommands.CmdHelp, | 125 subcommands.CmdHelp, |
| 139 }, | 126 }, |
| 140 } | 127 } |
| 141 | 128 |
| 142 func main() { | 129 func main() { |
| 143 os.Exit(subcommands.Run(application, os.Args[1:])) | 130 os.Exit(subcommands.Run(application, os.Args[1:])) |
| 144 } | 131 } |
| OLD | NEW |