k8s.io/test-infra@v0.0.0-20240520184403-27c6b4c223d8/testgrid/pkg/configurator/options/options.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package options
    18  
    19  import (
    20  	"errors"
    21  	"flag"
    22  	"strings"
    23  
    24  	"sigs.k8s.io/prow/pkg/flagutil"
    25  	configflagutil "sigs.k8s.io/prow/pkg/flagutil/config"
    26  
    27  	"github.com/sirupsen/logrus"
    28  )
    29  
    30  type MultiString []string
    31  
    32  func (m MultiString) String() string {
    33  	return strings.Join(m, ",")
    34  }
    35  
    36  func (m *MultiString) Set(v string) error {
    37  	*m = strings.Split(v, ",")
    38  	return nil
    39  }
    40  
    41  type Options struct {
    42  	Creds              string
    43  	Inputs             MultiString
    44  	Oneshot            bool
    45  	Output             flagutil.Strings
    46  	PrintText          bool
    47  	ValidateConfigFile bool
    48  	WorldReadable      bool
    49  	WriteYAML          bool
    50  	ProwConfig         configflagutil.ConfigOptions
    51  	DefaultYAML        string
    52  	UpdateDescription  bool
    53  	ProwJobURLPrefix   string
    54  	StrictUnmarshal    bool
    55  }
    56  
    57  func (o *Options) GatherOptions(fs *flag.FlagSet, args []string) error {
    58  	fs.StringVar(&o.Creds, "gcp-service-account", "", "/path/to/gcp/creds (use local creds if empty)")
    59  	fs.BoolVar(&o.Oneshot, "oneshot", false, "Write proto once and exit instead of monitoring --yaml files for changes")
    60  	fs.Var(&o.Output, "output", "write proto to gs://bucket/obj or /local/path")
    61  	fs.BoolVar(&o.PrintText, "print-text", false, "print generated info in text format to stdout")
    62  	fs.BoolVar(&o.ValidateConfigFile, "validate-config-file", false, "validate that the given config files are syntactically correct and exit (proto is not written anywhere)")
    63  	fs.BoolVar(&o.WorldReadable, "world-readable", false, "when uploading the proto to GCS, makes it world readable. Has no effect on writing to the local filesystem.")
    64  	fs.BoolVar(&o.WriteYAML, "output-yaml", false, "Output to TestGrid YAML instead of config proto")
    65  	fs.Var(&o.Inputs, "yaml", "comma-separated list of input YAML files or directories")
    66  	o.ProwConfig.ConfigPathFlagName = "prow-config"
    67  	o.ProwConfig.JobConfigPathFlagName = "prow-job-config"
    68  	o.ProwConfig.AddFlags(fs)
    69  	fs.StringVar(&o.DefaultYAML, "default", "", "path to default settings; required for proto outputs")
    70  	fs.BoolVar(&o.UpdateDescription, "update-description", false, "add prowjob info to description even if non-empty")
    71  	fs.StringVar(&o.ProwJobURLPrefix, "prowjob-url-prefix", "", "for prowjob_config_url in descriptions: {prowjob-url-prefix}/{prowjob.sourcepath}")
    72  	fs.BoolVar(&o.StrictUnmarshal, "strict-unmarshal", false, "whether or not we want to be strict when unmarshalling configs")
    73  
    74  	if err := fs.Parse(args); err != nil {
    75  		return err
    76  	}
    77  
    78  	if len(o.Inputs) == 0 || o.Inputs[0] == "" {
    79  		return errors.New("--yaml must include at least one file")
    80  	}
    81  
    82  	if !o.PrintText && !o.ValidateConfigFile && len(o.Output.Strings()) == 0 {
    83  		return errors.New("--print-text, --validate-config-file, or --output required")
    84  	}
    85  	if o.ValidateConfigFile && len(o.Output.Strings()) > 0 {
    86  		return errors.New("--validate-config-file doesn't write the proto anywhere")
    87  	}
    88  	if err := o.ProwConfig.ValidateConfigOptional(); err != nil {
    89  		return err
    90  	}
    91  	if o.DefaultYAML == "" && !o.WriteYAML {
    92  		logrus.Warnf("--default not explicitly specified; assuming %s", o.Inputs[0])
    93  		o.DefaultYAML = o.Inputs[0]
    94  	}
    95  	return nil
    96  }