commit 0069ea5e52e21fd885487a1d1ff7bdc1185a11ac
parent 9a6874b2cc200946d61f2c40cbf5d1d9de3727b4
Author: Sean Enck <sean@ttypty.com>
Date: Mon, 18 Jul 2022 19:15:57 -0400
refactoring use of env variables a bit
Diffstat:
8 files changed, 50 insertions(+), 22 deletions(-)
diff --git a/cmd/lb-totp/main.go b/cmd/lb-totp/main.go
@@ -33,11 +33,7 @@ func clear() {
}
func totpEnv() string {
- t := os.Getenv("LOCKBOX_TOTP")
- if t == "" {
- t = "totp"
- }
- return t
+ return inputs.EnvOrDefault(inputs.TotpEnv, "totp")
}
func display(token string, args cli.Arguments) error {
@@ -65,10 +61,7 @@ func display(token string, args cli.Arguments) error {
if err != nil {
return err
}
- exe := os.Getenv("LOCKBOX_EXE")
- if exe == "" {
- exe = mainExe
- }
+ exe := inputs.EnvOrDefault(inputs.ExeEnv, mainExe)
totpToken := string(val)
if !interactive {
code, err := otp.GenerateCode(totpToken, time.Now())
diff --git a/cmd/lb/main.go b/cmd/lb/main.go
@@ -163,10 +163,7 @@ func main() {
misc.Die("failed to handle clipboard clear", err)
}
default:
- lib := os.Getenv("LOCKBOX_LIBEXEC")
- if lib == "" {
- lib = libExec
- }
+ lib := inputs.EnvOrDefault(inputs.LibExecEnv, libExec)
if err := subcommands.LibExecCallback(subcommands.LibExecOptions{Directory: lib, Command: command, Args: args[2:]}); err != nil {
misc.Die("subcommand failed", err)
}
diff --git a/internal/encrypt/core.go b/internal/encrypt/core.go
@@ -75,14 +75,14 @@ func NewLockbox(options LockboxOptions) (Lockbox, error) {
func newLockbox(key, keyMode, file string) (Lockbox, error) {
useKeyMode := keyMode
if useKeyMode == "" {
- useKeyMode = os.Getenv("LOCKBOX_KEYMODE")
+ useKeyMode = os.Getenv(inputs.KeyModeEnv)
}
if useKeyMode == "" {
useKeyMode = CommandKeyMode
}
useKey := key
if useKey == "" {
- useKey = os.Getenv("LOCKBOX_KEY")
+ useKey = os.Getenv(inputs.KeyEnv)
}
if useKey == "" {
return Lockbox{}, errors.New("no key given")
diff --git a/internal/hooks/execute.go b/internal/hooks/execute.go
@@ -6,6 +6,7 @@ import (
"os/exec"
"path/filepath"
+ "github.com/enckse/lockbox/internal/inputs"
"github.com/enckse/lockbox/internal/misc"
)
@@ -27,7 +28,7 @@ const (
// Run executes any configured hooks.
func Run(action Action, step Step) error {
- hookDir := os.Getenv("LOCKBOX_HOOKDIR")
+ hookDir := os.Getenv(inputs.HooksDirEnv)
if !misc.PathExists(hookDir) {
return nil
}
diff --git a/internal/inputs/env.go b/internal/inputs/env.go
@@ -6,6 +6,40 @@ import (
"strings"
)
+const (
+ prefixKey = "LOCKBOX_"
+ noClipEnv = prefixKey + "NOCLIP"
+ noColorEnv = prefixKey + "NOCOLOR"
+ interactiveEnv = prefixKey + "INTERACTIVE"
+ // TotpEnv allows for overriding of the special name for totp entries.
+ TotpEnv = prefixKey + "TOTP"
+ // ExeEnv allows for installing lb to various locations.
+ ExeEnv = prefixKey + "EXE"
+ // KeyModeEnv indicates what the KEY value is (e.g. command, plaintext).
+ KeyModeEnv = prefixKey + "KEYMODE"
+ // KeyEnv is the key value used by the lockbox store.
+ KeyEnv = prefixKey + "KEY"
+ // LibExecEnv is the location of libexec files for callbacks to internal exes.
+ LibExecEnv = prefixKey + "LIBEXEC"
+ // HooksDirEnv is the location of hooks to run before/after operations.
+ HooksDirEnv = prefixKey + "HOOKDIR"
+ // PlatformEnv is the platform lb is running on.
+ PlatformEnv = prefixKey + "PLATFORM"
+ // StoreEnv is the location of the filesystem store that lb is operating on.
+ StoreEnv = prefixKey + "STORE"
+ // ClipMaxEnv is the max time a value should be stored in the clipboard.
+ ClipMaxEnv = prefixKey + "CLIPMAX"
+)
+
+// 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
+}
+
func isYesNoEnv(defaultValue bool, env string) (bool, error) {
value := strings.ToLower(strings.TrimSpace(os.Getenv(env)))
if len(value) == 0 {
@@ -22,15 +56,15 @@ func isYesNoEnv(defaultValue bool, env string) (bool, error) {
// IsNoClipEnabled indicates if clipboard mode is enabled.
func IsNoClipEnabled() (bool, error) {
- return isYesNoEnv(false, "LOCKBOX_NOCLIP")
+ return isYesNoEnv(false, noClipEnv)
}
// IsNoColorEnabled indicates if the flag is set to disable color.
func IsNoColorEnabled() (bool, error) {
- return isYesNoEnv(false, "LOCKBOX_NOCOLOR")
+ return isYesNoEnv(false, noColorEnv)
}
// IsInteractive indicates if running as a user UI experience.
func IsInteractive() (bool, error) {
- return isYesNoEnv(true, "LOCKBOX_INTERACTIVE")
+ return isYesNoEnv(true, interactiveEnv)
}
diff --git a/internal/platform/clipboard.go b/internal/platform/clipboard.go
@@ -39,7 +39,7 @@ func NewClipboard() (Clipboard, error) {
return Clipboard{}, err
}
max := maxTime
- useMax := os.Getenv("LOCKBOX_CLIPMAX")
+ useMax := os.Getenv(inputs.ClipMaxEnv)
if useMax != "" {
i, err := strconv.Atoi(useMax)
if err != nil {
diff --git a/internal/platform/core.go b/internal/platform/core.go
@@ -5,6 +5,8 @@ import (
"os"
"os/exec"
"strings"
+
+ "github.com/enckse/lockbox/internal/inputs"
)
type (
@@ -27,7 +29,7 @@ const (
// NewPlatform gets a new system platform.
func NewPlatform() (System, error) {
- env := os.Getenv("LOCKBOX_PLATFORM")
+ env := os.Getenv(inputs.PlatformEnv)
if env != "" {
switch env {
case string(MacOS):
diff --git a/internal/store/filesystem.go b/internal/store/filesystem.go
@@ -8,6 +8,7 @@ import (
"sort"
"strings"
+ "github.com/enckse/lockbox/internal/inputs"
"github.com/enckse/lockbox/internal/misc"
)
@@ -32,7 +33,7 @@ type (
// NewFileSystemStore gets the lockbox directory (filesystem-based) store.
func NewFileSystemStore() FileSystem {
- return FileSystem{path: os.Getenv("LOCKBOX_STORE")}
+ return FileSystem{path: os.Getenv(inputs.StoreEnv)}
}
// List will get all lockbox files in a store.