go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/cl-util/common.go (about) 1 // Copyright 2020 The Fuchsia 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 "context" 9 "fmt" 10 "net/http" 11 12 "github.com/maruel/subcommands" 13 14 "go.chromium.org/luci/auth" 15 "go.chromium.org/luci/auth/client/authcli" 16 "go.chromium.org/luci/common/logging" 17 ) 18 19 type commonFlags struct { 20 subcommands.CommandRunBase 21 gerritHost string 22 gerritProject string 23 logLevel logging.Level 24 authFlags authcli.Flags 25 26 parsedAuthOpts auth.Options 27 } 28 29 func (c *commonFlags) Init(authOpts auth.Options) { 30 c.authFlags = authcli.Flags{} 31 c.authFlags.Register(&c.Flags, authOpts) 32 c.Flags.StringVar(&c.gerritHost, "gerrit-host", "", "Gerrit host to use.") 33 c.Flags.StringVar(&c.gerritProject, "gerrit-project", "", "Gerrit project to use.") 34 c.Flags.Var(&c.logLevel, "log-level", "Logging level. Can be debug, info, warning, or error.") 35 } 36 37 func (c *commonFlags) Parse() error { 38 var err error 39 c.parsedAuthOpts, err = c.authFlags.Options() 40 if err != nil { 41 return err 42 } 43 if c.gerritHost == "" { 44 return fmt.Errorf("-gerrit-host is required") 45 } 46 if c.gerritProject == "" { 47 return fmt.Errorf("-gerrit-project is required") 48 } 49 return nil 50 } 51 52 // newAuthClient returns an authenticated *http.Client. 53 func newAuthClient(ctx context.Context, authOpts auth.Options) (*http.Client, error) { 54 return auth.NewAuthenticator(ctx, auth.OptionalLogin, authOpts).Client() 55 }