-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
86 lines (72 loc) · 1.83 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/iancoleman/strcase"
"github.com/juju/errors"
"github.com/rs/zerolog/log"
"github.com/shibukawa/configdir"
"gopkg.in/yaml.v2"
)
func getConfig(configFilename string) (Config, error) {
var config Config
fullPath, err := getRelevantConfigPath(configFilename)
if err != nil {
return config, err
}
data, err := ioutil.ReadFile(fullPath)
if err != nil {
return config, err
}
err = yaml.Unmarshal(data, &config)
if err != nil {
return config, err
}
return config, nil
}
func mustGetConfig(configFilename string) Config {
result, err := getConfig(configFilename)
err = errors.Annotate(err, "failed to read config file")
if err != nil {
log.Fatal().Err(err).Msg("")
}
return result
}
func getConfigPaths() []string {
var dirList []string
if wd, err := os.Getwd(); err == nil {
dirList = append(dirList, wd)
}
configDirs := configdir.New("", strcase.ToKebab(ToolName))
for _, c := range configDirs.QueryFolders(configdir.Local) {
dirList = append(dirList, c.Path)
}
for _, c := range configDirs.QueryFolders(configdir.Global) {
dirList = append(dirList, c.Path)
}
for _, c := range configDirs.QueryFolders(configdir.System) {
dirList = append(dirList, c.Path)
}
// more relevant first
return dirList
}
func getRelevantConfigPath(configFilename string) (string, error) {
var fullPath string
for _, c := range getConfigPaths() {
fullPath = filepath.Join(c, configFilename)
if fileExists(filepath.Join(fullPath)) {
return fullPath, nil
}
}
return "", errors.New(fmt.Sprintf("Config file %s not found in any of these paths: %s", configFilename, strings.Join(getConfigPaths(), ", ")))
}
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}