commit 8e40f50a580e0ab8e93252967cdf6d8292df5986
parent 3e8eac170fc70a332691fb36c5c70ec265777be2
Author: Sean Enck <sean@ttypty.com>
Date: Sun, 11 Aug 2024 11:21:46 -0400
rename function for registry
Diffstat:
3 files changed, 32 insertions(+), 27 deletions(-)
diff --git a/internal/config/core.go b/internal/config/core.go
@@ -489,7 +489,7 @@ func wrap(in string, maxLength int) string {
return strings.Join(lines, "\n")
}
-func register[T printer](obj T) T {
+func environmentRegister[T printer](obj T) T {
registry = append(registry, obj)
return obj
}
diff --git a/internal/config/vars.go b/internal/config/vars.go
@@ -39,7 +39,7 @@ var (
// TOTPDefaultBetween is the default color window as a string
TOTPDefaultBetween = toString(TOTPDefaultColorWindow)
// EnvClipMax gets the maximum clipboard time
- EnvClipMax = register(EnvironmentInt{
+ EnvClipMax = environmentRegister(EnvironmentInt{
environmentBase: environmentBase{
subKey: "MAX",
cat: clipCategory,
@@ -48,7 +48,7 @@ var (
shortDesc: "clipboard max time", allowZero: false, defaultValue: 45,
})
// EnvHashLength handles the hashing output length
- EnvHashLength = register(EnvironmentInt{
+ EnvHashLength = environmentRegister(EnvironmentInt{
environmentBase: environmentBase{
subKey: EnvJSONDataOutput.subKey + "_HASH_LENGTH",
desc: fmt.Sprintf("Maximum hash string length the JSON output should contain when '%s' mode is set for JSON output.", JSONDataOutputHash),
@@ -56,13 +56,13 @@ var (
shortDesc: "hash length", allowZero: true, defaultValue: 0,
})
// EnvClipOSC52 indicates if OSC52 clipboard mode is enabled
- EnvClipOSC52 = register(EnvironmentBool{environmentBase: environmentBase{
+ EnvClipOSC52 = environmentRegister(EnvironmentBool{environmentBase: environmentBase{
subKey: "OSC52",
cat: clipCategory,
desc: "Enable OSC52 clipboard mode.",
}, defaultValue: false})
// EnvNoTOTP indicates if TOTP is disabled
- EnvNoTOTP = register(EnvironmentBool{
+ EnvNoTOTP = environmentRegister(EnvironmentBool{
environmentBase: environmentBase{
subKey: "NOTOTP",
desc: "Disable TOTP integrations.",
@@ -70,12 +70,12 @@ var (
defaultValue: false,
})
// EnvReadOnly indicates if in read-only mode
- EnvReadOnly = register(EnvironmentBool{environmentBase: environmentBase{
+ EnvReadOnly = environmentRegister(EnvironmentBool{environmentBase: environmentBase{
subKey: "READONLY",
desc: "Operate in readonly mode.",
}, defaultValue: false})
// EnvNoClip indicates clipboard functionality is off
- EnvNoClip = register(EnvironmentBool{
+ EnvNoClip = environmentRegister(EnvironmentBool{
environmentBase: environmentBase{
subKey: "NOCLIP",
desc: "Disable clipboard operations.",
@@ -83,7 +83,7 @@ var (
defaultValue: false,
})
// EnvDefaultCompletion disable completion detection
- EnvDefaultCompletion = register(EnvironmentBool{
+ EnvDefaultCompletion = environmentRegister(EnvironmentBool{
environmentBase: environmentBase{
subKey: "DEFAULT_COMPLETION",
desc: "Use the default completion set (disable detection).",
@@ -93,60 +93,60 @@ var (
// EnvDefaultCompletionKey is the key for default completion handling
EnvDefaultCompletionKey = EnvDefaultCompletion.key()
// EnvNoColor indicates if color outputs are disabled
- EnvNoColor = register(EnvironmentBool{environmentBase: environmentBase{
+ EnvNoColor = environmentRegister(EnvironmentBool{environmentBase: environmentBase{
subKey: "NOCOLOR",
desc: "Disable terminal colors.",
}, defaultValue: false})
// EnvNoHooks disables hooks
- EnvNoHooks = register(EnvironmentBool{environmentBase: environmentBase{
+ EnvNoHooks = environmentRegister(EnvironmentBool{environmentBase: environmentBase{
subKey: "NOHOOKS",
desc: "Disable hooks",
}, defaultValue: false})
// EnvInteractive indicates if operating in interactive mode
- EnvInteractive = register(EnvironmentBool{environmentBase: environmentBase{
+ EnvInteractive = environmentRegister(EnvironmentBool{environmentBase: environmentBase{
subKey: "INTERACTIVE",
desc: "Enable interactive mode.",
}, defaultValue: true})
// EnvMaxTOTP is the max TOTP time to run (default)
- EnvMaxTOTP = register(EnvironmentInt{environmentBase: environmentBase{
+ EnvMaxTOTP = environmentRegister(EnvironmentInt{environmentBase: environmentBase{
subKey: "MAX",
cat: totpCategory,
desc: "Time, in seconds, in which to show a TOTP token before automatically exiting.",
}, shortDesc: "max totp time", allowZero: false, defaultValue: 120})
// EnvTOTPToken is the leaf token to use to store TOTP tokens
- EnvTOTPToken = register(EnvironmentString{environmentBase: environmentBase{
+ EnvTOTPToken = environmentRegister(EnvironmentString{environmentBase: environmentBase{
subKey: "TOTP",
desc: "Attribute name to store TOTP tokens within the database.",
}, allowed: []string{"<string>"}, canDefault: true, defaultValue: "totp"})
// EnvPlatform is the platform that the application is running on
- EnvPlatform = register(EnvironmentString{environmentBase: environmentBase{
+ EnvPlatform = environmentRegister(EnvironmentString{environmentBase: environmentBase{
subKey: "PLATFORM",
desc: "Override the detected platform.",
}, defaultValue: detectedValue, allowed: Platforms, canDefault: false})
// EnvStore is the location of the keepass file/store
- EnvStore = register(EnvironmentString{environmentBase: environmentBase{
+ EnvStore = environmentRegister(EnvironmentString{environmentBase: environmentBase{
subKey: "STORE",
desc: "Directory to the database file.", requirement: "must be set",
}, canDefault: false, allowed: []string{fileExample}})
// EnvHookDir is the directory of hooks to execute
- EnvHookDir = register(EnvironmentString{environmentBase: environmentBase{
+ EnvHookDir = environmentRegister(EnvironmentString{environmentBase: environmentBase{
subKey: "HOOKDIR",
desc: "The path to hooks to execute on actions against the database.",
}, allowed: []string{"<directory>"}, canDefault: true, defaultValue: ""})
// EnvClipCopy allows overriding the clipboard copy command
- EnvClipCopy = register(EnvironmentCommand{environmentBase: environmentBase{
+ EnvClipCopy = environmentRegister(EnvironmentCommand{environmentBase: environmentBase{
subKey: "COPY",
cat: clipCategory,
desc: "Override the detected platform copy command.",
}})
// EnvClipPaste allows overriding the clipboard paste command
- EnvClipPaste = register(EnvironmentCommand{environmentBase: environmentBase{
+ EnvClipPaste = environmentRegister(EnvironmentCommand{environmentBase: environmentBase{
subKey: "PASTE",
cat: clipCategory,
desc: "Override the detected platform paste command.",
}})
// EnvTOTPColorBetween handles terminal coloring for TOTP windows (seconds)
- EnvTOTPColorBetween = register(EnvironmentString{environmentBase: environmentBase{
+ EnvTOTPColorBetween = environmentRegister(EnvironmentString{environmentBase: environmentBase{
subKey: "BETWEEN",
cat: totpCategory,
desc: fmt.Sprintf(`Override when to set totp generated outputs to different colors,
@@ -154,17 +154,17 @@ must be a list of one (or more) rules where a '%s' delimits the start and end se
and '%s' allows for multiple windows.`, colorWindowSpan, colorWindowDelimiter),
}, canDefault: true, defaultValue: TOTPDefaultBetween, allowed: exampleColorWindows})
// EnvKeyFile is an keyfile for the database
- EnvKeyFile = register(EnvironmentString{environmentBase: environmentBase{
+ EnvKeyFile = environmentRegister(EnvironmentString{environmentBase: environmentBase{
subKey: "KEYFILE", requirement: requiredKeyOrKeyFile,
desc: "A keyfile to access/protect the database.",
}, allowed: []string{"keyfile"}, canDefault: true, defaultValue: ""})
// EnvModTime is modtime override ability for entries
- EnvModTime = register(EnvironmentString{environmentBase: environmentBase{
+ EnvModTime = environmentRegister(EnvironmentString{environmentBase: environmentBase{
subKey: "SET_MODTIME",
desc: fmt.Sprintf("Input modification time to set for the entry\n\nExpected format: %s.", ModTimeFormat),
}, canDefault: true, defaultValue: "", allowed: []string{"modtime"}})
// EnvJSONDataOutput controls how JSON is output in the 'data' field
- EnvJSONDataOutput = register(EnvironmentString{
+ EnvJSONDataOutput = environmentRegister(EnvironmentString{
environmentBase: environmentBase{
subKey: "JSON_DATA",
desc: fmt.Sprintf("Changes what the data field in JSON outputs will contain.\n\nUse '%s' with CAUTION.", JSONDataOutputRaw),
@@ -172,13 +172,13 @@ and '%s' allows for multiple windows.`, colorWindowSpan, colorWindowDelimiter),
allowed: []string{string(JSONDataOutputRaw), string(JSONDataOutputHash), string(JSONDataOutputBlank)},
})
// EnvFormatTOTP supports formatting the TOTP tokens for generation of tokens
- EnvFormatTOTP = register(EnvironmentFormatter{environmentBase: environmentBase{
+ EnvFormatTOTP = environmentRegister(EnvironmentFormatter{environmentBase: environmentBase{
subKey: "FORMAT",
cat: totpCategory,
desc: "Override the otpauth url used to store totp tokens. It must have ONE format string ('%s') to insert the totp base code.",
}, fxn: formatterTOTP, allowed: "otpauth//url/%s/args..."})
// EnvConfig is the location of the config file to read environment variables from
- EnvConfig = register(EnvironmentString{environmentBase: environmentBase{
+ EnvConfig = environmentRegister(EnvironmentString{environmentBase: environmentBase{
subKey: "ENV",
desc: fmt.Sprintf(`Allows setting a specific file of environment variables for lockbox to read and use as
configuration values (an '.env' file). The keyword '%s' will disable this functionality and the keyword '%s' will
@@ -188,7 +188,7 @@ paths: %v
Note that this setting is not output as part of the environment.`, noEnvironment, detectEnvironment, detectEnvironmentPaths),
}, canDefault: true, defaultValue: detectEnvironment, allowed: []string{detectEnvironment, fileExample, noEnvironment}})
- envKeyMode = register(EnvironmentString{
+ envKeyMode = environmentRegister(EnvironmentString{
environmentBase: environmentBase{
subKey: "KEYMODE", requirement: "must be set to a valid mode when using a key",
desc: fmt.Sprintf(`How to retrieve the database store password. Set to '%s' when only using a key file.
@@ -197,13 +197,13 @@ Set to '%s' to ignore the set key value`, noKeyMode, IgnoreKeyMode), whenUnset:
allowed: []string{string(askKeyMode), string(commandKeyMode), string(IgnoreKeyMode), string(noKeyMode), string(plainKeyMode)},
canDefault: true, defaultValue: "",
})
- envKey = register(EnvironmentString{environmentBase: environmentBase{
+ envKey = environmentRegister(EnvironmentString{environmentBase: environmentBase{
requirement: requiredKeyOrKeyFile, subKey: "KEY",
desc: fmt.Sprintf("The database key ('%s' mode) or command to run ('%s' mode) to retrieve the database password.",
plainKeyMode,
commandKeyMode),
}, allowed: []string{commandArgsExample, "password"}, canDefault: false})
- envConfigExpands = register(EnvironmentInt{environmentBase: environmentBase{
+ envConfigExpands = environmentRegister(EnvironmentInt{environmentBase: environmentBase{
subKey: EnvConfig.subKey + "_EXPANDS",
desc: `The maximum number of times to expand the input env to resolve variables (set to 0 to disable expansion).
diff --git a/internal/config/vars_test.go b/internal/config/vars_test.go
@@ -223,7 +223,9 @@ func TestEnvironDefinitions(t *testing.T) {
os.Clearenv()
vals := config.ListEnvironmentVariables()
expect := make(map[string]struct{})
+ found := false
for _, val := range vals {
+ found = true
env := strings.Split(strings.TrimSpace(val), "\n")[0]
if !strings.HasPrefix(env, "LOCKBOX_") {
t.Errorf("invalid env var: %s", env)
@@ -234,6 +236,9 @@ func TestEnvironDefinitions(t *testing.T) {
os.Setenv(env, "test")
expect[env] = struct{}{}
}
+ if !found {
+ t.Errorf("no environment variables found?")
+ }
read := config.Environ()
if len(read) != len(expect) {
t.Errorf("invalid environment variable info: %d != %d", len(expect), len(read))