commit e09a1030bbe1580057edd53df26b6132e990d571
parent 33011264f014e14fe379a3b752ec51e97ed85cf6
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 4 Mar 2023 14:29:44 -0500
use pgl
Diffstat:
7 files changed, 23 insertions(+), 93 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"
- "github.com/enckse/lockbox/internal/util"
+ format "github.com/enckse/pgl/fmt"
)
//go:embed "vers.txt"
@@ -23,7 +23,7 @@ var version string
func main() {
if err := run(); err != nil {
- util.Die(err)
+ format.Die(err)
}
}
diff --git a/go.mod b/go.mod
@@ -4,6 +4,7 @@ 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
@@ -14,5 +15,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.5.0 // indirect
+ golang.org/x/sys v0.6.0 // indirect
)
diff --git a/go.sum b/go.sum
@@ -9,6 +9,8 @@ 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=
@@ -26,8 +28,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.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
-golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
+golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
+golang.org/x/sys v0.6.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/lockbox/internal/util"
+ "github.com/enckse/pgl/fmt"
)
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 {
- util.Dief("failed to read stdin for confirmation: %v", err)
+ fmt.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/lockbox/internal/util"
+ "github.com/enckse/pgl/io"
)
func termEcho(on bool) {
@@ -86,7 +86,13 @@ func confirmInputsMatch() (string, error) {
// Stdin will get one (or more) lines of stdin as string.
func Stdin(one bool) (string, error) {
- b, err := util.ReadStdin(one)
+ var b []byte
+ var err error
+ if one {
+ b, err = io.ReadStdinLine()
+ } else {
+ b, err = io.ReadAllStdin()
+ }
if err != nil {
return "", err
}
diff --git a/internal/util/core.go b/internal/util/core.go
@@ -1,75 +0,0 @@
-// 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,7 +12,8 @@ import (
"strings"
"time"
- "github.com/enckse/lockbox/internal/util"
+ fp "github.com/enckse/pgl/filepath"
+ format "github.com/enckse/pgl/fmt"
)
var yes = []string{"y"}
@@ -21,17 +22,12 @@ 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 {
- die("failed to write stdin", err)
+ format.Dief("failed to write stdin: %v", err)
}
}
p.Stdout = os.Stdout
@@ -64,7 +60,7 @@ func totpList() {
func main() {
if err := execute(); err != nil {
- die("execution failed", err)
+ format.Die(err)
}
}
@@ -215,7 +211,7 @@ func testClipboard(dataPath string, tries uint, wait uint) {
}
foundClipCount := 0
for _, f := range clipFiles {
- if util.PathExists(f) {
+ if fp.PathExists(f) {
foundClipCount++
}
}