lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 9b19a35e05b70d9e46b3cc122f60542206e503c7
parent e09a1030bbe1580057edd53df26b6132e990d571
Author: Sean Enck <sean@ttypty.com>
Date:   Sat,  4 Mar 2023 16:01:56 -0500

Revert "use pgl"

This reverts commit 8ae1e4cb27d227ddacf1976e418e9f32f7e18ea7.

Diffstat:
Mcmd/main.go | 4++--
Mgo.mod | 3+--
Mgo.sum | 6++----
Minternal/app/core.go | 4++--
Minternal/inputs/stdin.go | 10++--------
Ainternal/util/core.go | 75+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mscripts/testing/check.go | 14+++++++++-----
7 files changed, 93 insertions(+), 23 deletions(-)

diff --git a/cmd/main.go b/cmd/main.go @@ -15,7 +15,7 @@ import ( "github.com/enckse/lockbox/internal/inputs" "github.com/enckse/lockbox/internal/platform" "github.com/enckse/lockbox/internal/totp" - format "github.com/enckse/pgl/fmt" + "github.com/enckse/lockbox/internal/util" ) //go:embed "vers.txt" @@ -23,7 +23,7 @@ var version string func main() { if err := run(); err != nil { - format.Die(err) + util.Die(err) } } diff --git a/go.mod b/go.mod @@ -4,7 +4,6 @@ go 1.19 require ( github.com/aymanbagabas/go-osc52 v1.2.2 - github.com/enckse/pgl v0.0.0-20230304192233-efce9c7eba30 github.com/pquerna/otp v1.4.0 github.com/tobischo/gokeepasslib/v3 v3.5.0 mvdan.cc/sh/v3 v3.6.0 @@ -15,5 +14,5 @@ require ( github.com/aead/chacha20 v0.0.0-20180709150244-8b13a72661da // indirect github.com/boombuler/barcode v1.0.1 // indirect golang.org/x/crypto v0.6.0 // indirect - golang.org/x/sys v0.6.0 // indirect + golang.org/x/sys v0.5.0 // indirect ) diff --git a/go.sum b/go.sum @@ -9,8 +9,6 @@ github.com/boombuler/barcode v1.0.1 h1:NDBbPmhS+EqABEs5Kg3n/5ZNjy73Pz7SIV+KCeqyX github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= -github.com/enckse/pgl v0.0.0-20230304192233-efce9c7eba30 h1:djxZAG9L0SB/T9vXPBPpgqU4o9uRpdzhDns3umXjVj8= -github.com/enckse/pgl v0.0.0-20230304192233-efce9c7eba30/go.mod h1:r5bqGzwqnJIeY6UbGT5u38keJ5+ZySlsWeaYYzdBhMg= github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= @@ -28,8 +26,8 @@ github.com/tobischo/gokeepasslib/v3 v3.5.0/go.mod h1:IFUgenONAqJlU2RLfVagQbF4GRY golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc= golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58= golang.org/x/exp v0.0.0-20230105202349-8879d0199aa3 h1:fJwx88sMf5RXwDwziL0/Mn9Wqs+efMSo/RYcL+37W9c= -golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= -golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= mvdan.cc/sh/v3 v3.6.0 h1:gtva4EXJ0dFNvl5bHjcUEvws+KRcDslT8VKheTYkbGU= mvdan.cc/sh/v3 v3.6.0/go.mod h1:U4mhtBLZ32iWhif5/lD+ygy1zrgaQhUu+XFy7C8+TTA= diff --git a/internal/app/core.go b/internal/app/core.go @@ -7,7 +7,7 @@ import ( "github.com/enckse/lockbox/internal/backend" "github.com/enckse/lockbox/internal/inputs" - "github.com/enckse/pgl/fmt" + "github.com/enckse/lockbox/internal/util" ) type ( @@ -54,7 +54,7 @@ func (a *DefaultCommand) Transaction() *backend.Transaction { func (a *DefaultCommand) Confirm(prompt string) bool { yesNo, err := inputs.ConfirmYesNoPrompt(prompt) if err != nil { - fmt.Dief("failed to read stdin for confirmation: %v", err) + util.Dief("failed to read stdin for confirmation: %v", err) } return yesNo } diff --git a/internal/inputs/stdin.go b/internal/inputs/stdin.go @@ -8,7 +8,7 @@ import ( "strings" "syscall" - "github.com/enckse/pgl/io" + "github.com/enckse/lockbox/internal/util" ) func termEcho(on bool) { @@ -86,13 +86,7 @@ func confirmInputsMatch() (string, error) { // Stdin will get one (or more) lines of stdin as string. func Stdin(one bool) (string, error) { - var b []byte - var err error - if one { - b, err = io.ReadStdinLine() - } else { - b, err = io.ReadAllStdin() - } + b, err := util.ReadStdin(one) if err != nil { return "", err } 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 +} + +// Dief will for a message and die +func Dief(format string, a ...any) { + Die(fmt.Sprintf(format, a...)) +} + +// Die will write to stderr and exit (1) +func Die(a any) { + if a != nil { + fmt.Fprintf(os.Stderr, "%v\n", a) + } + 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 +} + +// IfNotSet will return the value or the default if the value is the value's default setting +func IfNotSet[T comparable](val, defaultTo T) T { + if val == *new(T) { + return defaultTo + } + return val +} diff --git a/scripts/testing/check.go b/scripts/testing/check.go @@ -12,8 +12,7 @@ import ( "strings" "time" - fp "github.com/enckse/pgl/filepath" - format "github.com/enckse/pgl/fmt" + "github.com/enckse/lockbox/internal/util" ) var yes = []string{"y"} @@ -22,12 +21,17 @@ const ( testKey = "plaintextkey" ) +func die(message string, err error) { + fmt.Fprintf(os.Stderr, "%s (%v)", message, err) + os.Exit(1) +} + func runCommand(args []string, data []string) { p := exec.Command(os.Getenv("LB_BUILD"), args...) var buf bytes.Buffer for _, d := range data { if _, err := buf.WriteString(fmt.Sprintf("%s\n", d)); err != nil { - format.Dief("failed to write stdin: %v", err) + die("failed to write stdin", err) } } p.Stdout = os.Stdout @@ -60,7 +64,7 @@ func totpList() { func main() { if err := execute(); err != nil { - format.Die(err) + die("execution failed", err) } } @@ -211,7 +215,7 @@ func testClipboard(dataPath string, tries uint, wait uint) { } foundClipCount := 0 for _, f := range clipFiles { - if fp.PathExists(f) { + if util.PathExists(f) { foundClipCount++ } }