commit ac663d9d78e7f96596d477c37c40467d4a47260f
parent 23014eda042fab8ffbbb0e0dd09618a514e17b7b
Author: Sean Enck <sean@ttypty.com>
Date: Sat, 16 Jul 2022 09:13:34 -0400
remove 'custom' error
Diffstat:
7 files changed, 37 insertions(+), 44 deletions(-)
diff --git a/cmd/lb-rw/main.go b/cmd/lb-rw/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "errors"
"flag"
"fmt"
@@ -29,6 +30,6 @@ func main() {
}
fmt.Println(string(results))
default:
- internal.Die("invalid mode", internal.NewLockboxError("bad mode"))
+ internal.Die("invalid mode", errors.New("bad mode"))
}
}
diff --git a/cmd/lb-totp/main.go b/cmd/lb-totp/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "errors"
"fmt"
"io/fs"
"os"
@@ -46,7 +47,7 @@ func list() ([]string, error) {
results = append(results, obj)
}
if len(results) == 0 {
- return nil, internal.NewLockboxError("no objects found")
+ return nil, errors.New("no objects found")
}
return results, nil
}
@@ -76,7 +77,7 @@ func display(token string, args internal.Arguments) error {
interactive = false
}
if !interactive && args.Clip {
- return internal.NewLockboxError("clipboard not available in non-interactive mode")
+ return errors.New("clipboard not available in non-interactive mode")
}
redStart, redEnd, err := internal.GetColor(internal.ColorRed)
if err != nil {
@@ -85,7 +86,7 @@ func display(token string, args internal.Arguments) error {
tok := strings.TrimSpace(token)
store := filepath.Join(internal.GetStore(), tok, totpToken())
if !internal.PathExists(store) {
- return internal.NewLockboxError("object does not exist")
+ return errors.New("object does not exist")
}
l, err := internal.NewLockbox(internal.LockboxOptions{File: store})
if err != nil {
@@ -172,7 +173,7 @@ func display(token string, args internal.Arguments) error {
func main() {
args := os.Args
if len(args) > 3 || len(args) < 2 {
- internal.Die("subkey required", internal.NewLockboxError("invalid arguments"))
+ internal.Die("subkey required", errors.New("invalid arguments"))
}
cmd := args[1]
options := internal.ParseArgs(cmd)
@@ -189,7 +190,7 @@ func main() {
}
if len(args) == 3 {
if !options.Clip && !options.Short && !options.Once {
- internal.Die("subcommand not supported", internal.NewLockboxError("invalid sub command"))
+ internal.Die("subcommand not supported", errors.New("invalid sub command"))
}
cmd = args[2]
}
diff --git a/cmd/lb/main.go b/cmd/lb/main.go
@@ -2,6 +2,7 @@ package main
import (
"encoding/json"
+ "errors"
"fmt"
"os"
"os/exec"
@@ -32,7 +33,7 @@ type (
func getEntry(store string, args []string, idx int) string {
if len(args) != idx+1 {
- internal.Die("invalid entry given", internal.NewLockboxError("specific entry required"))
+ internal.Die("invalid entry given", errors.New("specific entry required"))
}
return filepath.Join(store, args[idx]) + internal.Extension
}
@@ -48,7 +49,7 @@ func getExecutable() string {
func main() {
args := os.Args
if len(args) < 2 {
- internal.Die("missing arguments", internal.NewLockboxError("requires subcommand"))
+ internal.Die("missing arguments", errors.New("requires subcommand"))
}
command := args[1]
store := internal.GetStore()
@@ -58,7 +59,7 @@ func main() {
searchTerm := ""
if isFind {
if len(args) < 3 {
- internal.Die("find requires an argument to search for", internal.NewLockboxError("search term required"))
+ internal.Die("find requires an argument to search for", errors.New("search term required"))
}
searchTerm = args[2]
}
@@ -81,16 +82,16 @@ func main() {
idx := 2
switch len(args) {
case 2:
- internal.Die("insert missing required arguments", internal.NewLockboxError("entry required"))
+ internal.Die("insert missing required arguments", errors.New("entry required"))
case 3:
case 4:
options = internal.ParseArgs(args[2])
if !options.Multi {
- internal.Die("multi-line insert must be after 'insert'", internal.NewLockboxError("invalid command"))
+ internal.Die("multi-line insert must be after 'insert'", errors.New("invalid command"))
}
idx = 3
default:
- internal.Die("too many arguments", internal.NewLockboxError("insert can only perform one operation"))
+ internal.Die("too many arguments", errors.New("insert can only perform one operation"))
}
isPipe := internal.IsInputFromPipe()
entry := getEntry(store, args, idx)
@@ -123,7 +124,7 @@ func main() {
password = input
}
if password == "" {
- internal.Die("empty password provided", internal.NewLockboxError("password can NOT be empty"))
+ internal.Die("empty password provided", errors.New("password can NOT be empty"))
}
l, err := internal.NewLockbox(internal.LockboxOptions{File: entry})
if err != nil {
@@ -137,7 +138,7 @@ func main() {
case "rm":
entry := getEntry(store, args, 2)
if !internal.PathExists(entry) {
- internal.Die("does not exists", internal.NewLockboxError("can not delete unknown entry"))
+ internal.Die("does not exists", errors.New("can not delete unknown entry"))
}
if confirm("remove entry") {
os.Remove(entry)
@@ -176,7 +177,7 @@ func main() {
isGlob := len(entries) > 1
if isGlob {
if !isShow {
- internal.Die("cannot glob to clipboard", internal.NewLockboxError("bad glob request"))
+ internal.Die("cannot glob to clipboard", errors.New("bad glob request"))
}
sort.Strings(entries)
}
@@ -187,7 +188,7 @@ func main() {
dumpData := []Dump{}
for _, entry := range entries {
if !internal.PathExists(entry) {
- internal.Die("invalid entry", internal.NewLockboxError("entry not found"))
+ internal.Die("invalid entry", errors.New("entry not found"))
}
l, err := internal.NewLockbox(internal.LockboxOptions{File: entry})
if err != nil {
diff --git a/internal/clip.go b/internal/clip.go
@@ -1,6 +1,7 @@
package internal
import (
+ "errors"
"fmt"
"os"
"os/exec"
@@ -36,7 +37,7 @@ func GetClipboardCommand() ([]string, []string, error) {
} else {
if strings.TrimSpace(os.Getenv("WAYLAND_DISPLAY")) == "" {
if strings.TrimSpace(os.Getenv("DISPLAY")) == "" {
- return nil, nil, NewLockboxError("unable to detect linux clipboard mode")
+ return nil, nil, errors.New("unable to detect linux clipboard mode")
}
env = xClipMode
} else {
@@ -44,7 +45,7 @@ func GetClipboardCommand() ([]string, []string, error) {
}
}
default:
- return nil, nil, NewLockboxError("unable to detect clipboard mode")
+ return nil, nil, errors.New("unable to detect clipboard mode")
}
}
switch env {
@@ -57,9 +58,9 @@ func GetClipboardCommand() ([]string, []string, error) {
case wslMode:
return []string{"clip.exe"}, []string{"powershell.exe", "-command", "Get-Clipboard"}, nil
case "off":
- return nil, nil, NewLockboxError("clipboard is turned off")
+ return nil, nil, errors.New("clipboard is turned off")
}
- return nil, nil, NewLockboxError("unable to get clipboard command(s)")
+ return nil, nil, errors.New("unable to get clipboard command(s)")
}
// CopyToClipboard will copy to clipboard, if non-empty will clear later.
diff --git a/internal/encrypt.go b/internal/encrypt.go
@@ -2,6 +2,7 @@ package internal
import (
"crypto/rand"
+ "errors"
"io"
random "math/rand"
"os"
@@ -57,7 +58,7 @@ func newLockbox(key, keyMode, file string) (Lockbox, error) {
useKey = os.Getenv("LOCKBOX_KEY")
}
if useKey == "" {
- return Lockbox{}, NewLockboxError("no key given")
+ return Lockbox{}, errors.New("no key given")
}
b, err := getKey(useKeyMode, useKey)
if err != nil {
@@ -65,11 +66,11 @@ func newLockbox(key, keyMode, file string) (Lockbox, error) {
}
if len(b) == 0 {
- return Lockbox{}, NewLockboxError("key is empty")
+ return Lockbox{}, errors.New("key is empty")
}
if len(b) > keyLength {
- return Lockbox{}, NewLockboxError("key is too large for use")
+ return Lockbox{}, errors.New("key is too large for use")
}
for len(b) < keyLength {
@@ -97,7 +98,7 @@ func getKey(keyMode, name string) ([]byte, error) {
case PlainKeyMode:
data = []byte(name)
default:
- return nil, NewLockboxError("unknown keymode")
+ return nil, errors.New("unknown keymode")
}
return []byte(strings.TrimSpace(string(data))), nil
}
@@ -143,7 +144,7 @@ func (l Lockbox) Decrypt() ([]byte, error) {
copy(nonce[:], encrypted[:nonceLength])
decrypted, ok := secretbox.Open(nil, encrypted[nonceLength:], &nonce, &l.secret)
if !ok {
- return nil, NewLockboxError("decrypt not ok")
+ return nil, errors.New("decrypt not ok")
}
padding := int(decrypted[0])
diff --git a/internal/hooks.go b/internal/hooks.go
@@ -1,6 +1,7 @@
package internal
import (
+ "errors"
"os"
"os/exec"
"path/filepath"
@@ -43,7 +44,7 @@ func Hooks(store string, action HookAction, step HookStep) error {
}
continue
}
- return NewLockboxError("hook is not a file")
+ return errors.New("hook is not a file")
}
return nil
}
diff --git a/internal/utils.go b/internal/utils.go
@@ -3,6 +3,7 @@ package internal
import (
"bufio"
"bytes"
+ "errors"
"fmt"
"io/fs"
"os"
@@ -15,10 +16,6 @@ import (
type (
// Color are terminal colors for dumb terminal coloring.
Color int
- // LockboxError for internal errors for lockbox operations.
- LockboxError struct {
- message string
- }
)
const (
@@ -41,7 +38,7 @@ func isYesNoEnv(defaultValue bool, env string) (bool, error) {
case "yes":
return true, nil
}
- return false, NewLockboxError(fmt.Sprintf("invalid yes/no env value for %s", env))
+ return false, fmt.Errorf("invalid yes/no env value for %s", env)
}
// IsInteractive indicates if running as a user UI experience.
@@ -52,7 +49,7 @@ func IsInteractive() (bool, error) {
// GetColor will retrieve start/end terminal coloration indicators.
func GetColor(color Color) (string, string, error) {
if color != ColorRed {
- return "", "", NewLockboxError("bad color")
+ return "", "", errors.New("bad color")
}
interactive, err := IsInteractive()
if err != nil {
@@ -81,7 +78,7 @@ func GetStore() string {
func List(store string, display bool) ([]string, error) {
var results []string
if !PathExists(store) {
- return nil, NewLockboxError("store does not exists")
+ return nil, errors.New("store does not exists")
}
err := filepath.Walk(store, func(path string, info fs.FileInfo, err error) error {
if err != nil {
@@ -154,7 +151,7 @@ func ConfirmInputsMatch(object string) (string, error) {
return "", err
}
if first != second {
- return "", NewLockboxError(fmt.Sprintf("%s(s) do NOT match", object))
+ return "", fmt.Errorf("%s(s) do NOT match", object)
}
return first, nil
}
@@ -174,16 +171,6 @@ func IsInputFromPipe() bool {
return fileInfo.Mode()&os.ModeCharDevice == 0
}
-// NewLockboxError creates a non-category error.
-func NewLockboxError(message string) error {
- return &LockboxError{message}
-}
-
-// Error gets the error message for a basic error.
-func (err *LockboxError) Error() string {
- return err.message
-}
-
// LogError will log an error to stderr.
func LogError(message string, err error) {
msg := message