mirror of
https://github.com/cheat/cheat.git
synced 2024-11-16 08:58:28 +01:00
6912771c39
Refactors the reading of multiple envvars out of `config.Paths` in order to facilitate cleaner unit-testing.
41 lines
1,011 B
Go
41 lines
1,011 B
Go
package config
|
|
|
|
import (
|
|
"fmt"
|
|
"path"
|
|
|
|
"github.com/mitchellh/go-homedir"
|
|
)
|
|
|
|
// Paths returns config file paths that are appropriate for the operating
|
|
// system
|
|
func Paths(sys string, envvars map[string]string) ([]string, error) {
|
|
|
|
// if CHEAT_CONFIG_PATH is set, expand ~ and return it
|
|
if confpath, ok := envvars["CHEAT_CONFIG_PATH"]; ok {
|
|
|
|
// expand ~
|
|
expanded, err := homedir.Expand(confpath)
|
|
if err != nil {
|
|
return []string{}, fmt.Errorf("failed to expand ~: %v", err)
|
|
}
|
|
|
|
return []string{expanded}, nil
|
|
}
|
|
|
|
switch sys {
|
|
case "darwin", "linux", "freebsd":
|
|
return []string{
|
|
path.Join(envvars["XDG_CONFIG_HOME"], "/cheat/conf.yml"),
|
|
path.Join(envvars["HOME"], ".config/cheat/conf.yml"),
|
|
path.Join(envvars["HOME"], ".cheat/conf.yml"),
|
|
}, nil
|
|
case "windows":
|
|
return []string{
|
|
fmt.Sprintf("%s/cheat/conf.yml", envvars["APPDATA"]),
|
|
fmt.Sprintf("%s/cheat/conf.yml", envvars["PROGRAMDATA"]),
|
|
}, nil
|
|
default:
|
|
return []string{}, fmt.Errorf("unsupported os: %s", sys)
|
|
}
|
|
}
|