go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/buildbucket/cli/status.go (about)

     1  // Copyright 2019 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package cli
    16  
    17  import (
    18  	"flag"
    19  	"fmt"
    20  	"sort"
    21  	"strings"
    22  
    23  	pb "go.chromium.org/luci/buildbucket/proto"
    24  )
    25  
    26  var statusFlagValues = map[string]pb.Status{
    27  	"scheduled":     pb.Status_SCHEDULED,
    28  	"started":       pb.Status_STARTED,
    29  	"ended":         pb.Status_ENDED_MASK,
    30  	"success":       pb.Status_SUCCESS,
    31  	"failure":       pb.Status_FAILURE,
    32  	"infra_failure": pb.Status_INFRA_FAILURE,
    33  	"canceled":      pb.Status_CANCELED,
    34  }
    35  
    36  var statusFlagNames map[pb.Status]string
    37  var statusFlagValuesName []string
    38  
    39  func init() {
    40  	statusFlagNames = make(map[pb.Status]string, len(statusFlagValues))
    41  	statusFlagValuesName = make([]string, 0, len(statusFlagValues))
    42  	for name, status := range statusFlagValues {
    43  		statusFlagValuesName = append(statusFlagValuesName, name)
    44  		statusFlagNames[status] = name
    45  	}
    46  	sort.Strings(statusFlagValuesName)
    47  }
    48  
    49  type statusFlag struct {
    50  	status *pb.Status
    51  }
    52  
    53  // StatusFlag returns a flag.Getter which reads a flag value into status.
    54  // Valid flag values: scheduled, started, ended, success, failure, infra_failure,
    55  // canceled.
    56  // Panics if status is nil.
    57  func StatusFlag(status *pb.Status) flag.Getter {
    58  	if status == nil {
    59  		panic("status is nil")
    60  	}
    61  	return &statusFlag{status}
    62  }
    63  
    64  func (f *statusFlag) String() string {
    65  	// https://godoc.org/flag#Value says that String() may be called with a
    66  	// zero-valued receiver.
    67  	if f == nil || f.status == nil {
    68  		return ""
    69  	}
    70  	return statusFlagNames[*f.status]
    71  }
    72  
    73  func (f *statusFlag) Get() any {
    74  	return *f.status
    75  }
    76  
    77  func (f *statusFlag) Set(s string) error {
    78  	st, ok := statusFlagValues[strings.ToLower(s)]
    79  	if !ok {
    80  		return fmt.Errorf("invalid status %q; expected one of %s", s, strings.Join(statusFlagValuesName, ", "))
    81  	}
    82  
    83  	*f.status = pb.Status(st)
    84  	return nil
    85  }