...

Source file src/edge-infra.dev/pkg/f8n/warehouse/promote/config.go

Documentation: edge-infra.dev/pkg/f8n/warehouse/promote

     1  package promote
     2  
     3  import (
     4  	"errors"
     5  	"flag"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/peterbourgon/ff/v3"
    10  )
    11  
    12  const (
    13  	WarehouseRepo        = "warehouse"
    14  	platformInfraProject = "ret-edge-pltf-infra"
    15  	defaultGCPLocation   = "us-east1"
    16  )
    17  
    18  var (
    19  	errMissingConfig = errors.New("missing configuration, run -help for more information")
    20  )
    21  
    22  type Config struct {
    23  	LockFile    string
    24  	AllPackages bool
    25  
    26  	sourceProjectID string
    27  	sourceRepoName  string
    28  	SourceRepo      Repository
    29  
    30  	destinationProjectID string
    31  	destinationRepoName  string
    32  	DestinationRepo      Repository
    33  
    34  	ForwarderProjectID  string
    35  	sourceLocation      string
    36  	destinationLocation string
    37  }
    38  
    39  func (cfg Config) String() string {
    40  	return fmt.Sprintf(
    41  		"{lockfile: %s source-repo: %s, destination-repo: %s, forwarder-project: %s}",
    42  		cfg.LockFile, cfg.SourceRepo, cfg.DestinationRepo, cfg.ForwarderProjectID,
    43  	)
    44  }
    45  
    46  func (cfg Config) Description() string {
    47  	return fmt.Sprintf(
    48  		"promoting all packages from source repo '%s' to destination repo '%s' using lockfile '%s'",
    49  		cfg.SourceRepo,
    50  		cfg.DestinationRepo,
    51  		cfg.LockFile,
    52  	)
    53  }
    54  
    55  func (cfg Config) Validate() error {
    56  	if cfg.sourceProjectID == "" || cfg.sourceRepoName == "" ||
    57  		cfg.destinationProjectID == "" || cfg.destinationRepoName == "" ||
    58  		cfg.ForwarderProjectID == "" || cfg.sourceLocation == "" || cfg.destinationLocation == "" ||
    59  		cfg.LockFile == "" {
    60  		return errMissingConfig
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  func newConfig() (*Config, error) {
    67  	cfg := &Config{}
    68  	fs := flag.NewFlagSet(PromoteTag, flag.ContinueOnError)
    69  
    70  	fs.StringVar(&cfg.LockFile, "lockfile", "", "the path of the lockfile to use as the package source")
    71  	fs.StringVar(&cfg.sourceProjectID, "src-project", "", "GCP project ID that contains the source repository (e.g. \"ret-edge-stage1-foreman\")")
    72  	fs.StringVar(&cfg.sourceRepoName, "src-repo", WarehouseRepo, "source GAR repository instance to promote from")
    73  	fs.StringVar(&cfg.destinationProjectID, "dest-project", "", "GCP project ID that contains the destination repository (e.g. \"ret-edge-stage2-foreman\")")
    74  	fs.StringVar(&cfg.destinationRepoName, "dest-repo", WarehouseRepo, "destination GAR repository instance to promote to")
    75  	fs.StringVar(&cfg.ForwarderProjectID, "fwder-project", platformInfraProject, "GCP project ID that contains the registry forwarder")
    76  	fs.StringVar(&cfg.sourceLocation, "src-location", defaultGCPLocation, "source GCP location / region")
    77  	fs.StringVar(&cfg.destinationLocation, "dest-location", defaultGCPLocation, "destination GCP location / region")
    78  
    79  	if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarNoPrefix()); err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	if err := cfg.Validate(); err != nil {
    84  		return nil, err
    85  	}
    86  
    87  	cfg.SourceRepo = newRepository(cfg.sourceRepoName, cfg.sourceProjectID, cfg.sourceLocation)
    88  	cfg.DestinationRepo = newRepository(cfg.destinationRepoName, cfg.destinationProjectID, cfg.destinationLocation)
    89  
    90  	return cfg, nil
    91  }
    92  

View as plain text