commit 769ee71a046ae2817afd7ed5f662cccda232eac0
parent 819627a83a2ba898993f0ce03b362f657f6d80a3
Author: Sean Enck <sean@ttypty.com>
Date: Sun, 8 Dec 2024 17:34:42 -0500
merge env types
Diffstat:
7 files changed, 73 insertions(+), 69 deletions(-)
diff --git a/internal/app/pwgen.go b/internal/app/pwgen.go
@@ -31,7 +31,7 @@ func GeneratePassword(cmd CommandOptions) error {
return fmt.Errorf("word count must be >= 1")
}
tmplString := config.EnvPasswordGenTemplate.Get()
- wordList := config.EnvPasswordGenWordList.Get()
+ wordList := config.EnvPasswordGenWordList.AsArray()
if len(wordList) == 0 {
return errors.New("word list command must set")
}
diff --git a/internal/config/core.go b/internal/config/core.go
@@ -31,6 +31,7 @@ const (
ModTimeFormat = time.RFC3339
exampleColorWindow = "start" + util.TimeWindowSpan + "end"
detectedValue = "(detected)"
+ unset = "(unset)"
)
var (
diff --git a/internal/config/env.go b/internal/config/env.go
@@ -28,18 +28,14 @@ type (
EnvironmentBool struct {
environmentDefault[bool]
}
- // EnvironmentString are string-based settings
- EnvironmentString struct {
+ // EnvironmentStrings are string-based settings
+ EnvironmentStrings struct {
environmentDefault[string]
canDefault bool
allowed []string
isArray bool
canExpand bool
}
- // EnvironmentArray are settings that are parsed as shell commands
- EnvironmentArray struct {
- environmentBase
- }
// EnvironmentFormatter allows for sending a string into a get request
EnvironmentFormatter struct {
environmentBase
@@ -92,7 +88,7 @@ func (e EnvironmentInt) Get() (int64, error) {
}
// Get will read the string from the environment
-func (e EnvironmentString) Get() string {
+func (e EnvironmentStrings) Get() string {
val, ok := store.GetString(e.Key())
if !ok {
if !e.canDefault {
@@ -103,11 +99,10 @@ func (e EnvironmentString) Get() string {
return val
}
-// Get will read (and shlex) the value if set
-func (e EnvironmentArray) Get() []string {
+func (e EnvironmentStrings) AsArray() []string {
val, ok := store.GetArray(e.Key())
- if !ok {
- return []string{}
+ if !ok && e.canDefault {
+ val = []string{e.value}
}
return val
}
@@ -117,17 +112,27 @@ func (e EnvironmentFormatter) Get(value string) string {
return e.fxn(e.Key(), value)
}
-func (e EnvironmentString) display() metaData {
+func (e EnvironmentStrings) display() metaData {
var t tomlType
t = tomlString
v := "\"\""
+ show := e.allowed
+ value := e.value
if e.isArray {
t = tomlArray
v = "[]"
+ if e.canExpand {
+ if len(show) == 0 {
+ show = []string{"[cmd args...]"}
+ }
+ if value == "" {
+ value = "(detected)"
+ }
+ }
}
return metaData{
- value: e.value,
- allowed: e.allowed,
+ value: value,
+ allowed: show,
tomlType: t,
tomlValue: v,
canExpand: e.canExpand,
@@ -171,13 +176,3 @@ func (e EnvironmentFormatter) display() metaData {
canExpand: false,
}
}
-
-func (e EnvironmentArray) display() metaData {
- return metaData{
- value: detectedValue,
- allowed: []string{commandArgsExample},
- tomlType: tomlArray,
- tomlValue: "[]",
- canExpand: true,
- }
-}
diff --git a/internal/config/key.go b/internal/config/key.go
@@ -6,8 +6,6 @@ import (
"fmt"
"os/exec"
"strings"
-
- "github.com/seanenck/lockbox/internal/config/store"
)
type (
@@ -57,8 +55,8 @@ func NewKey(defaultKeyModeType KeyModeType) (Key, error) {
default:
return Key{}, fmt.Errorf("unknown key mode: %s", keyMode)
}
- useKey, ok := store.GetArray(envPassword.Key())
- isEmpty := !ok || len(useKey) == 0
+ useKey := envPassword.AsArray()
+ isEmpty := len(useKey) == 0
if !isEmpty {
if strings.TrimSpace(useKey[0]) == "" {
isEmpty = true
diff --git a/internal/config/toml.go b/internal/config/toml.go
@@ -105,7 +105,7 @@ func generateDetailText(data printer) (string, error) {
md := data.display()
value := md.value
if len(value) == 0 {
- value = "(unset)"
+ value = unset
}
key := env.Key()
description := strings.TrimSpace(util.TextWrap(2, env.description))
diff --git a/internal/config/vars.go b/internal/config/vars.go
@@ -18,8 +18,7 @@ var (
key: clipCategory + "TIMEOUT",
description: "Override the amount of time before totp clears the clipboard (seconds).",
}),
- short: "clipboard max time",
- canZero: false,
+ short: "clipboard max time",
})
// EnvJSONHashLength handles the hashing output length
EnvJSONHashLength = environmentRegister(
@@ -108,7 +107,7 @@ var (
})
// EnvTOTPEntry is the leaf token to use to store TOTP tokens
EnvTOTPEntry = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
environmentDefault: newDefaultedEnvironment("totp",
environmentBase{
key: totpCategory + "ENTRY",
@@ -119,18 +118,17 @@ var (
})
// EnvPlatform is the platform that the application is running on
EnvPlatform = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
environmentDefault: newDefaultedEnvironment(detectedValue,
environmentBase{
key: "PLATFORM",
description: "Override the detected platform.",
}),
- allowed: platform.Systems.List(),
- canDefault: false,
+ allowed: platform.Systems.List(),
})
// EnvStore is the location of the keepass file/store
EnvStore = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
canExpand: true,
environmentDefault: newDefaultedEnvironment("",
environmentBase{
@@ -138,12 +136,11 @@ var (
description: "Directory to the database file.",
requirement: "must be set",
}),
- canDefault: false,
- allowed: []string{fileExample},
+ allowed: []string{fileExample},
})
// EnvHookDir is the directory of hooks to execute
EnvHookDir = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
canExpand: true,
environmentDefault: newDefaultedEnvironment("",
environmentBase{
@@ -154,18 +151,28 @@ var (
canDefault: true,
})
// EnvClipCopy allows overriding the clipboard copy command
- EnvClipCopy = environmentRegister(EnvironmentArray{environmentBase: environmentBase{
- key: clipCategory + "COPY_COMMAND",
- description: "Override the detected platform copy command.",
- }})
+ EnvClipCopy = environmentRegister(EnvironmentStrings{
+ environmentDefault: newDefaultedEnvironment("",
+ environmentBase{
+ key: clipCategory + "COPY_COMMAND",
+ description: "Override the detected platform copy command.",
+ }),
+ isArray: true,
+ canExpand: true,
+ })
// EnvClipPaste allows overriding the clipboard paste command
- EnvClipPaste = environmentRegister(EnvironmentArray{environmentBase: environmentBase{
- key: clipCategory + "PASTE_COMMAND",
- description: "Override the detected platform paste command.",
- }})
+ EnvClipPaste = environmentRegister(EnvironmentStrings{
+ environmentDefault: newDefaultedEnvironment("",
+ environmentBase{
+ key: clipCategory + "PASTE_COMMAND",
+ description: "Override the detected platform paste command.",
+ }),
+ isArray: true,
+ canExpand: true,
+ })
// EnvTOTPColorBetween handles terminal coloring for TOTP windows (seconds)
EnvTOTPColorBetween = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
environmentDefault: newDefaultedEnvironment(TOTPDefaultBetween,
environmentBase{
key: totpCategory + "COLOR_WINDOWS",
@@ -179,7 +186,7 @@ and '%s' allows for multiple windows.`, util.TimeWindowSpan, util.TimeWindowDeli
})
// EnvKeyFile is an keyfile for the database
EnvKeyFile = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
canExpand: true,
environmentDefault: newDefaultedEnvironment("",
environmentBase{
@@ -192,7 +199,7 @@ and '%s' allows for multiple windows.`, util.TimeWindowSpan, util.TimeWindowDeli
})
// EnvDefaultModTime is modtime override ability for entries
EnvDefaultModTime = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
environmentDefault: newDefaultedEnvironment("",
environmentBase{
key: defaultCategory + "MODTIME",
@@ -203,7 +210,7 @@ and '%s' allows for multiple windows.`, util.TimeWindowSpan, util.TimeWindowDeli
})
// EnvJSONMode controls how JSON is output in the 'data' field
EnvJSONMode = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
environmentDefault: newDefaultedEnvironment(string(output.JSONModes.Hash),
environmentBase{
key: jsonCategory + "MODE",
@@ -219,7 +226,7 @@ and '%s' allows for multiple windows.`, util.TimeWindowSpan, util.TimeWindowDeli
}, fxn: formatterTOTP, allowed: "otpauth//url/%s/args..."})
// EnvPasswordMode indicates how the password is read
EnvPasswordMode = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
environmentDefault: newDefaultedEnvironment(string(DefaultKeyMode),
environmentBase{
key: credsCategory + "PASSWORD_MODE",
@@ -231,9 +238,9 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
canDefault: true,
})
envPassword = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
canExpand: true,
- environmentDefault: newDefaultedEnvironment("",
+ environmentDefault: newDefaultedEnvironment(unset,
environmentBase{
requirement: requiredKeyOrKeyFile,
key: credsCategory + "PASSWORD",
@@ -241,9 +248,8 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
plainKeyMode,
commandKeyMode),
}),
- isArray: true,
- allowed: []string{commandArgsExample, "password"},
- canDefault: false,
+ allowed: []string{commandArgsExample, "password"},
+ isArray: true,
})
// EnvPasswordGenWordCount is the number of words that will be selected for password generation
EnvPasswordGenWordCount = environmentRegister(
@@ -253,8 +259,7 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
key: genCategory + "WORD_COUNT",
description: "Number of words to select and include in the generated password.",
}),
- short: "word count",
- canZero: false,
+ short: "word count",
})
// EnvPasswordGenTitle indicates if titling (e.g. uppercasing) will occur to words
EnvPasswordGenTitle = environmentRegister(
@@ -267,7 +272,7 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
})
// EnvPasswordGenTemplate is the output template for controlling how output words are placed together
EnvPasswordGenTemplate = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
environmentDefault: newDefaultedEnvironment("{{range $i, $val := .}}{{if $i}}-{{end}}{{$val.Text}}{{end}}",
environmentBase{
key: genCategory + "TEMPLATE",
@@ -277,13 +282,18 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
canDefault: true,
})
// EnvPasswordGenWordList is the command text to generate the word list
- EnvPasswordGenWordList = environmentRegister(EnvironmentArray{environmentBase: environmentBase{
- key: genCategory + "WORDS_COMMAND",
- description: "Command to retrieve the word list to use for password generation (must be split by newline).",
- }})
+ EnvPasswordGenWordList = environmentRegister(EnvironmentStrings{
+ environmentDefault: newDefaultedEnvironment("",
+ environmentBase{
+ key: genCategory + "WORDS_COMMAND",
+ description: "Command to retrieve the word list to use for password generation (must be split by newline).",
+ }),
+ isArray: true,
+ canExpand: true,
+ })
// EnvLanguage is the language to use for everything
EnvLanguage = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
environmentDefault: newDefaultedEnvironment("en-US",
environmentBase{
key: "LANGUAGE",
@@ -303,7 +313,7 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
})
// EnvPasswordGenChars allows for restricting which characters can be used
EnvPasswordGenChars = environmentRegister(
- EnvironmentString{
+ EnvironmentStrings{
environmentDefault: newDefaultedEnvironment("",
environmentBase{
key: genCategory + "CHARACTERS",
diff --git a/internal/platform/clip/core.go b/internal/platform/clip/core.go
@@ -35,8 +35,8 @@ func New() (Board, error) {
if !config.EnvClipEnabled.Get() {
return Board{}, errors.New("clipboard is off")
}
- overridePaste := config.EnvClipPaste.Get()
- overrideCopy := config.EnvClipCopy.Get()
+ overridePaste := config.EnvClipPaste.AsArray()
+ overrideCopy := config.EnvClipCopy.AsArray()
setPaste := len(overridePaste) > 0
setCopy := len(overrideCopy) > 0
if setPaste && setCopy {