commit e0ed820c2ac0e2082423a1da8ddac045e5de6e96
parent 769ee71a046ae2817afd7ed5f662cccda232eac0
Author: Sean Enck <sean@ttypty.com>
Date: Sun, 8 Dec 2024 17:51:18 -0500
merging fields into set flags
Diffstat:
3 files changed, 59 insertions(+), 45 deletions(-)
diff --git a/internal/config/core.go b/internal/config/core.go
@@ -34,6 +34,13 @@ const (
unset = "(unset)"
)
+const (
+ canDefaultFlag = iota
+ canExpandFlag
+ isArrayFlag
+ isCommandFlag
+)
+
var (
exampleColorWindows = []string{fmt.Sprintf("[%s]", strings.Join([]string{exampleColorWindow, exampleColorWindow, exampleColorWindow + "..."}, util.TimeWindowDelimiter))}
configDirFile = filepath.Join("lockbox", "config.toml")
@@ -61,7 +68,8 @@ var (
)
type (
- printer interface {
+ stringsFlags int
+ printer interface {
display() metaData
self() environmentBase
}
diff --git a/internal/config/env.go b/internal/config/env.go
@@ -3,6 +3,7 @@ package config
import (
"fmt"
+ "slices"
"strings"
"github.com/seanenck/lockbox/internal/config/store"
@@ -31,10 +32,8 @@ type (
// EnvironmentStrings are string-based settings
EnvironmentStrings struct {
environmentDefault[string]
- canDefault bool
- allowed []string
- isArray bool
- canExpand bool
+ flags []stringsFlags
+ allowed []string
}
// EnvironmentFormatter allows for sending a string into a get request
EnvironmentFormatter struct {
@@ -91,18 +90,22 @@ func (e EnvironmentInt) Get() (int64, error) {
func (e EnvironmentStrings) Get() string {
val, ok := store.GetString(e.Key())
if !ok {
- if !e.canDefault {
- return ""
+ flags := e.flattenFlags()
+ if slices.Contains(flags, canDefaultFlag) {
+ val = e.value
}
- val = e.value
}
return val
}
+// AsArray indicates the item should be queried as an array
func (e EnvironmentStrings) AsArray() []string {
val, ok := store.GetArray(e.Key())
- if !ok && e.canDefault {
- val = []string{e.value}
+ if !ok {
+ flags := e.flattenFlags()
+ if slices.Contains(flags, canDefaultFlag) {
+ val = []string{e.value}
+ }
}
return val
}
@@ -118,10 +121,12 @@ func (e EnvironmentStrings) display() metaData {
v := "\"\""
show := e.allowed
value := e.value
- if e.isArray {
+ flags := e.flattenFlags()
+ canExpand := slices.Contains(flags, canExpandFlag)
+ if slices.Contains(flags, isArrayFlag) {
t = tomlArray
v = "[]"
- if e.canExpand {
+ if canExpand {
if len(show) == 0 {
show = []string{"[cmd args...]"}
}
@@ -135,7 +140,7 @@ func (e EnvironmentStrings) display() metaData {
allowed: show,
tomlType: t,
tomlValue: v,
- canExpand: e.canExpand,
+ canExpand: canExpand,
}
}
@@ -176,3 +181,11 @@ func (e EnvironmentFormatter) display() metaData {
canExpand: false,
}
}
+
+func (e EnvironmentStrings) flattenFlags() []stringsFlags {
+ flags := e.flags
+ if slices.Contains(e.flags, isCommandFlag) {
+ flags = append(flags, canExpandFlag, isArrayFlag)
+ }
+ return flags
+}
diff --git a/internal/config/vars.go b/internal/config/vars.go
@@ -113,8 +113,8 @@ var (
key: totpCategory + "ENTRY",
description: "Entry name to store TOTP tokens within the database.",
}),
- allowed: []string{"<string>"},
- canDefault: true,
+ allowed: []string{"<string>"},
+ flags: []stringsFlags{canDefaultFlag},
})
// EnvPlatform is the platform that the application is running on
EnvPlatform = environmentRegister(
@@ -129,7 +129,6 @@ var (
// EnvStore is the location of the keepass file/store
EnvStore = environmentRegister(
EnvironmentStrings{
- canExpand: true,
environmentDefault: newDefaultedEnvironment("",
environmentBase{
key: "STORE",
@@ -137,18 +136,18 @@ var (
requirement: "must be set",
}),
allowed: []string{fileExample},
+ flags: []stringsFlags{canExpandFlag},
})
// EnvHookDir is the directory of hooks to execute
EnvHookDir = environmentRegister(
EnvironmentStrings{
- canExpand: true,
environmentDefault: newDefaultedEnvironment("",
environmentBase{
key: hookCategory + "DIRECTORY",
description: "The path to hooks to execute on actions against the database.",
}),
- allowed: []string{"<directory>"},
- canDefault: true,
+ allowed: []string{"<directory>"},
+ flags: []stringsFlags{canDefaultFlag, canExpandFlag},
})
// EnvClipCopy allows overriding the clipboard copy command
EnvClipCopy = environmentRegister(EnvironmentStrings{
@@ -157,8 +156,7 @@ var (
key: clipCategory + "COPY_COMMAND",
description: "Override the detected platform copy command.",
}),
- isArray: true,
- canExpand: true,
+ flags: []stringsFlags{isCommandFlag},
})
// EnvClipPaste allows overriding the clipboard paste command
EnvClipPaste = environmentRegister(EnvironmentStrings{
@@ -167,8 +165,7 @@ var (
key: clipCategory + "PASTE_COMMAND",
description: "Override the detected platform paste command.",
}),
- isArray: true,
- canExpand: true,
+ flags: []stringsFlags{isCommandFlag},
})
// EnvTOTPColorBetween handles terminal coloring for TOTP windows (seconds)
EnvTOTPColorBetween = environmentRegister(
@@ -180,22 +177,20 @@ var (
must be a list of one (or more) rules where a '%s' delimits the start and end second (0-60 for each),
and '%s' allows for multiple windows.`, util.TimeWindowSpan, util.TimeWindowDelimiter),
}),
- isArray: true,
- canDefault: true,
- allowed: exampleColorWindows,
+ flags: []stringsFlags{isArrayFlag, canDefaultFlag},
+ allowed: exampleColorWindows,
})
// EnvKeyFile is an keyfile for the database
EnvKeyFile = environmentRegister(
EnvironmentStrings{
- canExpand: true,
environmentDefault: newDefaultedEnvironment("",
environmentBase{
key: credsCategory + "KEY_FILE",
requirement: requiredKeyOrKeyFile,
description: "A keyfile to access/protect the database.",
}),
- allowed: []string{"keyfile"},
- canDefault: true,
+ allowed: []string{"keyfile"},
+ flags: []stringsFlags{canDefaultFlag, canExpandFlag},
})
// EnvDefaultModTime is modtime override ability for entries
EnvDefaultModTime = environmentRegister(
@@ -205,8 +200,8 @@ and '%s' allows for multiple windows.`, util.TimeWindowSpan, util.TimeWindowDeli
key: defaultCategory + "MODTIME",
description: fmt.Sprintf("Input modification time to set for the entry\n\nExpected format: %s.", ModTimeFormat),
}),
- canDefault: true,
- allowed: []string{"modtime"},
+ flags: []stringsFlags{canDefaultFlag},
+ allowed: []string{"modtime"},
})
// EnvJSONMode controls how JSON is output in the 'data' field
EnvJSONMode = environmentRegister(
@@ -216,8 +211,8 @@ and '%s' allows for multiple windows.`, util.TimeWindowSpan, util.TimeWindowDeli
key: jsonCategory + "MODE",
description: fmt.Sprintf("Changes what the data field in JSON outputs will contain.\n\nUse '%s' with CAUTION.", output.JSONModes.Raw),
}),
- canDefault: true,
- allowed: output.JSONModes.List(),
+ flags: []stringsFlags{canDefaultFlag},
+ allowed: output.JSONModes.List(),
})
// EnvTOTPFormat supports formatting the TOTP tokens for generation of tokens
EnvTOTPFormat = environmentRegister(EnvironmentFormatter{environmentBase: environmentBase{
@@ -234,12 +229,11 @@ and '%s' allows for multiple windows.`, util.TimeWindowSpan, util.TimeWindowDeli
description: fmt.Sprintf(`How to retrieve the database store password. Set to '%s' when only using a key file.
Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
}),
- allowed: []string{string(AskKeyMode), string(commandKeyMode), string(IgnoreKeyMode), string(noKeyMode), string(plainKeyMode)},
- canDefault: true,
+ allowed: []string{string(AskKeyMode), string(commandKeyMode), string(IgnoreKeyMode), string(noKeyMode), string(plainKeyMode)},
+ flags: []stringsFlags{canDefaultFlag},
})
envPassword = environmentRegister(
EnvironmentStrings{
- canExpand: true,
environmentDefault: newDefaultedEnvironment(unset,
environmentBase{
requirement: requiredKeyOrKeyFile,
@@ -249,7 +243,7 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
commandKeyMode),
}),
allowed: []string{commandArgsExample, "password"},
- isArray: true,
+ flags: []stringsFlags{isArrayFlag, canExpandFlag},
})
// EnvPasswordGenWordCount is the number of words that will be selected for password generation
EnvPasswordGenWordCount = environmentRegister(
@@ -278,8 +272,8 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
key: genCategory + "TEMPLATE",
description: fmt.Sprintf("The go text template to use to format the chosen words into a password. Available fields: %s.", util.TextPositionFields()),
}),
- allowed: []string{"<go template>"},
- canDefault: true,
+ allowed: []string{"<go template>"},
+ flags: []stringsFlags{canDefaultFlag},
})
// EnvPasswordGenWordList is the command text to generate the word list
EnvPasswordGenWordList = environmentRegister(EnvironmentStrings{
@@ -288,8 +282,7 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
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,
+ flags: []stringsFlags{isCommandFlag},
})
// EnvLanguage is the language to use for everything
EnvLanguage = environmentRegister(
@@ -299,8 +292,8 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
key: "LANGUAGE",
description: "Language to run under.",
}),
- allowed: []string{"<language code>"},
- canDefault: true,
+ allowed: []string{"<language code>"},
+ flags: []stringsFlags{canDefaultFlag},
})
// EnvPasswordGenEnabled indicates if password generation is enabled
EnvPasswordGenEnabled = environmentRegister(
@@ -319,7 +312,7 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode),
key: genCategory + "CHARACTERS",
description: "The set of allowed characters in output words (empty means any character is allowed).",
}),
- allowed: []string{"<list of characters>"},
- canDefault: true,
+ allowed: []string{"<list of characters>"},
+ flags: []stringsFlags{canDefaultFlag},
})
)