commit dc0e3e7f0f810b4b2aad325180dc872f2401c75d
parent cd5455500c103ca61b9a8a95c8b75bfb22e0eb04
Author: Sean Enck <sean@ttypty.com>
Date: Fri, 31 Mar 2023 19:04:42 -0400
pgl update
Diffstat:
8 files changed, 34 insertions(+), 39 deletions(-)
diff --git a/go.mod b/go.mod
@@ -4,7 +4,7 @@ go 1.19
require (
github.com/aymanbagabas/go-osc52 v1.2.2
- github.com/enckse/pgl v1.0.7
+ github.com/enckse/pgl v1.0.8
github.com/pquerna/otp v1.4.0
github.com/tobischo/gokeepasslib/v3 v3.5.0
mvdan.cc/sh/v3 v3.6.0
diff --git a/go.sum b/go.sum
@@ -9,8 +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 v1.0.7 h1:bfhCYhxwm+gUVaQBvoe1JbbdjSUkl5Mqbeg2n06M65c=
-github.com/enckse/pgl v1.0.7/go.mod h1:r5bqGzwqnJIeY6UbGT5u38keJ5+ZySlsWeaYYzdBhMg=
+github.com/enckse/pgl v1.0.8 h1:QE2p3FWr8XCdlkxSK/N2nd4zXcLljFYctrWTzRFpb6A=
+github.com/enckse/pgl v1.0.8/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=
diff --git a/internal/backend/actions.go b/internal/backend/actions.go
@@ -10,6 +10,7 @@ import (
"time"
"github.com/enckse/lockbox/internal/inputs"
+ "github.com/enckse/pgl/os/env"
"github.com/tobischo/gokeepasslib/v3"
"github.com/tobischo/gokeepasslib/v3/wrappers"
)
@@ -19,7 +20,7 @@ func NewHook(path string, a ActionMode) (Hook, error) {
if strings.TrimSpace(path) == "" {
return Hook{}, errors.New("empty path is not allowed for hooks")
}
- dir := inputs.EnvOrDefault(inputs.HookDirEnv, "")
+ dir := env.GetOrDefault(inputs.HookDirEnv, "")
if dir == "" {
return Hook{enabled: false}, nil
}
@@ -65,7 +66,7 @@ func (t *Transaction) act(cb action) error {
return err
}
k := string(key)
- file := inputs.EnvOrDefault(inputs.KeyFileEnv, "")
+ file := env.GetOrDefault(inputs.KeyFileEnv, "")
if !t.exists {
if err := create(t.file, k, file); err != nil {
return err
@@ -225,7 +226,7 @@ func (t *Transaction) Move(src QueryEntity, dst string) error {
if strings.TrimSpace(src.Value) == "" {
return errors.New("empty secret not allowed")
}
- mod := inputs.EnvOrDefault(inputs.ModTimeEnv, "")
+ mod := env.GetOrDefault(inputs.ModTimeEnv, "")
modTime := time.Now()
if mod != "" {
p, err := time.Parse(inputs.ModTimeFormat, mod)
diff --git a/internal/inputs/env.go b/internal/inputs/env.go
@@ -12,6 +12,7 @@ import (
"strings"
"time"
+ "github.com/enckse/pgl/os/env"
"mvdan.cc/sh/v3/shell"
)
@@ -44,8 +45,6 @@ const (
// ClipCopyEnv allows overriding the clipboard copy command
ClipCopyEnv = clipBaseEnv + "COPY"
clipOSC52Env = clipBaseEnv + "OSC52"
- isYes = "yes"
- isNo = "no"
defaultTOTPField = "totp"
commandArgsExample = "[cmd args...]"
// MacOSPlatform is the macos indicator for platform
@@ -69,7 +68,7 @@ const (
MaxTOTPTimeDefault = "120"
)
-var isYesNoArgs = []string{isYes, isNo}
+var isYesNoArgs = []string{env.Yes, env.No}
type (
environmentOutput struct {
@@ -119,15 +118,6 @@ func GetReKey(args []string) ([]string, error) {
return out, nil
}
-// EnvOrDefault will get the environment value OR default if env is not set.
-func EnvOrDefault(envKey, defaultValue string) string {
- val := os.Getenv(envKey)
- if val == "" {
- return defaultValue
- }
- return val
-}
-
// GetClipboardMax will get max time to keep an entry in the clipboard before clearing
func GetClipboardMax() (int, error) {
max := defaultMaxClipboard
@@ -192,18 +182,18 @@ func getKey(keyMode, name string) ([]byte, error) {
return []byte(strings.TrimSpace(string(data))), nil
}
-func isYesNoEnv(defaultValue bool, env string) (bool, error) {
- value := strings.ToLower(strings.TrimSpace(os.Getenv(env)))
- if len(value) == 0 {
- return defaultValue, nil
- }
- switch value {
- case isNo:
+func isYesNoEnv(defaultValue bool, envKey string) (bool, error) {
+ read := env.GetValue(envKey)
+ switch read {
+ case env.NoValue:
return false, nil
- case isYes:
+ case env.YesValue:
return true, nil
+ case env.UnknownValue:
+ return defaultValue, nil
}
- return false, fmt.Errorf("invalid yes/no env value for %s", env)
+
+ return false, fmt.Errorf("invalid yes/no env value for %s", envKey)
}
// IsClipOSC52 indicates if OSC52 mode is enabled
@@ -238,7 +228,7 @@ func IsInteractive() (bool, error) {
// TOTPToken gets the name of the totp special case tokens
func TOTPToken() string {
- return EnvOrDefault(fieldTOTPEnv, defaultTOTPField)
+ return env.GetOrDefault(fieldTOTPEnv, defaultTOTPField)
}
func (o environmentOutput) formatEnvironmentVariable(required bool, name, val, desc string, allowed []string) string {
@@ -270,10 +260,10 @@ func ListEnvironmentVariables(showValues bool) []string {
results = append(results, e.formatEnvironmentVariable(true, StoreEnv, "", "directory to the database file", []string{"file"}))
results = append(results, e.formatEnvironmentVariable(true, keyModeEnv, commandKeyMode, "how to retrieve the database store password", []string{commandKeyMode, plainKeyMode}))
results = append(results, e.formatEnvironmentVariable(true, keyEnv, "", fmt.Sprintf("the database key ('%s' mode) or command to run ('%s' mode)\nto retrieve the database password", plainKeyMode, commandKeyMode), []string{commandArgsExample, "password"}))
- results = append(results, e.formatEnvironmentVariable(false, noClipEnv, isNo, "disable clipboard operations", isYesNoArgs))
- results = append(results, e.formatEnvironmentVariable(false, noColorEnv, isNo, "disable terminal colors", isYesNoArgs))
- results = append(results, e.formatEnvironmentVariable(false, interactiveEnv, isYes, "enable interactive mode", isYesNoArgs))
- results = append(results, e.formatEnvironmentVariable(false, readOnlyEnv, isNo, "operate in readonly mode", isYesNoArgs))
+ results = append(results, e.formatEnvironmentVariable(false, noClipEnv, env.No, "disable clipboard operations", isYesNoArgs))
+ results = append(results, e.formatEnvironmentVariable(false, noColorEnv, env.No, "disable terminal colors", isYesNoArgs))
+ results = append(results, e.formatEnvironmentVariable(false, interactiveEnv, env.Yes, "enable interactive mode", isYesNoArgs))
+ results = append(results, e.formatEnvironmentVariable(false, readOnlyEnv, env.No, "operate in readonly mode", isYesNoArgs))
results = append(results, e.formatEnvironmentVariable(false, fieldTOTPEnv, defaultTOTPField, "attribute name to store TOTP tokens within the database", []string{"string"}))
results = append(results, e.formatEnvironmentVariable(false, formatTOTPEnv, strings.ReplaceAll(strings.ReplaceAll(FormatTOTP("%s"), "%25s", "%s"), "&", " \\\n &"), "override the otpauth url used to store totp tokens. It must have ONE format\nstring ('%s') to insert the totp base code", []string{"otpauth//url/%s/args..."}))
results = append(results, e.formatEnvironmentVariable(false, MaxTOTPTime, MaxTOTPTimeDefault, "time, in seconds, in which to show a TOTP token before automatically exiting", []string{"integer"}))
@@ -282,9 +272,9 @@ func ListEnvironmentVariables(showValues bool) []string {
results = append(results, e.formatEnvironmentVariable(false, ClipCopyEnv, detectedValue, "override the detected platform copy command", []string{commandArgsExample}))
results = append(results, e.formatEnvironmentVariable(false, clipMaxEnv, fmt.Sprintf("%d", defaultMaxClipboard), "override the amount of time before totp clears the clipboard (e.g. 10),\nmust be an integer", []string{"integer"}))
results = append(results, e.formatEnvironmentVariable(false, PlatformEnv, detectedValue, "override the detected platform", PlatformSet()))
- results = append(results, e.formatEnvironmentVariable(false, noTOTPEnv, isNo, "disable TOTP integrations", isYesNoArgs))
+ results = append(results, e.formatEnvironmentVariable(false, noTOTPEnv, env.No, "disable TOTP integrations", isYesNoArgs))
results = append(results, e.formatEnvironmentVariable(false, HookDirEnv, "", "the path to hooks to execute on actions against the database", []string{"directory"}))
- results = append(results, e.formatEnvironmentVariable(false, clipOSC52Env, isNo, "enable OSC52 clipboard mode", isYesNoArgs))
+ results = append(results, e.formatEnvironmentVariable(false, clipOSC52Env, env.No, "enable OSC52 clipboard mode", isYesNoArgs))
results = append(results, e.formatEnvironmentVariable(false, KeyFileEnv, "", "additional keyfile to access/protect the database", []string{"keyfile"}))
results = append(results, e.formatEnvironmentVariable(false, ModTimeEnv, ModTimeFormat, fmt.Sprintf("input modification time to set for the entry\n(expected format: %s)", ModTimeFormat), []string{"modtime"}))
return results
diff --git a/internal/inputs/env_test.go b/internal/inputs/env_test.go
@@ -34,7 +34,7 @@ func checkYesNo(key string, t *testing.T, cb func() (bool, error), onEmpty bool)
if c {
t.Error("invalid setting")
}
- os.Setenv(key, "lkaj;f")
+ os.Setenv(key, "true")
_, err = cb()
if err == nil || err.Error() != fmt.Sprintf("invalid yes/no env value for %s", key) {
t.Errorf("unexpected error: %v", err)
diff --git a/internal/inputs/totp.go b/internal/inputs/totp.go
@@ -5,6 +5,8 @@ import (
"fmt"
"net/url"
"strings"
+
+ "github.com/enckse/pgl/os/env"
)
const (
@@ -17,7 +19,7 @@ func FormatTOTP(value string) string {
if strings.HasPrefix(value, otpAuth) {
return value
}
- override := EnvOrDefault(formatTOTPEnv, "")
+ override := env.GetOrDefault(formatTOTPEnv, "")
if override != "" {
return fmt.Sprintf(override, value)
}
diff --git a/internal/platform/clipboard.go b/internal/platform/clipboard.go
@@ -10,6 +10,7 @@ import (
osc "github.com/aymanbagabas/go-osc52"
"github.com/enckse/lockbox/internal/inputs"
+ "github.com/enckse/pgl/os/env"
)
type (
@@ -31,7 +32,7 @@ func newClipboard(copying, pasting []string) (Clipboard, error) {
}
func overrideCommand(v string) ([]string, error) {
- value := inputs.EnvOrDefault(v, "")
+ value := env.GetOrDefault(v, "")
if strings.TrimSpace(value) == "" {
return nil, nil
}
diff --git a/internal/totp/core.go b/internal/totp/core.go
@@ -16,6 +16,7 @@ import (
"github.com/enckse/lockbox/internal/colors"
"github.com/enckse/lockbox/internal/inputs"
"github.com/enckse/lockbox/internal/platform"
+ "github.com/enckse/pgl/os/env"
coreotp "github.com/pquerna/otp"
otp "github.com/pquerna/otp/totp"
)
@@ -85,7 +86,7 @@ func clear() {
}
func colorWhenRules() ([]inputs.ColorWindow, error) {
- envTime := inputs.EnvOrDefault(inputs.ColorBetweenEnv, inputs.TOTPDefaultBetween)
+ envTime := env.GetOrDefault(inputs.ColorBetweenEnv, inputs.TOTPDefaultBetween)
if envTime == inputs.TOTPDefaultBetween {
return inputs.TOTPDefaultColorWindow, nil
}
@@ -159,7 +160,7 @@ func (args *Arguments) display(opts Options) error {
if err != nil {
return err
}
- runString := inputs.EnvOrDefault(inputs.MaxTOTPTime, inputs.MaxTOTPTimeDefault)
+ runString := env.GetOrDefault(inputs.MaxTOTPTime, inputs.MaxTOTPTimeDefault)
runFor, err := strconv.Atoi(runString)
if err != nil {
return err