| // Copyright 2020 The Fuchsia Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| package main |
| |
| import ( |
| "context" |
| "fmt" |
| "net/http" |
| |
| "github.com/maruel/subcommands" |
| |
| "go.chromium.org/luci/auth" |
| "go.chromium.org/luci/auth/client/authcli" |
| ) |
| |
| type commonFlags struct { |
| subcommands.CommandRunBase |
| gerritHost string |
| gerritProject string |
| authFlags authcli.Flags |
| |
| parsedAuthOpts auth.Options |
| } |
| |
| func (c *commonFlags) Init(authOpts auth.Options) { |
| c.authFlags = authcli.Flags{} |
| c.authFlags.Register(&c.Flags, authOpts) |
| c.Flags.StringVar(&c.gerritHost, "gerrit-host", "", "Gerrit host to use.") |
| c.Flags.StringVar(&c.gerritProject, "gerrit-project", "", "Gerrit project to use.") |
| } |
| |
| func (c *commonFlags) Parse() error { |
| var err error |
| c.parsedAuthOpts, err = c.authFlags.Options() |
| if err != nil { |
| return err |
| } |
| if c.gerritHost == "" { |
| return fmt.Errorf("-gerrit-host is required") |
| } |
| if c.gerritProject == "" { |
| return fmt.Errorf("-gerrit-project is required") |
| } |
| return nil |
| } |
| |
| // newAuthClient returns an authenticated *http.Client. |
| func newAuthClient(ctx context.Context, authOpts auth.Options) (*http.Client, error) { |
| return auth.NewAuthenticator(ctx, auth.OptionalLogin, authOpts).Client() |
| } |