mirror of https://github.com/cheat/cheat.git
chore: refactors `config.Paths`
Refactors the reading of multiple envvars out of `config.Paths` in order to facilitate cleaner unit-testing.
This commit is contained in:
parent
d4c6200702
commit
6912771c39
|
@ -6,6 +6,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/docopt/docopt-go"
|
"github.com/docopt/docopt-go"
|
||||||
|
|
||||||
|
@ -31,8 +32,15 @@ func main() {
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// read the envvars into a map of strings
|
||||||
|
envvars := map[string]string{}
|
||||||
|
for _, e := range os.Environ() {
|
||||||
|
pair := strings.SplitN(e, "=", 2)
|
||||||
|
envvars[pair[0]] = pair[1]
|
||||||
|
}
|
||||||
|
|
||||||
// load the os-specifc paths at which the config file may be located
|
// load the os-specifc paths at which the config file may be located
|
||||||
confpaths, err := config.Paths(runtime.GOOS)
|
confpaths, err := config.Paths(runtime.GOOS, envvars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -2,7 +2,6 @@ package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
"github.com/mitchellh/go-homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
|
@ -10,13 +9,13 @@ import (
|
||||||
|
|
||||||
// Paths returns config file paths that are appropriate for the operating
|
// Paths returns config file paths that are appropriate for the operating
|
||||||
// system
|
// system
|
||||||
func Paths(sys string) ([]string, error) {
|
func Paths(sys string, envvars map[string]string) ([]string, error) {
|
||||||
|
|
||||||
// if CHEAT_CONFIG_PATH is set, return it
|
// if CHEAT_CONFIG_PATH is set, expand ~ and return it
|
||||||
if os.Getenv("CHEAT_CONFIG_PATH") != "" {
|
if confpath, ok := envvars["CHEAT_CONFIG_PATH"]; ok {
|
||||||
|
|
||||||
// expand ~
|
// expand ~
|
||||||
expanded, err := homedir.Expand(os.Getenv("CHEAT_CONFIG_PATH"))
|
expanded, err := homedir.Expand(confpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []string{}, fmt.Errorf("failed to expand ~: %v", err)
|
return []string{}, fmt.Errorf("failed to expand ~: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -27,14 +26,14 @@ func Paths(sys string) ([]string, error) {
|
||||||
switch sys {
|
switch sys {
|
||||||
case "darwin", "linux", "freebsd":
|
case "darwin", "linux", "freebsd":
|
||||||
return []string{
|
return []string{
|
||||||
path.Join(os.Getenv("XDG_CONFIG_HOME"), "/cheat/conf.yml"),
|
path.Join(envvars["XDG_CONFIG_HOME"], "/cheat/conf.yml"),
|
||||||
path.Join(os.Getenv("HOME"), ".config/cheat/conf.yml"),
|
path.Join(envvars["HOME"], ".config/cheat/conf.yml"),
|
||||||
path.Join(os.Getenv("HOME"), ".cheat/conf.yml"),
|
path.Join(envvars["HOME"], ".cheat/conf.yml"),
|
||||||
}, nil
|
}, nil
|
||||||
case "windows":
|
case "windows":
|
||||||
return []string{
|
return []string{
|
||||||
fmt.Sprintf("%s/cheat/conf.yml", os.Getenv("APPDATA")),
|
fmt.Sprintf("%s/cheat/conf.yml", envvars["APPDATA"]),
|
||||||
fmt.Sprintf("%s/cheat/conf.yml", os.Getenv("PROGRAMDATA")),
|
fmt.Sprintf("%s/cheat/conf.yml", envvars["PROGRAMDATA"]),
|
||||||
}, nil
|
}, nil
|
||||||
default:
|
default:
|
||||||
return []string{}, fmt.Errorf("unsupported os: %s", sys)
|
return []string{}, fmt.Errorf("unsupported os: %s", sys)
|
||||||
|
|
Loading…
Reference in New Issue