From efd09575df4422449d6178cd922a680e0f871c21 Mon Sep 17 00:00:00 2001 From: Chris Lane Date: Thu, 5 Mar 2020 17:46:49 -0500 Subject: [PATCH] feat(config): refactor config path detection Previously, failing other checks, on Unix and BSD systems, `config.Paths` would attempt to compute the user's home directory by reading the `HOME` environment variable. This change deprecates that approach with a call to `homedir.Dir`, which is used elsewhere throughout the application. --- internal/config/paths.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/internal/config/paths.go b/internal/config/paths.go index 52a2d86..b5752c4 100644 --- a/internal/config/paths.go +++ b/internal/config/paths.go @@ -11,6 +11,12 @@ import ( // system func Paths(sys string, envvars map[string]string) ([]string, error) { + // get the user's home directory + home, err := homedir.Dir() + if err != nil { + return []string{}, fmt.Errorf("failed to get user home directory: %v", err) + } + // if `CHEAT_CONFIG_PATH` is set, expand ~ and return it if confpath, ok := envvars["CHEAT_CONFIG_PATH"]; ok { @@ -32,10 +38,10 @@ func Paths(sys string, envvars map[string]string) ([]string, error) { paths = append(paths, path.Join(xdgpath, "/cheat/conf.yml")) } - // `HOME` will always be set on a POSIX-compliant system, though + // if `XDG_CONFIG_HOME` is not set, search the user's home directory paths = append(paths, []string{ - path.Join(envvars["HOME"], ".config/cheat/conf.yml"), - path.Join(envvars["HOME"], ".cheat/conf.yml"), + path.Join(home, ".config/cheat/conf.yml"), + path.Join(home, ".cheat/conf.yml"), }...) return paths, nil