lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 91ee3ead5b451a08a14b252d774df0013098c5ec
parent b724ca7e974dd57eec7071be5cac676bf867fc25
Author: Sean Enck <sean@ttypty.com>
Date:   Sun,  8 Dec 2024 08:29:47 -0500

fixing up config detection

Diffstat:
Minternal/config/core.go | 36++++++++++++++----------------------
Minternal/config/core_test.go | 12++++++------
Minternal/config/env.go | 6+++---
Minternal/config/toml.go | 10+---------
Minternal/config/vars.go | 15---------------
5 files changed, 24 insertions(+), 55 deletions(-)

diff --git a/internal/config/core.go b/internal/config/core.go @@ -6,6 +6,7 @@ import ( "net/url" "os" "path/filepath" + "strconv" "strings" "time" @@ -14,10 +15,6 @@ import ( ) const ( - yes = "true" - no = "false" - detectEnvironment = "detect" - tomlFile = "lockbox.toml" // sub categories clipCategory keyCategory = "CLIP_" totpCategory keyCategory = "TOTP_" @@ -26,29 +23,26 @@ const ( credsCategory keyCategory = "CREDENTIALS_" defaultCategory keyCategory = "DEFAULTS_" hookCategory keyCategory = "HOOKS_" - // YesValue are yes (on) values - YesValue = yes - // NoValue are no (off) values - NoValue = no // TemplateVariable is used to handle '$' in shell vars (due to expansion) TemplateVariable = "[%]" - configDirName = "lockbox" - configDir = ".config" environmentPrefix = "LOCKBOX_" commandArgsExample = "[cmd args...]" fileExample = "<file>" - detectedValue = "<detected>" requiredKeyOrKeyFile = "a key, a key file, or both must be set" // ModTimeFormat is the expected modtime format ModTimeFormat = time.RFC3339 exampleColorWindow = "start" + util.TimeWindowSpan + "end" + detectedValue = "(detected)" ) var ( + YesValue = strconv.FormatBool(true) + NoValue = strconv.FormatBool(false) exampleColorWindows = []string{fmt.Sprintf("[%s]", strings.Join([]string{exampleColorWindow, exampleColorWindow, exampleColorWindow + "..."}, util.TimeWindowDelimiter))} - configDirOffsetFile = filepath.Join(configDirName, tomlFile) - xdgPaths = []string{configDirOffsetFile, tomlFile} - homePaths = []string{filepath.Join(configDir, configDirOffsetFile), filepath.Join(configDir, tomlFile)} + configDirFile = filepath.Join("lockbox", "config.toml") + ConfigXDG = configDirFile + ConfigHome = filepath.Join(".config", configDirFile) + ConfigEnv = environmentPrefix + "CONFIG_TOML" registry = map[string]printer{} // TOTPDefaultColorWindow is the default coloring rules for totp TOTPDefaultColorWindow = []util.TimeWindow{{Start: 0, End: 5}, {Start: 30, End: 35}} @@ -73,21 +67,19 @@ type ( // NewConfigFiles will get the list of candidate config files func NewConfigFiles() []string { - v := os.Expand(os.Getenv(EnvConfig.Key()), os.Getenv) - if v != detectEnvironment { + v := os.Expand(os.Getenv(ConfigEnv), os.Getenv) + if v != "" { return []string{v} } var options []string - pathAdder := func(root string, err error, subs []string) { + pathAdder := func(root, sub string, err error) { if err == nil && root != "" { - for _, s := range subs { - options = append(options, filepath.Join(root, s)) - } + options = append(options, filepath.Join(root, sub)) } } - pathAdder(os.Getenv("XDG_CONFIG_HOME"), nil, xdgPaths) + pathAdder(os.Getenv("XDG_CONFIG_HOME"), ConfigXDG, nil) h, err := os.UserHomeDir() - pathAdder(h, err, homePaths) + pathAdder(h, ConfigHome, err) return options } diff --git a/internal/config/core_test.go b/internal/config/core_test.go @@ -17,21 +17,21 @@ func TestNewEnvFiles(t *testing.T) { t.Errorf("invalid files: %v", f) } t.Setenv("HOME", "test") - t.Setenv("LOCKBOX_CONFIG_TOML", "detect") + t.Setenv("LOCKBOX_CONFIG_TOML", "") f = config.NewConfigFiles() - if len(f) != 2 { + if len(f) != 1 { t.Errorf("invalid files: %v", f) } - t.Setenv("LOCKBOX_CONFIG_TOML", "detect") + t.Setenv("LOCKBOX_CONFIG_TOML", "") t.Setenv("XDG_CONFIG_HOME", "test") f = config.NewConfigFiles() - if len(f) != 4 { + if len(f) != 2 { t.Errorf("invalid files: %v", f) } - t.Setenv("LOCKBOX_CONFIG_TOML", "detect") + t.Setenv("LOCKBOX_CONFIG_TOML", "") os.Unsetenv("HOME") f = config.NewConfigFiles() - if len(f) != 2 { + if len(f) != 1 { t.Errorf("invalid files: %v", f) } } diff --git a/internal/config/env.go b/internal/config/env.go @@ -119,11 +119,11 @@ func (e environmentBase) self() environmentBase { } func (e EnvironmentBool) values() (string, []string) { - val := no + val := NoValue if e.defaultValue { - val = yes + val = YesValue } - return val, []string{yes, no} + return val, []string{YesValue, NoValue} } func (e EnvironmentInt) values() (string, []string) { diff --git a/internal/config/toml.go b/internal/config/toml.go @@ -34,11 +34,7 @@ func DefaultTOML() (string, error) { const root = "_root_" unmapped := make(map[string][]string) keys := []string{} - isConfig := EnvConfig.Key() for envKey, item := range registry { - if envKey == isConfig { - continue - } tomlKey := strings.ToLower(strings.TrimPrefix(envKey, environmentPrefix)) parts := strings.Split(tomlKey, "_") length := len(parts) @@ -74,11 +70,7 @@ func DefaultTOML() (string, error) { } sort.Strings(keys) builder := strings.Builder{} - configEnv, err := generateDetailText(EnvConfig) - if err != nil { - return "", err - } - for _, header := range []string{configEnv, "\n", fmt.Sprintf(` + for _, header := range []string{fmt.Sprintf(` # include additional configs, allowing globs ('*'), nesting # depth allowed up to %d include levels # diff --git a/internal/config/vars.go b/internal/config/vars.go @@ -3,7 +3,6 @@ package config import ( "fmt" - "strings" "github.com/seanenck/lockbox/internal/output" "github.com/seanenck/lockbox/internal/platform" @@ -231,20 +230,6 @@ and '%s' allows for multiple windows.`, util.TimeWindowSpan, util.TimeWindowDeli 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 - EnvConfig = environmentRegister( - EnvironmentString{ - environmentDefault: newDefaultedEnvironment(detectEnvironment, - environmentBase{ - subKey: "CONFIG_TOML", - desc: fmt.Sprintf(`Allows setting a specific toml file to read and load. - -The keyword '%s' will search for a file in the following paths in -XDG_CONFIG_HOME (%s) or from the user's HOME (%s). Matches the first file found.`, detectEnvironment, strings.Join(xdgPaths, ","), strings.Join(homePaths, ",")), - }), - canDefault: true, - allowed: []string{detectEnvironment, fileExample}, - }) // EnvPasswordMode indicates how the password is read EnvPasswordMode = environmentRegister( EnvironmentString{