go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/cmd/lkg/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 "fmt" 9 "strings" 10 11 "github.com/maruel/subcommands" 12 13 "go.chromium.org/luci/auth" 14 "go.chromium.org/luci/auth/client/authcli" 15 buildbucketpb "go.chromium.org/luci/buildbucket/proto" 16 ) 17 18 type commonFlags struct { 19 subcommands.CommandRunBase 20 buildbucketHost string 21 gitilesRef string 22 searchRange int 23 authFlags authcli.Flags 24 25 parsedAuthOpts auth.Options 26 } 27 28 func (c *commonFlags) Init(authOpts auth.Options) { 29 c.authFlags = authcli.Flags{} 30 c.authFlags.Register(&c.Flags, authOpts) 31 c.Flags.StringVar(&c.buildbucketHost, "host", "cr-buildbucket.appspot.com", "Buildbucket hostname") 32 c.Flags.StringVar(&c.gitilesRef, "gitiles-ref", "", "Optionally filter for matching gitiles ref e.g. refs/heads/main") 33 c.Flags.IntVar(&c.searchRange, "search-range", 25, "Number of builds to search") 34 } 35 36 func (c *commonFlags) checkFlags() error { 37 var err error 38 c.parsedAuthOpts, err = c.authFlags.Options() 39 if err != nil { 40 return err 41 } 42 if c.buildbucketHost == "" { 43 return fmt.Errorf("-host is required") 44 } 45 return nil 46 } 47 48 // newBuilderID returns a *buildbucketpb.BuilderID for a project/bucket/builder string. 49 func newBuilderID(builder string) (*buildbucketpb.BuilderID, error) { 50 components := strings.Split(builder, "/") 51 if len(components) != 3 { 52 return nil, fmt.Errorf("failed to parse builder %s", builder) 53 } 54 return &buildbucketpb.BuilderID{ 55 Project: components[0], 56 Bucket: components[1], 57 Builder: components[2], 58 }, nil 59 } 60 61 // builders is a flag.Getter implementation representing a []*buildbucketpb.BuilderID. 62 type builders []*buildbucketpb.BuilderID 63 64 // String returns a comma-separated string representation of the builder IDs. 65 func (b builders) String() string { 66 strs := make([]string, len(b)) 67 for i, bid := range b { 68 strs[i] = bid.String() 69 } 70 return strings.Join(strs, ", ") 71 } 72 73 // Set records seeing a flag value. 74 func (b *builders) Set(val string) error { 75 bid, err := newBuilderID(val) 76 if err != nil { 77 return err 78 } 79 *b = append(*b, bid) 80 return nil 81 } 82 83 // Get retrieves the flag value. 84 func (b builders) Get() any { 85 return []*buildbucketpb.BuilderID(b) 86 }