2020-01-29 20:08:03 +01:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-09-28 18:33:59 +02:00
|
|
|
"path/filepath"
|
2020-01-29 20:08:03 +01:00
|
|
|
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Paths returns config file paths that are appropriate for the operating
|
|
|
|
// system
|
2020-03-07 02:17:26 +01:00
|
|
|
func Paths(
|
|
|
|
sys string,
|
|
|
|
home string,
|
|
|
|
envvars map[string]string,
|
|
|
|
) ([]string, error) {
|
2020-01-29 20:08:03 +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 {
|
2021-09-28 18:33:59 +02:00
|
|
|
paths = append(paths, filepath.Join(xdgpath, "/cheat/conf.yml"))
|
2020-01-31 01:59:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
paths = append(paths, []string{
|
2021-09-28 18:33:59 +02:00
|
|
|
filepath.Join(home, ".config/cheat/conf.yml"),
|
|
|
|
filepath.Join(home, ".cheat/conf.yml"),
|
2020-07-10 00:28:10 +02:00
|
|
|
"/etc/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{
|
2021-09-28 18:33:59 +02:00
|
|
|
filepath.Join(envvars["APPDATA"], "/cheat/conf.yml"),
|
|
|
|
filepath.Join(envvars["PROGRAMDATA"], "/cheat/conf.yml"),
|
2020-01-29 20:08:03 +01:00
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
return []string{}, fmt.Errorf("unsupported os: %s", sys)
|
|
|
|
}
|
|
|
|
}
|