Skip to content

Commit

Permalink
adding interactive move for WifConfig creation
Browse files Browse the repository at this point in the history
  • Loading branch information
renan-campos committed Oct 16, 2024
1 parent 8b70707 commit 8c66815
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 9 deletions.
55 changes: 49 additions & 6 deletions cmd/ocm/gcp/create-wif-config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")

Expand All @@ -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 {
Expand All @@ -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()
Expand Down
7 changes: 4 additions & 3 deletions cmd/ocm/gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8c66815

Please sign in to comment.