github.com/argoproj/argo-cd/v3@v3.2.1/hack/gen-resources/util/gen_options_parser.go (about)

     1  package util
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"gopkg.in/yaml.v2"
     8  )
     9  
    10  type SourceOpts struct {
    11  	Strategy string `yaml:"strategy"`
    12  }
    13  
    14  type DestinationOpts struct {
    15  	Strategy string `yaml:"strategy"`
    16  }
    17  
    18  type ApplicationOpts struct {
    19  	Samples         int             `yaml:"samples"`
    20  	SourceOpts      SourceOpts      `yaml:"source"`
    21  	DestinationOpts DestinationOpts `yaml:"destination"`
    22  }
    23  
    24  type RepositoryOpts struct {
    25  	Samples int `yaml:"samples"`
    26  }
    27  
    28  type ProjectOpts struct {
    29  	Samples int `yaml:"samples"`
    30  }
    31  
    32  type ClusterOpts struct {
    33  	Samples              int    `yaml:"samples"`
    34  	NamespacePrefix      string `yaml:"namespacePrefix"`
    35  	ValuesFilePath       string `yaml:"valuesFilePath"`
    36  	DestinationNamespace string `yaml:"destinationNamespace"`
    37  	ClusterNamePrefix    string `yaml:"clusterNamePrefix"`
    38  	Concurrency          int    `yaml:"parallel"`
    39  }
    40  
    41  type GenerateOpts struct {
    42  	ApplicationOpts ApplicationOpts `yaml:"application"`
    43  	ClusterOpts     ClusterOpts     `yaml:"cluster"`
    44  	RepositoryOpts  RepositoryOpts  `yaml:"repository"`
    45  	ProjectOpts     ProjectOpts     `yaml:"project"`
    46  	GithubToken     string
    47  	Namespace       string `yaml:"namespace"`
    48  }
    49  
    50  func setDefaults(opts *GenerateOpts) {
    51  	if opts.ClusterOpts.Concurrency == 0 {
    52  		opts.ClusterOpts.Concurrency = 2
    53  	}
    54  }
    55  
    56  func Parse(opts *GenerateOpts, file string) error {
    57  	fp, err := os.ReadFile(file)
    58  	if err != nil {
    59  		return fmt.Errorf("error reading the template file: %s : %w", file, err)
    60  	}
    61  
    62  	if e := yaml.Unmarshal(fp, &opts); e != nil {
    63  		return e
    64  	}
    65  
    66  	setDefaults(opts)
    67  
    68  	return nil
    69  }