Skip to content

Commit 34ac465

Browse files
authored
refactor(experiment): swap prompt interrupt error after prompt interrupt (#412)
1 parent 8056444 commit 34ac465

3 files changed

Lines changed: 19 additions & 10 deletions

File tree

.claude/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"permissions": {
33
"allow": [
4+
"Bash(go build:*)",
45
"Bash(go mod tidy:*)",
6+
"Bash(go test:*)",
57
"Bash(gofmt:*)",
68
"Bash(gh issue view:*)",
79
"Bash(gh label list:*)",

cmd/root.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"strings"
2323
"syscall"
2424

25-
huh "charm.land/huh/v2"
2625
"github.com/slackapi/slack-cli/cmd/app"
2726
"github.com/slackapi/slack-cli/cmd/auth"
2827
"github.com/slackapi/slack-cli/cmd/collaborators"
@@ -149,10 +148,6 @@ func Init(ctx context.Context) (*cobra.Command, *shared.ClientFactory) {
149148
// updateNotification will check for an update in the background and print a message after the command runs
150149
var updateNotification *update.UpdateNotification
151150

152-
// Override huh's default user abort error with a Slack CLI error so that
153-
// cancelled prompts are handled consistently as process interruptions.
154-
huh.ErrUserAborted = slackerror.New(slackerror.ErrProcessInterrupted)
155-
156151
clients = shared.NewClientFactory(shared.SetVersion(version.Raw()))
157152
rootCmd := NewRootCommand(clients, updateNotification)
158153

internal/iostreams/charm.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@ package iostreams
1919

2020
import (
2121
"context"
22+
"errors"
2223
"slices"
2324

2425
huh "charm.land/huh/v2"
26+
"github.com/slackapi/slack-cli/internal/slackerror"
2527
"github.com/slackapi/slack-cli/internal/style"
2628
)
2729

@@ -42,7 +44,9 @@ func buildInputForm(message string, cfg InputPromptConfig, input *string) *huh.F
4244
func charmInputPrompt(_ *IOStreams, _ context.Context, message string, cfg InputPromptConfig) (string, error) {
4345
var input string
4446
err := buildInputForm(message, cfg, &input).Run()
45-
if err != nil {
47+
if errors.Is(err, huh.ErrUserAborted) {
48+
return "", slackerror.New(slackerror.ErrProcessInterrupted)
49+
} else if err != nil {
4650
return "", err
4751
}
4852
return input, nil
@@ -60,7 +64,9 @@ func buildConfirmForm(message string, choice *bool) *huh.Form {
6064
func charmConfirmPrompt(_ *IOStreams, _ context.Context, message string, defaultValue bool) (bool, error) {
6165
var choice = defaultValue
6266
err := buildConfirmForm(message, &choice).Run()
63-
if err != nil {
67+
if errors.Is(err, huh.ErrUserAborted) {
68+
return false, slackerror.New(slackerror.ErrProcessInterrupted)
69+
} else if err != nil {
6470
return false, err
6571
}
6672
return choice, nil
@@ -92,7 +98,9 @@ func buildSelectForm(msg string, options []string, cfg SelectPromptConfig, selec
9298
func charmSelectPrompt(_ *IOStreams, _ context.Context, msg string, options []string, cfg SelectPromptConfig) (SelectPromptResponse, error) {
9399
var selected string
94100
err := buildSelectForm(msg, options, cfg, &selected).Run()
95-
if err != nil {
101+
if errors.Is(err, huh.ErrUserAborted) {
102+
return SelectPromptResponse{}, slackerror.New(slackerror.ErrProcessInterrupted)
103+
} else if err != nil {
96104
return SelectPromptResponse{}, err
97105
}
98106

@@ -117,7 +125,9 @@ func buildPasswordForm(message string, cfg PasswordPromptConfig, input *string)
117125
func charmPasswordPrompt(_ *IOStreams, _ context.Context, message string, cfg PasswordPromptConfig) (PasswordPromptResponse, error) {
118126
var input string
119127
err := buildPasswordForm(message, cfg, &input).Run()
120-
if err != nil {
128+
if errors.Is(err, huh.ErrUserAborted) {
129+
return PasswordPromptResponse{}, slackerror.New(slackerror.ErrProcessInterrupted)
130+
} else if err != nil {
121131
return PasswordPromptResponse{}, err
122132
}
123133
return PasswordPromptResponse{Prompt: true, Value: input}, nil
@@ -142,7 +152,9 @@ func buildMultiSelectForm(message string, options []string, selected *[]string)
142152
func charmMultiSelectPrompt(_ *IOStreams, _ context.Context, message string, options []string) ([]string, error) {
143153
var selected []string
144154
err := buildMultiSelectForm(message, options, &selected).Run()
145-
if err != nil {
155+
if errors.Is(err, huh.ErrUserAborted) {
156+
return []string{}, slackerror.New(slackerror.ErrProcessInterrupted)
157+
} else if err != nil {
146158
return []string{}, err
147159
}
148160
return selected, nil

0 commit comments

Comments
 (0)