lockbox

password manager
Log | Files | Refs | README | LICENSE

commit 7ad58a74c0119f7a89927aabcb0cf909f5fd92a1
parent 5b6f975d160bfd8e4c50e7d4dd7d9f4900a2cce6
Author: Sean Enck <sean@ttypty.com>
Date:   Wed,  9 Oct 2024 18:51:50 -0400

rework configuration file detection pathing

Diffstat:
Minternal/config/core.go | 30++++++++++++++++++++----------
Minternal/config/core_test.go | 19+++++++++++++++++++
Minternal/config/vars.go | 7+++----
3 files changed, 42 insertions(+), 14 deletions(-)

diff --git a/internal/config/core.go b/internal/config/core.go @@ -34,12 +34,16 @@ const ( YesValue = yes // TemplateVariable is used to handle '$' in shell vars (due to expansion) TemplateVariable = "[%]" + configDirName = "lockbox" + configDir = ".config" ) var ( - detectEnvironmentPaths = []string{filepath.Join(".config", envFile), filepath.Join(".config", "lockbox", envFile)} - exampleColorWindows = []string{strings.Join([]string{exampleColorWindow, exampleColorWindow, exampleColorWindow + "..."}, colorWindowDelimiter)} - registeredEnv = []printer{} + configDirOffsetFile = filepath.Join(configDirName, envFile) + xdgPaths = []string{configDirOffsetFile, envFile} + homePaths = []string{filepath.Join(configDir, configDirOffsetFile), filepath.Join(configDir, envFile)} + exampleColorWindows = []string{strings.Join([]string{exampleColorWindow, exampleColorWindow, exampleColorWindow + "..."}, colorWindowDelimiter)} + registeredEnv = []printer{} ) type ( @@ -326,15 +330,21 @@ func NewEnvFiles() ([]string, error) { if v != detectEnvironment { return []string{v}, nil } - h, err := os.UserHomeDir() - if err != nil { - return nil, err + var options []string + pathAdder := func(root string, err error, subs []string) { + if err == nil && root != "" { + for _, s := range subs { + options = append(options, filepath.Join(root, s)) + } + } } - var results []string - for _, p := range detectEnvironmentPaths { - results = append(results, filepath.Join(h, p)) + pathAdder(os.Getenv("XDG_CONFIG_HOME"), nil, xdgPaths) + h, err := os.UserHomeDir() + pathAdder(h, err, homePaths) + if len(options) == 0 { + return nil, errors.New("unable to initialize default config locations") } - return results, nil + return options, nil } // IsUnset will indicate if a variable is an unset (and unset it) or return that it isn't diff --git a/internal/config/core_test.go b/internal/config/core_test.go @@ -107,6 +107,7 @@ func TestParseWindows(t *testing.T) { } func TestNewEnvFiles(t *testing.T) { + os.Clearenv() os.Setenv("LOCKBOX_ENV", "none") f, err := config.NewEnvFiles() if len(f) != 0 || err != nil { @@ -123,6 +124,24 @@ func TestNewEnvFiles(t *testing.T) { if len(f) != 2 || err != nil { t.Errorf("invalid files: %v %v", f, err) } + os.Setenv("LOCKBOX_ENV", "detect") + os.Setenv("XDG_CONFIG_HOME", "test") + f, err = config.NewEnvFiles() + if len(f) != 4 || err != nil { + t.Errorf("invalid files: %v %v", f, err) + } + os.Setenv("LOCKBOX_ENV", "detect") + os.Unsetenv("HOME") + f, err = config.NewEnvFiles() + if len(f) != 2 || err != nil { + t.Errorf("invalid files: %v %v", f, err) + } + os.Setenv("LOCKBOX_ENV", "detect") + os.Unsetenv("XDG_CONFIG_HOME") + _, err = config.NewEnvFiles() + if err == nil || err.Error() != "unable to initialize default config locations" { + t.Errorf("invalid files: %v", err) + } } func TestIsUnset(t *testing.T) { diff --git a/internal/config/vars.go b/internal/config/vars.go @@ -279,11 +279,10 @@ and '%s' allows for multiple windows.`, colorWindowSpan, colorWindowDelimiter), 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 -search for a file in the following paths in the user's home directory matching the first file found. +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. -paths: %v - -Note that this setting is not output as part of the environment.`, noEnvironment, detectEnvironment, detectEnvironmentPaths), +Note that this setting is not output as part of the environment.`, noEnvironment, detectEnvironment, strings.Join(xdgPaths, ","), strings.Join(homePaths, ",")), }), canDefault: true, allowed: []string{detectEnvironment, fileExample, noEnvironment},