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.
This commit is contained in:
Chris Lane 2020-03-05 17:46:49 -05:00
parent ec10244ebe
commit efd09575df
1 changed files with 9 additions and 3 deletions

View File

@ -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