go.fuchsia.dev/infra@v0.0.0-20240507153436-9b593402251b/buildbucket/builder.go (about)

     1  // Copyright 2019 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 buildbucket
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"strings"
    11  
    12  	buildbucketpb "go.chromium.org/luci/buildbucket/proto"
    13  )
    14  
    15  // BuilderIDFlag identifies a Builder. This is a convenience type for reading a builder id
    16  // from command line flags.
    17  type BuilderIDFlag struct {
    18  	project string
    19  	bucket  string
    20  	builder string
    21  }
    22  
    23  func (b *BuilderIDFlag) String() string {
    24  	return fmt.Sprintf("%s/%s/%s", b.project, b.bucket, b.builder)
    25  }
    26  
    27  // Set implements flag.Value
    28  func (b *BuilderIDFlag) Set(input string) error {
    29  	parts := strings.SplitN(input, "/", 3)
    30  	if len(parts) != 3 {
    31  		return fmt.Errorf("invalid builder: %q must have form 'project/bucket/builder'", input)
    32  	}
    33  
    34  	b.project = parts[0]
    35  	b.bucket = parts[1]
    36  	b.builder = parts[2]
    37  	return nil
    38  }
    39  
    40  // Get returns the parsed flag value. The output can be cast as a buildbucketpb.BuilderID.
    41  func (b BuilderIDFlag) Get() any {
    42  	return &buildbucketpb.BuilderID{
    43  		Project: b.project,
    44  		Bucket:  b.bucket,
    45  		Builder: b.builder,
    46  	}
    47  }
    48  
    49  // BuilderID returns a flag.Value for reading a builder ID from a string.  The format of
    50  // the input is project/bucket/build.
    51  func BuilderID() flag.Getter {
    52  	return &BuilderIDFlag{}
    53  }