-
Notifications
You must be signed in to change notification settings - Fork 5.8k
cmd/prompt: remove uses of github.com/AlecAivazis/survey/v2 #14161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -17,10 +17,11 @@ | |||||
| package prompt | ||||||
|
|
||||||
| import ( | ||||||
| "bufio" | ||||||
| "fmt" | ||||||
| "io" | ||||||
| "strings" | ||||||
|
|
||||||
| "github.com/AlecAivazis/survey/v2" | ||||||
| "github.com/docker/cli/cli/streams" | ||||||
|
|
||||||
| "github.com/docker/compose/v5/pkg/utils" | ||||||
|
|
@@ -35,56 +36,20 @@ type UI interface { | |||||
|
|
||||||
| func NewPrompt(stdin *streams.In, stdout *streams.Out) UI { | ||||||
| if stdin.IsTerminal() { | ||||||
| return User{stdin: streamsFileReader{stdin}, stdout: streamsFileWriter{stdout}} | ||||||
| return User{stdin: bufio.NewReader(stdin), stdout: stdout} | ||||||
| } | ||||||
| return Pipe{stdin: stdin, stdout: stdout} | ||||||
| } | ||||||
|
|
||||||
| // User - in a terminal | ||||||
| type User struct { | ||||||
| stdout streamsFileWriter | ||||||
| stdin streamsFileReader | ||||||
| } | ||||||
|
|
||||||
| // adapt streams.Out to terminal.FileWriter | ||||||
| type streamsFileWriter struct { | ||||||
| stream *streams.Out | ||||||
| } | ||||||
|
|
||||||
| func (s streamsFileWriter) Write(p []byte) (n int, err error) { | ||||||
| return s.stream.Write(p) | ||||||
| } | ||||||
|
|
||||||
| func (s streamsFileWriter) Fd() uintptr { | ||||||
| return s.stream.FD() | ||||||
| } | ||||||
|
|
||||||
| // adapt streams.In to terminal.FileReader | ||||||
| type streamsFileReader struct { | ||||||
| stream *streams.In | ||||||
| } | ||||||
|
|
||||||
| func (s streamsFileReader) Read(p []byte) (n int, err error) { | ||||||
| return s.stream.Read(p) | ||||||
| } | ||||||
|
|
||||||
| func (s streamsFileReader) Fd() uintptr { | ||||||
| return s.stream.FD() | ||||||
| stdout io.Writer | ||||||
| stdin *bufio.Reader | ||||||
| } | ||||||
|
|
||||||
| // Confirm asks for yes or no input | ||||||
| func (u User) Confirm(message string, defaultValue bool) (bool, error) { | ||||||
| qs := &survey.Confirm{ | ||||||
| Message: message, | ||||||
| Default: defaultValue, | ||||||
| } | ||||||
| var b bool | ||||||
| err := survey.AskOne(qs, &b, func(options *survey.AskOptions) error { | ||||||
| options.Stdio.In = u.stdin | ||||||
| options.Stdio.Out = u.stdout | ||||||
| return nil | ||||||
| }) | ||||||
| return b, err | ||||||
| return confirm(u.stdin, u.stdout, message, defaultValue) | ||||||
| } | ||||||
|
|
||||||
| // Pipe - aggregates prompt methods | ||||||
|
|
@@ -100,3 +65,18 @@ func (u Pipe) Confirm(message string, defaultValue bool) (bool, error) { | |||||
| _, _ = fmt.Fscanln(u.stdin, &answer) | ||||||
| return utils.StringToBool(answer), nil | ||||||
| } | ||||||
|
|
||||||
| func confirm(stdin *bufio.Reader, stdout io.Writer, message string, defaultValue bool) (bool, error) { | ||||||
| _, _ = fmt.Fprint(stdout, message) | ||||||
|
|
||||||
| answer, err := stdin.ReadString('\n') | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This blocking read has no way to observe cancellation. Since |
||||||
| if err != nil && err != io.EOF { | ||||||
| return false, err | ||||||
| } | ||||||
|
|
||||||
| answer = strings.TrimSpace(answer) | ||||||
| if answer == "" { | ||||||
| return defaultValue, nil | ||||||
| } | ||||||
| return utils.StringToBool(answer), nil | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [medium] Invalid user input silently returns false instead of re-prompting When the user types an unrecognised string (e.g. return utils.StringToBool(answer), nil
Consider validating the answer and returning an error or looping on unrecognised input. A minimal fix: result, ok := parseBool(answer)
if !ok {
return false, fmt.Errorf("invalid input %q: expected y/yes/n/no", answer)
}
return result, nil
|
||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| Copyright 2020 Docker Compose CLI authors | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package prompt | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "bytes" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "gotest.tools/v3/assert" | ||
| ) | ||
|
|
||
| // TestUserConfirmSequential verifies that consecutive confirmations | ||
| // consume one line at a time. | ||
| func TestUserConfirmSequential(t *testing.T) { | ||
| var stdout bytes.Buffer | ||
| user := User{ | ||
| stdin: bufio.NewReader(strings.NewReader("y\nn\n")), | ||
| stdout: &stdout, | ||
| } | ||
|
|
||
| got, err := user.Confirm("first? ", false) | ||
| assert.NilError(t, err) | ||
| assert.Assert(t, got) | ||
|
|
||
| got, err = user.Confirm("second? ", true) | ||
| assert.NilError(t, err) | ||
| assert.Assert(t, !got) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[medium] confirm() does not print [y/N] hint; behavioral regression from survey.Confirm
The new
confirm()function writesmessageverbatim to stdout with no indication of valid responses or the current default:survey.Confirmautomatically appended(y/N)or(Y/n)(depending ondefaultValue) to every prompt. The new implementation omits this entirely. Unless every caller already embeds the hint in its own message string, users running in a terminal will see a bare prompt with no cue thaty/nare the expected inputs or which value is the default.Consider appending the hint in
confirm()itself so callers don't have to know about this detail: