sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/initupload/options.go (about)

     1  /*
     2  Copyright 2018 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 initupload
    18  
    19  import (
    20  	"encoding/json"
    21  	"flag"
    22  
    23  	"sigs.k8s.io/prow/pkg/gcsupload"
    24  )
    25  
    26  const (
    27  	// JSONConfigEnvVar is the environment variable where utilities expect to find a full JSON
    28  	// configuration.
    29  	JSONConfigEnvVar = "INITUPLOAD_OPTIONS"
    30  )
    31  
    32  // NewOptions returns an empty Options with no nil fields.
    33  func NewOptions() *Options {
    34  	return &Options{
    35  		Options: gcsupload.NewOptions(),
    36  	}
    37  }
    38  
    39  // Options holds the GCS options and the log file of clone records.
    40  type Options struct {
    41  	*gcsupload.Options
    42  
    43  	// Log is the log file to which clone records are written. If unspecified, no clone records
    44  	// are uploaded.
    45  	Log string `json:"log,omitempty"`
    46  }
    47  
    48  // ConfigVar exposes the environment variable used to store serialized configuration.
    49  func (o *Options) ConfigVar() string {
    50  	return JSONConfigEnvVar
    51  }
    52  
    53  // LoadConfig loads options from serialized config.
    54  func (o *Options) LoadConfig(config string) error {
    55  	return json.Unmarshal([]byte(config), o)
    56  }
    57  
    58  // AddFlags binds flags to options.
    59  func (o *Options) AddFlags(flags *flag.FlagSet) {
    60  	flags.StringVar(&o.Log, "clone-log", "", "Path to the clone records log")
    61  	o.Options.AddFlags(flags)
    62  }
    63  
    64  // Complete internalizes command line arguments.
    65  func (o *Options) Complete(args []string) {
    66  	o.Options.Complete(args)
    67  }
    68  
    69  // Encode will encode the set of options in the format that is expected for the configuration
    70  // environment variable.
    71  func Encode(options Options) (string, error) {
    72  	encoded, err := json.Marshal(options)
    73  	return string(encoded), err
    74  }