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-01-31 00:19:43 +01:00
|
|
|
func Paths(sys string, envvars map[string]string) ([]string, error) {
|
2020-01-29 20:08:03 +01:00
|
|
|
|
2020-01-31 00:19:43 +01:00
|
|
|
// if CHEAT_CONFIG_PATH is set, expand ~ and return it
|
|
|
|
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":
|
|
|
|
return []string{
|
2020-01-31 00:19:43 +01:00
|
|
|
path.Join(envvars["XDG_CONFIG_HOME"], "/cheat/conf.yml"),
|
|
|
|
path.Join(envvars["HOME"], ".config/cheat/conf.yml"),
|
|
|
|
path.Join(envvars["HOME"], ".cheat/conf.yml"),
|
2020-01-29 20:08:03 +01:00
|
|
|
}, nil
|
|
|
|
case "windows":
|
|
|
|
return []string{
|
2020-01-31 00:19:43 +01:00
|
|
|
fmt.Sprintf("%s/cheat/conf.yml", envvars["APPDATA"]),
|
|
|
|
fmt.Sprintf("%s/cheat/conf.yml", envvars["PROGRAMDATA"]),
|
2020-01-29 20:08:03 +01:00
|
|
|
}, nil
|
|
|
|
default:
|
|
|
|
return []string{}, fmt.Errorf("unsupported os: %s", sys)
|
|
|
|
}
|
|
|
|
}
|