2020-01-29 20:08:03 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Paths returns config file paths that are appropriate for the operating
|
|
|
|
// system
|
2020-03-06 02:19:58 +01:00
|
|
|
func Paths(
|
|
|
|
sys string,
|
|
|
|
home string,
|
|
|
|
envvars map[string]string,
|
|
|
|
) ([]string, error) {
|
2020-03-05 23:46:49 +01:00
|
|
|
|
2020-01-31 01:59:35 +01:00
|
|
|
// if `CHEAT_CONFIG_PATH` is set, expand ~ and return it
|
2020-01-31 00:19:43 +01:00
|
|
|
if confpath, ok := envvars["CHEAT_CONFIG_PATH"]; ok {
|
2020-01-29 20:08:03 +01:00
|
|
|
|
|
|
|
// expand ~
|
2020-01-31 00:19:43 +01:00
|
|
|
expanded, err := homedir.Expand(confpath)
|
2020-01-29 20:08:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return []string{}, fmt.Errorf("failed to expand ~: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return []string{expanded}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch sys {
|
|
|
|
case "darwin", "linux", "freebsd":
|
2020-01-31 01:59:35 +01:00
|
|
|
paths := []string{}
|
|
|
|
|
|
|
|
// don't include the `XDG_CONFIG_HOME` path if that envvar is not set
|
|
|
|
if xdgpath, ok := envvars["XDG_CONFIG_HOME"]; ok {
|
|
|
|
paths = append(paths, path.Join(xdgpath, "/cheat/conf.yml"))
|
|
|
|
}
|
|
|
|
|
2020-03-05 23:46:49 +01:00
|
|
|
// if `XDG_CONFIG_HOME` is not set, search the user's home directory
|
2020-01-31 01:59:35 +01:00
|
|
|
paths = append(paths, []string{
|
2020-03-05 23:46:49 +01:00
|
|
|
path.Join(home, ".config/cheat/conf.yml"),
|
|
|
|
path.Join(home, ".cheat/conf.yml"),
|
2020-01-31 01:59:35 +01:00
|
|
|
}...)
|
|
|
|
|
|
|
|
return paths, nil
|
2020-01-29 20:08:03 +01:00
|
|
|
case "windows":
|
|
|
|
return []string{
|
2020-01-31 01:45:02 +01:00
|
|
|
path.Join(envvars["APPDATA"], "/cheat/conf.yml"),
|
|
|
|
path.Join(envvars["PROGRAMDATA"], "/cheat/conf.yml"),
|
2020-01-29 20:08:03 +01:00
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
return []string{}, fmt.Errorf("unsupported os: %s", sys)
|
|
|
|
}
|
|
|
|
}
|