Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions internal/iostreams/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,18 +192,30 @@ func errInteractivityFlags(cfg PromptConfig) error {
// ConfirmPrompt prompts the user for a "yes" or "no" (true or false) value for
// the message
func (io *IOStreams) ConfirmPrompt(ctx context.Context, message string, defaultValue bool) (bool, error) {
if !io.IsTTY() {
return false, errInteractivityFlags(ConfirmPromptConfig{})
}
return confirmForm(io, ctx, message, defaultValue)
}

// InputPrompt prompts the user for a string value for the message, which can
// optionally be made required
func (io *IOStreams) InputPrompt(ctx context.Context, message string, cfg InputPromptConfig) (string, error) {
if !io.IsTTY() {
if cfg.IsRequired() {
return "", errInteractivityFlags(cfg)
}
return "", nil
}
return inputForm(io, ctx, message, cfg)
}

// MultiSelectPrompt prompts the user to select multiple values in a list and
// returns the selected values
func (io *IOStreams) MultiSelectPrompt(ctx context.Context, message string, options []string) ([]string, error) {
if !io.IsTTY() {
return nil, errInteractivityFlags(MultiSelectPromptConfig{})
}
Comment on lines +216 to +218
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔬 note: Here is the implementation of this function, with a default for no flags:

// errInteractivityFlags formats an error for when flag substitutes are needed
func errInteractivityFlags(cfg PromptConfig) error {
flags := cfg.GetFlags()
var remediation string
var helpMessage = "Learn more about this command with `--help`"
if len(flags) == 1 {
remediation = fmt.Sprintf("Try running the command with the `--%s` flag included", flags[0].Name)
helpMessage = "Learn more about this flag with `--help`"
} else if len(flags) > 1 {
var names []string
for _, flag := range flags {
names = append(names, flag.Name)
}
flags := strings.Join(names, "`\n `--")
remediation = fmt.Sprintf("Consider using the following flags when running this command:\n `--%s`", flags)
helpMessage = "Learn more about these flags with `--help`"
}
return slackerror.New(slackerror.ErrPrompt).
WithDetails(slackerror.ErrorDetails{
slackerror.ErrorDetail{Message: "The input device is not a TTY or does not support interactivity"},
}).
WithRemediation("%s\n%s", remediation, helpMessage)
}

return multiSelectForm(io, ctx, message, options)
}

Expand Down
91 changes: 91 additions & 0 deletions internal/iostreams/prompts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,97 @@ func TestPasswordPrompt(t *testing.T) {
}
}

func TestConfirmPrompt(t *testing.T) {
tests := map[string]struct {
expectedError string
}{
"error if non-TTY": {
expectedError: slackerror.ErrPrompt,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())

fsMock := slackdeps.NewFsMock()
osMock := slackdeps.NewOsMock()
osMock.On("Stdout").Return(&slackdeps.FileMock{FileInfo: &slackdeps.FileInfoNamedPipe{}})
cfg := config.NewConfig(fsMock, osMock)
io := NewIOStreams(cfg, fsMock, osMock)

_, err := io.ConfirmPrompt(ctx, "Continue?", false)

assert.Error(t, err)
assert.Equal(t, tc.expectedError, slackerror.ToSlackError(err).Code)
})
}
}

func TestInputPrompt(t *testing.T) {
tests := map[string]struct {
required bool
expectedError string
expectedValue string
}{
"error if non-TTY and required": {
required: true,
expectedError: slackerror.ErrPrompt,
},
"no error if non-TTY and optional": {
required: false,
expectedValue: "",
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())

fsMock := slackdeps.NewFsMock()
osMock := slackdeps.NewOsMock()
osMock.On("Stdout").Return(&slackdeps.FileMock{FileInfo: &slackdeps.FileInfoNamedPipe{}})
cfg := config.NewConfig(fsMock, osMock)
io := NewIOStreams(cfg, fsMock, osMock)

value, err := io.InputPrompt(ctx, "Enter name", InputPromptConfig{
Required: tc.required,
})

if err != nil {
assert.Equal(t, tc.expectedError, slackerror.ToSlackError(err).Code)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedValue, value)
}
})
}
}

func TestMultiSelectPrompt(t *testing.T) {
tests := map[string]struct {
expectedError string
}{
"error if non-TTY": {
expectedError: slackerror.ErrPrompt,
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ctx := slackcontext.MockContext(t.Context())

fsMock := slackdeps.NewFsMock()
osMock := slackdeps.NewOsMock()
osMock.On("Stdout").Return(&slackdeps.FileMock{FileInfo: &slackdeps.FileInfoNamedPipe{}})
cfg := config.NewConfig(fsMock, osMock)
io := NewIOStreams(cfg, fsMock, osMock)

_, err := io.MultiSelectPrompt(ctx, "Pick items", []string{"a", "b"})

assert.Error(t, err)
assert.Equal(t, tc.expectedError, slackerror.ToSlackError(err).Code)
})
}
}

func TestSelectPrompt(t *testing.T) {
tests := map[string]struct {
flagValue string
Expand Down
Loading