- Notifications
You must be signed in to change notification settings - Fork926
fix: fix loss of buffered input on cliui.Prompt#15421
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
37 changes: 31 additions & 6 deletionscli/cliui/prompt.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package cliui | ||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/signal" | ||
"strings" | ||
@@ -96,14 +96,13 @@ func Prompt(inv *serpent.Invocation, opts PromptOptions) (string, error) { | ||
signal.Notify(interrupt, os.Interrupt) | ||
defer signal.Stop(interrupt) | ||
line, err = readUntil(inv.Stdin, '\n') | ||
// Check if the first line beings with JSON object or array chars. | ||
// This enables multiline JSON to be pasted into an input, and have | ||
// it parse properly. | ||
if err == nil && (strings.HasPrefix(line, "{") || strings.HasPrefix(line, "[")) { | ||
line, err = promptJSON(inv.Stdin, line) | ||
} | ||
} | ||
if err != nil { | ||
@@ -144,7 +143,7 @@ func Prompt(inv *serpent.Invocation, opts PromptOptions) (string, error) { | ||
} | ||
} | ||
func promptJSON(readerio.Reader, line string) (string, error) { | ||
var data bytes.Buffer | ||
for { | ||
_, _ = data.WriteString(line) | ||
@@ -162,7 +161,7 @@ func promptJSON(reader *bufio.Reader, line string) (string, error) { | ||
// Read line-by-line. We can't use a JSON decoder | ||
// here because it doesn't work by newline, so | ||
// reads will block. | ||
line, err =readUntil(reader,'\n') | ||
if err != nil { | ||
break | ||
} | ||
@@ -179,3 +178,29 @@ func promptJSON(reader *bufio.Reader, line string) (string, error) { | ||
} | ||
return line, nil | ||
} | ||
// readUntil the first occurrence of delim in the input, returning a string containing the data up | ||
// to and including the delimiter. Unlike `bufio`, it only reads until the delimiter and no further | ||
// bytes. If readUntil encounters an error before finding a delimiter, it returns the data read | ||
// before the error and the error itself (often io.EOF). readUntil returns err != nil if and only if | ||
// the returned data does not end in delim. | ||
func readUntil(r io.Reader, delim byte) (string, error) { | ||
var ( | ||
have []byte | ||
b = make([]byte, 1) | ||
) | ||
for { | ||
n, err := r.Read(b) | ||
if n > 0 { | ||
spikecurtis marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
have = append(have, b[0]) | ||
if b[0] == delim { | ||
// match `bufio` in that we only return non-nil if we didn't find the delimiter, | ||
// regardless of whether we also erred. | ||
return string(have), nil | ||
} | ||
} | ||
if err != nil { | ||
return string(have), err | ||
} | ||
} | ||
} |
68 changes: 53 additions & 15 deletionscli/cliui/prompt_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.