github.com/yrj2011/jx-test-infra@v0.0.0-20190529031832-7a2065ee98eb/prow/gcsupload/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 gcsupload
    18  
    19  import (
    20  	"encoding/json"
    21  	"errors"
    22  	"flag"
    23  	"fmt"
    24  
    25  	"k8s.io/test-infra/prow/kube"
    26  	"k8s.io/test-infra/testgrid/util/gcs"
    27  )
    28  
    29  // NewOptions returns an empty Options with no nil fields
    30  func NewOptions() *Options {
    31  	return &Options{
    32  		GCSConfiguration: &kube.GCSConfiguration{},
    33  	}
    34  }
    35  
    36  // Options exposes the configuration necessary
    37  // for defining where in GCS an upload will land.
    38  type Options struct {
    39  	// Items are files or directories to upload
    40  	Items []string `json:"items,omitempty"`
    41  
    42  	// SubDir is appended to the GCS path
    43  	SubDir string `json:"sub_dir,omitempty"`
    44  
    45  	*kube.GCSConfiguration
    46  
    47  	// GcsCredentialsFile is the path to the JSON
    48  	// credentials for pushing to GCS
    49  	GcsCredentialsFile string `json:"gcs_credentials_file,omitempty"`
    50  	DryRun             bool   `json:"dry_run"`
    51  
    52  	// gcsPath is used to store human-provided GCS
    53  	// paths that are parsed to get more granular
    54  	// fields
    55  	gcsPath gcs.Path
    56  }
    57  
    58  // Validate ensures that the set of options are
    59  // self-consistent and valid
    60  func (o *Options) Validate() error {
    61  	if o.gcsPath.String() != "" {
    62  		o.Bucket = o.gcsPath.Bucket()
    63  		o.PathPrefix = o.gcsPath.Object()
    64  	}
    65  
    66  	if !o.DryRun {
    67  		if o.Bucket == "" {
    68  			return errors.New("GCS upload was requested no GCS bucket was provided")
    69  		}
    70  
    71  		if o.GcsCredentialsFile == "" {
    72  			return errors.New("GCS upload was requested but no GCS credentials file was provided")
    73  		}
    74  	}
    75  
    76  	if o.PathStrategy != kube.PathStrategyLegacy && o.PathStrategy != kube.PathStrategyExplicit && o.PathStrategy != kube.PathStrategySingle {
    77  		return fmt.Errorf("GCS path strategy must be one of %q, %q, or %q", kube.PathStrategyLegacy, kube.PathStrategyExplicit, kube.PathStrategySingle)
    78  	}
    79  
    80  	if o.PathStrategy != kube.PathStrategyExplicit && (o.DefaultOrg == "" || o.DefaultRepo == "") {
    81  		return fmt.Errorf("default org and repo must be provided for GCS strategy %q", o.PathStrategy)
    82  	}
    83  
    84  	return nil
    85  }
    86  
    87  // ConfigVar exposes the environment variable used
    88  // to store serialized configuration
    89  func (o *Options) ConfigVar() string {
    90  	return JSONConfigEnvVar
    91  }
    92  
    93  // LoadConfig loads options from serialized config
    94  func (o *Options) LoadConfig(config string) error {
    95  	return json.Unmarshal([]byte(config), o)
    96  }
    97  
    98  // BindOptions binds flags to options
    99  func (o *Options) BindOptions(flags *flag.FlagSet) {
   100  	BindOptions(o, flags)
   101  }
   102  
   103  // Complete internalizes command line arguments
   104  func (o *Options) Complete(args []string) {
   105  	o.Items = args
   106  }
   107  
   108  // BindOptions adds flags to the FlagSet that populate
   109  // the GCS upload options struct given.
   110  func BindOptions(options *Options, fs *flag.FlagSet) {
   111  	fs.StringVar(&options.SubDir, "sub-dir", "", "Optional sub-directory of the job's path to which artifacts are uploaded")
   112  
   113  	fs.StringVar(&options.PathStrategy, "path-strategy", kube.PathStrategyExplicit, "how to encode org and repo into GCS paths")
   114  	fs.StringVar(&options.DefaultOrg, "default-org", "", "optional default org for GCS path encoding")
   115  	fs.StringVar(&options.DefaultRepo, "default-repo", "", "optional default repo for GCS path encoding")
   116  
   117  	fs.Var(&options.gcsPath, "gcs-path", "GCS path to upload into")
   118  	fs.StringVar(&options.GcsCredentialsFile, "gcs-credentials-file", "", "file where Google Cloud authentication credentials are stored")
   119  	fs.BoolVar(&options.DryRun, "dry-run", true, "do not interact with GCS")
   120  }
   121  
   122  const (
   123  	// JSONConfigEnvVar is the environment variable that
   124  	// utilities expect to find a full JSON configuration
   125  	// in when run.
   126  	JSONConfigEnvVar = "GCSUPLOAD_OPTIONS"
   127  )
   128  
   129  // Encode will encode the set of options in the format that
   130  // is expected for the configuration environment variable
   131  func Encode(options Options) (string, error) {
   132  	encoded, err := json.Marshal(options)
   133  	return string(encoded), err
   134  }