package promote import ( "errors" "flag" "fmt" "os" "github.com/peterbourgon/ff/v3" ) const ( WarehouseRepo = "warehouse" platformInfraProject = "ret-edge-pltf-infra" defaultGCPLocation = "us-east1" ) var ( errMissingConfig = errors.New("missing configuration, run -help for more information") ) type Config struct { LockFile string AllPackages bool sourceProjectID string sourceRepoName string SourceRepo Repository destinationProjectID string destinationRepoName string DestinationRepo Repository ForwarderProjectID string sourceLocation string destinationLocation string } func (cfg Config) String() string { return fmt.Sprintf( "{lockfile: %s source-repo: %s, destination-repo: %s, forwarder-project: %s}", cfg.LockFile, cfg.SourceRepo, cfg.DestinationRepo, cfg.ForwarderProjectID, ) } func (cfg Config) Description() string { return fmt.Sprintf( "promoting all packages from source repo '%s' to destination repo '%s' using lockfile '%s'", cfg.SourceRepo, cfg.DestinationRepo, cfg.LockFile, ) } func (cfg Config) Validate() error { if cfg.sourceProjectID == "" || cfg.sourceRepoName == "" || cfg.destinationProjectID == "" || cfg.destinationRepoName == "" || cfg.ForwarderProjectID == "" || cfg.sourceLocation == "" || cfg.destinationLocation == "" || cfg.LockFile == "" { return errMissingConfig } return nil } func newConfig() (*Config, error) { cfg := &Config{} fs := flag.NewFlagSet(PromoteTag, flag.ContinueOnError) fs.StringVar(&cfg.LockFile, "lockfile", "", "the path of the lockfile to use as the package source") fs.StringVar(&cfg.sourceProjectID, "src-project", "", "GCP project ID that contains the source repository (e.g. \"ret-edge-stage1-foreman\")") fs.StringVar(&cfg.sourceRepoName, "src-repo", WarehouseRepo, "source GAR repository instance to promote from") fs.StringVar(&cfg.destinationProjectID, "dest-project", "", "GCP project ID that contains the destination repository (e.g. \"ret-edge-stage2-foreman\")") fs.StringVar(&cfg.destinationRepoName, "dest-repo", WarehouseRepo, "destination GAR repository instance to promote to") fs.StringVar(&cfg.ForwarderProjectID, "fwder-project", platformInfraProject, "GCP project ID that contains the registry forwarder") fs.StringVar(&cfg.sourceLocation, "src-location", defaultGCPLocation, "source GCP location / region") fs.StringVar(&cfg.destinationLocation, "dest-location", defaultGCPLocation, "destination GCP location / region") if err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarNoPrefix()); err != nil { return nil, err } if err := cfg.Validate(); err != nil { return nil, err } cfg.SourceRepo = newRepository(cfg.sourceRepoName, cfg.sourceProjectID, cfg.sourceLocation) cfg.DestinationRepo = newRepository(cfg.destinationRepoName, cfg.destinationProjectID, cfg.destinationLocation) return cfg, nil }