Skip to content

Commit

Permalink
chore: update behavior to use selectOne whenever any envs are returned
Browse files Browse the repository at this point in the history
  • Loading branch information
bvtujo committed Jul 26, 2023
1 parent 68f8e97 commit e494f0c
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
23 changes: 8 additions & 15 deletions internal/pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -570,38 +570,31 @@ func (o *initOpts) askEnvName() error {
return fmt.Errorf("list environments: %w", err)
}
// Configure prompt options based on number of environments.
var promptOptEnvDefaultName prompt.PromptConfig
switch len(envs) {
case 0:
promptOptEnvDefaultName = prompt.WithDefaultInput(defaultEnvironmentName)
case 1:
promptOptEnvDefaultName = prompt.WithDefaultInput(envs[0].Name)
default:
log.Infoln("It looks like you have more than one environment in this application.")
promptOpts := []prompt.PromptConfig{prompt.WithFinalMessage("Environment name:")}
if len(envs) == 0 {
promptOpts = append(promptOpts, prompt.WithDefaultInput(defaultEnvironmentName))
} else {
log.Infoln("It looks like you have existing environments in this application.")
// Set up selection options.
options := make([]string, len(envs))
for e := range envs {
options[e] = envs[e].Name
}
options = append(options, envPromptCreateNew)
// Select one
// Select one of existing envs or create a new one.
v, err := o.prompt.SelectOne(initExistingEnvSelectPrompt, initExistingEnvSelectHelp, options)
if err != nil {
return fmt.Errorf("select environment: %w", err)
}
// Customer has selected an existing environment.
// Customer has selected an existing environment. Return early.
if v != envPromptCreateNew {
if initEnvCmd, ok := o.initEnvCmd.(*initEnvOpts); ok {
initEnvCmd.name = v
}
return nil
}
}
// Optionally include a default environment name.
promptOpts := []prompt.PromptConfig{prompt.WithFinalMessage("Environment name:")}
if promptOptEnvDefaultName != nil {
promptOpts = append(promptOpts, promptOptEnvDefaultName)
}

v, err := o.prompt.Get(envInitNamePrompt, envInitNameHelpPrompt, validateEnvironmentName, promptOpts...)
if err != nil {
return fmt.Errorf("get environment name: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/cli/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func TestInitOpts_Run(t *testing.T) {
App: "",
Name: "test2",
}}, nil)
opts.prompt.(*climocks.Mockprompter).EXPECT().Get(envInitNamePrompt, envInitNameHelpPrompt, gomock.Any(), gomock.Any(), gomock.Any()).Return("test2", nil)
opts.prompt.(*climocks.Mockprompter).EXPECT().SelectOne(initExistingEnvSelectPrompt, initExistingEnvSelectHelp, []string{"test2", envPromptCreateNew}).Return("test2", nil)
opts.initEnvCmd.(*climocks.MockactionCommand).EXPECT().Execute().Return(nil)
opts.deployEnvCmd.(*climocks.Mockcmd).EXPECT().Execute().Return(nil)
opts.deploySvcCmd.(*climocks.MockactionCommand).EXPECT().Ask().Return(nil)
Expand Down

0 comments on commit e494f0c

Please sign in to comment.