blob: 850e5180d22f0cabcd2b28339480cdde411a3121 [file] [log] [blame]
// Copyright 2019 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 buildbucket
import (
"flag"
"fmt"
"strings"
buildbucketpb "go.chromium.org/luci/buildbucket/proto"
)
// BuilderIDFlag identifies a Builder. This is a convenience type for reading a builder id
// from command line flags.
type BuilderIDFlag struct {
project string
bucket string
builder string
}
func (b *BuilderIDFlag) String() string {
return fmt.Sprintf("%s/%s/%s", b.project, b.bucket, b.builder)
}
// Set implements flag.Value
func (b *BuilderIDFlag) Set(input string) error {
parts := strings.SplitN(input, "/", 3)
if len(parts) != 3 {
return fmt.Errorf("invalid builder: %q must have form 'project/bucket/builder'", input)
}
b.project = parts[0]
b.bucket = parts[1]
b.builder = parts[2]
return nil
}
// Get returns the parsed flag value. The output can be cast as a buildbucketpb.BuilderID.
func (b BuilderIDFlag) Get() any {
return &buildbucketpb.BuilderID{
Project: b.project,
Bucket: b.bucket,
Builder: b.builder,
}
}
// BuilderID returns a flag.Value for reading a builder ID from a string. The format of
// the input is project/bucket/build.
func BuilderID() flag.Getter {
return &BuilderIDFlag{}
}