Skip to content

Commit 779cd90

Browse files
spboyerCopilot
andcommitted
fix: unify fail-on-prompt error format with shared helper
- Extract FailOnPromptError/FailOnPromptSelectError helpers - Use shared helpers in asker.go, console.go, and prompt_service.go - Revert unrelated go fix change (process_darwin.go) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 6132f92 commit 779cd90

File tree

5 files changed

+47
-77
lines changed

5 files changed

+47
-77
lines changed

cli/azd/internal/grpcserver/prompt_service.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/azure/azure-dev/cli/azd/pkg/ai"
1717
"github.com/azure/azure-dev/cli/azd/pkg/azapi"
1818
"github.com/azure/azure-dev/cli/azd/pkg/azdext"
19+
"github.com/azure/azure-dev/cli/azd/pkg/input"
1920
"github.com/azure/azure-dev/cli/azd/pkg/output"
2021
"github.com/azure/azure-dev/cli/azd/pkg/prompt"
2122
"github.com/azure/azure-dev/cli/azd/pkg/ux"
@@ -53,11 +54,7 @@ func (s *promptService) Confirm(ctx context.Context, req *azdext.ConfirmRequest)
5354
}
5455

5556
if s.globalOptions.FailOnPrompt {
56-
return nil, fmt.Errorf(
57-
"interactive prompt not allowed in strict mode: %q"+
58-
" (provide the value via command-line flags or environment variables)",
59-
req.Options.Message,
60-
)
57+
return nil, input.FailOnPromptError(req.Options.Message)
6158
}
6259

6360
if s.globalOptions.NoPrompt {
@@ -102,13 +99,7 @@ func (s *promptService) Select(ctx context.Context, req *azdext.SelectRequest) (
10299
for i, c := range req.Options.Choices {
103100
choiceLabels[i] = c.Label
104101
}
105-
return nil, fmt.Errorf(
106-
"interactive prompt not allowed in strict mode: %q"+
107-
" (available options: %s -- specify via"+
108-
" command-line flags or environment variables)",
109-
req.Options.Message,
110-
strings.Join(choiceLabels, ", "),
111-
)
102+
return nil, input.FailOnPromptSelectError(req.Options.Message, choiceLabels)
112103
}
113104

114105
if s.globalOptions.NoPrompt {
@@ -166,13 +157,7 @@ func (s *promptService) MultiSelect(
166157
for i, c := range req.Options.Choices {
167158
choiceLabels[i] = c.Label
168159
}
169-
return nil, fmt.Errorf(
170-
"interactive prompt not allowed in strict mode: %q"+
171-
" (available options: %s -- specify via"+
172-
" command-line flags or environment variables)",
173-
req.Options.Message,
174-
strings.Join(choiceLabels, ", "),
175-
)
160+
return nil, input.FailOnPromptSelectError(req.Options.Message, choiceLabels)
176161
}
177162

178163
if s.globalOptions.NoPrompt {
@@ -231,11 +216,7 @@ func (s *promptService) MultiSelect(
231216

232217
func (s *promptService) Prompt(ctx context.Context, req *azdext.PromptRequest) (*azdext.PromptResponse, error) {
233218
if s.globalOptions.FailOnPrompt {
234-
return nil, fmt.Errorf(
235-
"interactive prompt not allowed in strict mode: %q"+
236-
" (provide the value via command-line flags or environment variables)",
237-
req.Options.Message,
238-
)
219+
return nil, input.FailOnPromptError(req.Options.Message)
239220
}
240221

241222
if s.globalOptions.NoPrompt {

cli/azd/pkg/azdext/process_darwin.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ func findProcessByNameOS(name string) []ProcessInfo {
6666
nameLower := strings.ToLower(name)
6767
var results []ProcessInfo
6868

69-
for line := range strings.SplitSeq(string(output), "\n") {
69+
for _, line := range strings.Split(string(output), "\n") {
7070
line = strings.TrimSpace(line)
7171
if line == "" {
7272
continue

cli/azd/pkg/input/asker.go

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -37,48 +37,17 @@ func NewAsker(noPrompt bool, failOnPrompt bool, isTerminal bool, w io.Writer, r
3737
func askOneFailOnPrompt(p survey.Prompt, _ any) error {
3838
switch v := p.(type) {
3939
case *survey.Input:
40-
return fmt.Errorf(
41-
"interactive prompt not allowed in strict mode: %q"+
42-
" (provide the value via command-line flags"+
43-
" or environment variables)",
44-
v.Message,
45-
)
40+
return FailOnPromptError(v.Message)
4641
case *survey.Select:
47-
return fmt.Errorf(
48-
"interactive prompt not allowed in strict mode: %q"+
49-
" (available options: %s -- specify via"+
50-
" command-line flags or environment variables)",
51-
v.Message,
52-
strings.Join(v.Options, ", "),
53-
)
42+
return FailOnPromptSelectError(v.Message, v.Options)
5443
case *survey.Confirm:
55-
return fmt.Errorf(
56-
"interactive prompt not allowed in strict mode: %q"+
57-
" (provide the value via command-line flags"+
58-
" or environment variables)",
59-
v.Message,
60-
)
44+
return FailOnPromptError(v.Message)
6145
case *survey.MultiSelect:
62-
return fmt.Errorf(
63-
"interactive prompt not allowed in strict mode: %q"+
64-
" (available options: %s -- specify via"+
65-
" command-line flags or environment variables)",
66-
v.Message,
67-
strings.Join(v.Options, ", "),
68-
)
46+
return FailOnPromptSelectError(v.Message, v.Options)
6947
case *survey.Password:
70-
return fmt.Errorf(
71-
"interactive prompt not allowed in strict mode: %q"+
72-
" (provide the value via command-line flags"+
73-
" or environment variables)",
74-
v.Message,
75-
)
48+
return FailOnPromptError(v.Message)
7649
default:
77-
return fmt.Errorf(
78-
"interactive prompt not allowed in strict mode" +
79-
" (provide the value via command-line flags" +
80-
" or environment variables)",
81-
)
50+
return FailOnPromptError("")
8251
}
8352
}
8453

cli/azd/pkg/input/console.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,7 @@ func (c *AskerConsole) SupportsPromptDialog() bool {
584584
// values.
585585
func (c *AskerConsole) PromptDialog(ctx context.Context, dialog PromptDialog) (map[string]any, error) {
586586
if c.failOnPrompt {
587-
return nil, fmt.Errorf(
588-
"interactive prompt not allowed in strict mode (--fail-on-prompt): %q"+
589-
" -- specify via command-line flags or environment variables", dialog.Title)
587+
return nil, FailOnPromptError(dialog.Title)
590588
}
591589

592590
request := externalPromptDialogRequest{
@@ -629,9 +627,7 @@ func (c *AskerConsole) Prompt(ctx context.Context, options ConsoleOptions) (stri
629627
var response string
630628

631629
if c.failOnPrompt && c.promptClient != nil {
632-
return "", fmt.Errorf(
633-
"interactive prompt not allowed in strict mode (--fail-on-prompt): %q"+
634-
" -- specify via command-line flags or environment variables", options.Message)
630+
return "", FailOnPromptError(options.Message)
635631
}
636632

637633
if c.promptClient != nil {
@@ -693,9 +689,7 @@ func choicesFromOptions(options ConsoleOptions) []promptChoice {
693689
// Prompts the user to select from a set of values
694690
func (c *AskerConsole) Select(ctx context.Context, options ConsoleOptions) (int, error) {
695691
if c.failOnPrompt && c.promptClient != nil {
696-
return -1, fmt.Errorf(
697-
"interactive prompt not allowed in strict mode (--fail-on-prompt): %q"+
698-
" -- specify via command-line flags or environment variables", options.Message)
692+
return -1, FailOnPromptError(options.Message)
699693
}
700694

701695
if c.promptClient != nil {
@@ -779,9 +773,7 @@ func (c *AskerConsole) MultiSelect(ctx context.Context, options ConsoleOptions)
779773
var response []string
780774

781775
if c.failOnPrompt && c.promptClient != nil {
782-
return nil, fmt.Errorf(
783-
"interactive prompt not allowed in strict mode (--fail-on-prompt): %q"+
784-
" -- specify via command-line flags or environment variables", options.Message)
776+
return nil, FailOnPromptError(options.Message)
785777
}
786778

787779
if c.promptClient != nil {
@@ -853,9 +845,7 @@ func (c *AskerConsole) MultiSelect(ctx context.Context, options ConsoleOptions)
853845
// Prompts the user to confirm an operation
854846
func (c *AskerConsole) Confirm(ctx context.Context, options ConsoleOptions) (bool, error) {
855847
if c.failOnPrompt && c.promptClient != nil {
856-
return false, fmt.Errorf(
857-
"interactive prompt not allowed in strict mode (--fail-on-prompt): %q"+
858-
" -- specify via command-line flags or environment variables", options.Message)
848+
return false, FailOnPromptError(options.Message)
859849
}
860850

861851
if c.promptClient != nil {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
package input
5+
6+
import (
7+
"fmt"
8+
"strings"
9+
)
10+
11+
// FailOnPromptError returns a consistent error for when --fail-on-prompt blocks a simple prompt.
12+
func FailOnPromptError(promptMessage string) error {
13+
return fmt.Errorf(
14+
"interactive prompt not allowed in strict mode (--fail-on-prompt): %q"+
15+
" -- provide the value via command-line flags or environment variables",
16+
promptMessage,
17+
)
18+
}
19+
20+
// FailOnPromptSelectError returns a consistent error for when --fail-on-prompt blocks a selection prompt,
21+
// including the available options in the message.
22+
func FailOnPromptSelectError(promptMessage string, options []string) error {
23+
return fmt.Errorf(
24+
"interactive prompt not allowed in strict mode (--fail-on-prompt): %q"+
25+
" (available options: %s)"+
26+
" -- specify via command-line flags or environment variables",
27+
promptMessage,
28+
strings.Join(options, ", "),
29+
)
30+
}

0 commit comments

Comments
 (0)