diff --git a/cmd/ocm/gcp/create-wif-config.go b/cmd/ocm/gcp/create-wif-config.go index c01490f0..2e951cd5 100644 --- a/cmd/ocm/gcp/create-wif-config.go +++ b/cmd/ocm/gcp/create-wif-config.go @@ -6,6 +6,8 @@ import ( "log" "strconv" + "github.com/AlecAivazis/survey/v2" + "github.com/openshift-online/ocm-cli/pkg/arguments" "github.com/openshift-online/ocm-cli/pkg/gcp" "github.com/openshift-online/ocm-cli/pkg/ocm" cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1" @@ -39,12 +41,15 @@ func NewCreateWorkloadIdentityConfiguration() *cobra.Command { RunE: createWorkloadIdentityConfigurationCmd, } + arguments.AddInteractiveFlag( + createWifConfigCmd.PersistentFlags(), + &CreateWifConfigOpts.Interactive, + ) + createWifConfigCmd.PersistentFlags().StringVar(&CreateWifConfigOpts.Name, "name", "", "User-defined name for all created Google cloud resources") - createWifConfigCmd.MarkPersistentFlagRequired("name") createWifConfigCmd.PersistentFlags().StringVar(&CreateWifConfigOpts.Project, "project", "", "ID of the Google cloud project") - createWifConfigCmd.MarkPersistentFlagRequired("project") createWifConfigCmd.PersistentFlags().StringVar(&CreateWifConfigOpts.RolePrefix, "role-prefix", "", "Prefix for naming custom roles") @@ -64,11 +69,11 @@ func NewCreateWorkloadIdentityConfiguration() *cobra.Command { } func validationForCreateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { - if CreateWifConfigOpts.Name == "" { - return fmt.Errorf("Name is required") + if err := promptWifDisplayName(); err != nil { + return err } - if CreateWifConfigOpts.Project == "" { - return fmt.Errorf("Project is required") + if err := promptProjectId(); err != nil { + return err } if CreateWifConfigOpts.Mode != ModeAuto && CreateWifConfigOpts.Mode != ModeManual { @@ -83,6 +88,44 @@ func validationForCreateWorkloadIdentityConfigurationCmd(cmd *cobra.Command, arg return nil } +func promptWifDisplayName() error { + const wifNameHelp = "The name can be used as the identifier of the WifConfig resource." + if CreateWifConfigOpts.Name == "" { + if CreateWifConfigOpts.Interactive { + prompt := &survey.Input{ + Message: "WifConfig name", + Help: wifNameHelp, + } + return survey.AskOne( + prompt, + &CreateWifConfigOpts.Name, + survey.WithValidator(survey.Required), + ) + } + return fmt.Errorf("flag 'name' is required") + } + return nil +} + +func promptProjectId() error { + const projectIdHelp = "The GCP Project Id that will be used by the WifConfig" + if CreateWifConfigOpts.Project == "" { + if CreateWifConfigOpts.Interactive { + prompt := &survey.Input{ + Message: "Gcp Project ID", + Help: projectIdHelp, + } + return survey.AskOne( + prompt, + &CreateWifConfigOpts.Project, + survey.WithValidator(survey.Required), + ) + } + return fmt.Errorf("Flag 'project' is required") + } + return nil +} + func createWorkloadIdentityConfigurationCmd(cmd *cobra.Command, argv []string) error { ctx := context.Background() log := log.Default() diff --git a/cmd/ocm/gcp/gcp.go b/cmd/ocm/gcp/gcp.go index 26ebfe8e..069c229d 100644 --- a/cmd/ocm/gcp/gcp.go +++ b/cmd/ocm/gcp/gcp.go @@ -5,14 +5,15 @@ import ( ) type options struct { - TargetDir string - Region string + Interactive bool + Mode string Name string Project string + Region string RolePrefix string + TargetDir string WorkloadIdentityPool string WorkloadIdentityProvider string - Mode string } // NewGcpCmd implements the "gcp" subcommand for the credentials provisioning