lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 8ef5a5323ca96b424767ee7e88db69ca5b577a34
parent 16cd589fc8a322e78e4098fe6f1d9e78efac31ea
Author: Sean Enck <sean@ttypty.com>
Date:   Mon, 12 Aug 2024 18:43:42 -0400

this is a holdover from an older way of reading stdin, converge

Diffstat:
Minternal/platform/os.go | 51+++++++++++----------------------------------------
1 file changed, 11 insertions(+), 40 deletions(-)

diff --git a/internal/platform/os.go b/internal/platform/os.go @@ -11,10 +11,6 @@ import ( "syscall" ) -type ( - stdinReaderFunc func(string) (bool, error) -) - func termEcho(on bool) { // Common settings and variables for both stty calls. attrs := syscall.ProcAttr{ @@ -98,15 +94,6 @@ func confirmInputsMatch() (string, error) { return first, nil } -// Stdin will get one (or more) lines of stdin as a string. -func Stdin(one bool) (string, error) { - b, err := read(one) - if err != nil { - return "", err - } - return strings.TrimSpace(string(b)), nil -} - // IsInputFromPipe will indicate if connected to stdin pipe. func IsInputFromPipe() bool { fileInfo, _ := os.Stdin.Stat() @@ -123,41 +110,25 @@ func ConfirmYesNoPrompt(prompt string) (bool, error) { return resp == "Y" || resp == "y", nil } -func readFunc(reader stdinReaderFunc) error { - if reader == nil { - return errors.New("invalid reader, nil") - } +// Stdin will get one (or more) lines of stdin as a string. +func Stdin(one bool) (string, error) { + var b bytes.Buffer scanner := bufio.NewScanner(os.Stdin) for scanner.Scan() { - ok, err := reader(scanner.Text()) - if err != nil { - return err - } - if !ok { - break - } - } - return scanner.Err() -} - -func read(one bool) ([]byte, error) { - var b bytes.Buffer - err := readFunc(func(line string) (bool, error) { - if _, err := b.WriteString(line); err != nil { - return false, err + if _, err := b.WriteString(scanner.Text()); err != nil { + return "", err } if _, err := b.WriteString("\n"); err != nil { - return false, err + return "", err } if one { - return false, nil + break } - return true, nil - }) - if err != nil { - return nil, err } - return b.Bytes(), nil + if err := scanner.Err(); err != nil { + return "", err + } + return strings.TrimSpace(b.String()), nil } // PathExists indicates whether a path exists (true) or not (false)