-
Notifications
You must be signed in to change notification settings - Fork 209
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prompt text should handle piped input
- Loading branch information
1 parent
5619752
commit 82b3892
Showing
14 changed files
with
90 additions
and
67 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package utils | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"strings" | ||
|
||
"github.com/go-errors/errors" | ||
"golang.org/x/term" | ||
) | ||
|
||
type Console struct { | ||
isTTY bool | ||
stdin *bufio.Scanner | ||
stdout io.Writer | ||
stderr io.Writer | ||
} | ||
|
||
func NewConsole() Console { | ||
return Console{ | ||
isTTY: term.IsTerminal(int(os.Stdin.Fd())), | ||
stdin: bufio.NewScanner(os.Stdin), | ||
stdout: os.Stdout, | ||
stderr: GetDebugLogger(), | ||
} | ||
} | ||
|
||
// PromptYesNo asks yes/no questions using the label. | ||
func (c Console) PromptYesNo(label string, def bool) bool { | ||
choices := "Y/n" | ||
if !def { | ||
choices = "y/N" | ||
} | ||
labelWithChoice := fmt.Sprintf("%s [%s] ", label, choices) | ||
// Any error will be handled as default value | ||
if s, err := c.PromptText(labelWithChoice); err != nil { | ||
fmt.Fprintln(c.stdout) | ||
if !errors.Is(err, io.EOF) { | ||
fmt.Fprintln(c.stderr, err) | ||
} | ||
} else if answer := parseYesNo(s); answer != nil { | ||
return *answer | ||
} | ||
return def | ||
} | ||
|
||
func parseYesNo(s string) *bool { | ||
s = strings.ToLower(s) | ||
if s == "y" || s == "yes" { | ||
return Ptr(true) | ||
} | ||
if s == "n" || s == "no" { | ||
return Ptr(false) | ||
} | ||
return nil | ||
} | ||
|
||
// PromptText asks for input using the label. | ||
func (c Console) PromptText(label string) (string, error) { | ||
fmt.Fprint(os.Stderr, label) | ||
// Scan a single line from input or file | ||
if !c.stdin.Scan() { | ||
return "", errors.New(io.EOF) | ||
} | ||
if err := c.stdin.Err(); err != nil { | ||
return "", errors.Errorf("failed to scan stdin: %w", err) | ||
} | ||
token := strings.TrimSpace(c.stdin.Text()) | ||
// Echo input from non-interactive terminal | ||
if !c.isTTY { | ||
fmt.Fprintln(c.stdout, token) | ||
} | ||
return token, nil | ||
} |
This file contains 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