| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package main | 5 package main |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 "flag" | 9 "flag" |
| 10 "fmt" |
| 10 "os" | 11 "os" |
| 11 "os/signal" | 12 "os/signal" |
| 12 | 13 |
| 13 "github.com/maruel/subcommands" | 14 "github.com/maruel/subcommands" |
| 14 "golang.org/x/net/context" | 15 "golang.org/x/net/context" |
| 15 | 16 |
| 16 "github.com/luci/luci-go/client/authcli" | 17 "github.com/luci/luci-go/client/authcli" |
| 17 "github.com/luci/luci-go/common/auth" | 18 "github.com/luci/luci-go/common/auth" |
| 18 "github.com/luci/luci-go/common/cli" | 19 "github.com/luci/luci-go/common/cli" |
| 20 "github.com/luci/luci-go/common/config" |
| 19 "github.com/luci/luci-go/common/logdog/coordinator" | 21 "github.com/luci/luci-go/common/logdog/coordinator" |
| 20 log "github.com/luci/luci-go/common/logging" | 22 log "github.com/luci/luci-go/common/logging" |
| 21 "github.com/luci/luci-go/common/logging/gologger" | 23 "github.com/luci/luci-go/common/logging/gologger" |
| 22 "github.com/luci/luci-go/common/prpc" | 24 "github.com/luci/luci-go/common/prpc" |
| 23 ) | 25 ) |
| 24 | 26 |
| 25 func init() { | 27 func init() { |
| 26 prpc.DefaultUserAgent = "logdog_cat" | 28 prpc.DefaultUserAgent = "logdog_cat" |
| 27 } | 29 } |
| 28 | 30 |
| 29 type application struct { | 31 type application struct { |
| 30 cli.Application | 32 cli.Application |
| 31 context.Context | 33 context.Context |
| 32 | 34 |
| 35 project config.ProjectName |
| 33 authFlags authcli.Flags | 36 authFlags authcli.Flags |
| 34 coordinator string | 37 coordinator string |
| 35 insecure bool | 38 insecure bool |
| 36 | 39 |
| 37 coord *coordinator.Client | 40 coord *coordinator.Client |
| 38 } | 41 } |
| 39 | 42 |
| 40 func (a *application) addToFlagSet(ctx context.Context, fs *flag.FlagSet) { | 43 func (a *application) addToFlagSet(ctx context.Context, fs *flag.FlagSet) { |
| 41 fs.StringVar(&a.coordinator, "host", "", | 44 fs.StringVar(&a.coordinator, "host", "", |
| 42 "The LogDog Coordinator [host][:port].") | 45 "The LogDog Coordinator [host][:port].") |
| 43 fs.BoolVar(&a.insecure, "insecure", false, | 46 fs.BoolVar(&a.insecure, "insecure", false, |
| 44 "Use insecure transport for RPC.") | 47 "Use insecure transport for RPC.") |
| 48 fs.Var(&a.project, "project", |
| 49 "The log stream's project.") |
| 45 } | 50 } |
| 46 | 51 |
| 47 func (a *application) validate() error { | 52 func (a *application) validate() error { |
| 48 if a.coordinator == "" { | 53 if a.coordinator == "" { |
| 49 return errors.New("main: missing coordinator URL (-url)") | 54 return errors.New("main: missing coordinator URL (-url)") |
| 50 } | 55 } |
| 56 // TODO(dnj): Error on empty project once that's disallowed. |
| 57 if a.project != "" { |
| 58 if err := a.project.Validate(); err != nil { |
| 59 return fmt.Errorf("main: invalid project name (-project)
: %s", err) |
| 60 } |
| 61 } |
| 51 return nil | 62 return nil |
| 52 } | 63 } |
| 53 | 64 |
| 54 func mainImpl() int { | 65 func mainImpl() int { |
| 55 ctx := context.Background() | 66 ctx := context.Background() |
| 56 ctx = gologger.StdConfig.Use(ctx) | 67 ctx = gologger.StdConfig.Use(ctx) |
| 57 | 68 |
| 58 authOptions := auth.Options{ | 69 authOptions := auth.Options{ |
| 59 Scopes: coordinator.Scopes, | 70 Scopes: coordinator.Scopes, |
| 60 } | 71 } |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 121 signal.Stop(signalC) | 132 signal.Stop(signalC) |
| 122 close(signalC) | 133 close(signalC) |
| 123 }() | 134 }() |
| 124 | 135 |
| 125 // Instantiate our authenticated HTTP client. | 136 // Instantiate our authenticated HTTP client. |
| 126 authOpts, err := a.authFlags.Options() | 137 authOpts, err := a.authFlags.Options() |
| 127 if err != nil { | 138 if err != nil { |
| 128 log.Errorf(log.SetError(ctx, err), "Failed to create auth option
s.") | 139 log.Errorf(log.SetError(ctx, err), "Failed to create auth option
s.") |
| 129 return 1 | 140 return 1 |
| 130 } | 141 } |
| 131 » httpClient, err := auth.NewAuthenticator(ctx, auth.SilentLogin, authOpts
).Client() | 142 » httpClient, err := auth.NewAuthenticator(ctx, auth.OptionalLogin, authOp
ts).Client() |
| 132 if err != nil { | 143 if err != nil { |
| 133 log.Errorf(log.SetError(ctx, err), "Failed to create authenticat
ed client.") | 144 log.Errorf(log.SetError(ctx, err), "Failed to create authenticat
ed client.") |
| 134 return 1 | 145 return 1 |
| 135 } | 146 } |
| 136 | 147 |
| 137 // Get our Coordinator client instance. | 148 // Get our Coordinator client instance. |
| 138 prpcClient := &prpc.Client{ | 149 prpcClient := &prpc.Client{ |
| 139 C: httpClient, | 150 C: httpClient, |
| 140 Host: a.coordinator, | 151 Host: a.coordinator, |
| 141 Options: prpc.DefaultOptions(), | 152 Options: prpc.DefaultOptions(), |
| 142 } | 153 } |
| 143 prpcClient.Options.Insecure = a.insecure | 154 prpcClient.Options.Insecure = a.insecure |
| 144 | 155 |
| 145 » a.coord = coordinator.NewClient(prpcClient) | 156 » a.coord = coordinator.NewClient(prpcClient, a.project) |
| 146 a.Context = ctx | 157 a.Context = ctx |
| 147 return subcommands.Run(&a, flags.Args()) | 158 return subcommands.Run(&a, flags.Args()) |
| 148 } | 159 } |
| 149 | 160 |
| 150 func main() { | 161 func main() { |
| 151 os.Exit(mainImpl()) | 162 os.Exit(mainImpl()) |
| 152 } | 163 } |
| OLD | NEW |