lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 489b1d9f022653a343f9ac27da71ce835bc31498
parent fb19f738e743784b69636ba4768db8063f1da72f
Author: Sean Enck <sean@ttypty.com>
Date:   Thu, 16 Feb 2023 18:17:33 -0500

another common function

Diffstat:
Mcmd/main.go | 10+++-------
Minternal/inputs/stdin.go | 22+++-------------------
Ainternal/util/core.go | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 81 insertions(+), 26 deletions(-)

diff --git a/cmd/main.go b/cmd/main.go @@ -15,6 +15,7 @@ import ( "github.com/enckse/lockbox/internal/inputs" "github.com/enckse/lockbox/internal/platform" "github.com/enckse/lockbox/internal/totp" + "github.com/enckse/lockbox/internal/util" ) //go:embed "vers.txt" @@ -36,14 +37,9 @@ func internalCallback(name string) callbackFunction { return nil } -func exit(err error) { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) -} - func main() { if err := run(); err != nil { - exit(err) + util.Fatal(err) } } @@ -364,7 +360,7 @@ func clearClipboard(args []string) error { func confirm(prompt string) bool { yesNo, err := inputs.ConfirmYesNoPrompt(prompt) if err != nil { - exit(wrapped("failed to get response", err)) + util.Die("failed to read stdin for confirmation", err) } return yesNo } diff --git a/internal/inputs/stdin.go b/internal/inputs/stdin.go @@ -2,13 +2,13 @@ package inputs import ( - "bufio" - "bytes" "errors" "fmt" "os" "strings" "syscall" + + "github.com/enckse/lockbox/internal/util" ) func termEcho(on bool) { @@ -86,7 +86,7 @@ func confirmInputsMatch() (string, error) { // Stdin will get one (or more) lines of stdin as string. func Stdin(one bool) (string, error) { - b, err := getStdin(one) + b, err := util.ReadStdin(one) if err != nil { return "", err } @@ -108,19 +108,3 @@ func ConfirmYesNoPrompt(prompt string) (bool, error) { } return resp == "Y" || resp == "y", nil } - -func getStdin(one bool) ([]byte, error) { - scanner := bufio.NewScanner(os.Stdin) - var b bytes.Buffer - for scanner.Scan() { - b.WriteString(scanner.Text()) - b.WriteString("\n") - if one { - break - } - } - if err := scanner.Err(); err != nil { - return nil, err - } - return b.Bytes(), nil -} diff --git a/internal/util/core.go b/internal/util/core.go @@ -0,0 +1,75 @@ +// Package util provides some common operations +package util + +import ( + "bufio" + "bytes" + "errors" + "fmt" + "io/fs" + "os" +) + +// PathExists will indicate if a path exists +func PathExists(file string) bool { + if _, err := os.Stat(file); errors.Is(err, os.ErrNotExist) { + return false + } + return true +} + +// Fatal will call Die but without a message +func Fatal(err error) { + Die("", err) +} + +// Die will write to stderr and exit (1) +func Die(message string, err error) { + msg := message + if err != nil { + if msg == "" { + msg = err.Error() + } else { + msg = fmt.Sprintf("%s (%v)", msg, err) + } + } + if msg != "" { + fmt.Fprintf(os.Stderr, "%s\n", msg) + } + os.Exit(1) +} + +// Copy will copy a file from source to destination via ReadFile/WriteFile +func Copy(src, dst string, mode fs.FileMode) error { + if !PathExists(src) { + return fmt.Errorf("source file '%s' does not exist", src) + } + + in, err := os.ReadFile(src) + if err != nil { + return err + } + + if err := os.WriteFile(dst, in, mode); err != nil { + return err + } + + return nil +} + +// ReadStdin will read one (or more) stdin lines +func ReadStdin(one bool) ([]byte, error) { + scanner := bufio.NewScanner(os.Stdin) + var b bytes.Buffer + for scanner.Scan() { + b.WriteString(scanner.Text()) + b.WriteString("\n") + if one { + break + } + } + if err := scanner.Err(); err != nil { + return nil, err + } + return b.Bytes(), nil +}