forked from 99designs/smartling
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathconfig.go
97 lines (77 loc) · 1.81 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
87
88
89
90
91
92
93
94
95
96
97
package main
import (
"os"
"dario.cat/mergo"
"github.com/gobwas/glob"
"github.com/kovetskiy/ko"
"github.com/reconquest/hierr-go"
"gopkg.in/yaml.v2"
)
type FileConfig struct {
Pull struct {
Format string `yaml:"format,omitempty"`
} `yaml:"pull,omitempty"`
Push struct {
Type string `yaml:"type,omitempty"`
Directives map[string]string `yaml:"directives,omitempty,flow"`
} `yaml:"push,omitempty"`
}
type Config struct {
UserID string `yaml:"user_id"`
Secret string `yaml:"secret"`
AccountID string `yaml:"account_id"`
ProjectID string `yaml:"project_id,omitempty"`
Threads int `yaml:"threads"`
Files map[string]FileConfig `yaml:"files"`
Proxy string `yaml:"proxy,omitempty"`
path string
}
func NewConfig(path string) (Config, error) {
config := Config{
path: path,
}
err := ko.Load(path, &config, yaml.Unmarshal)
if err != nil {
if os.IsNotExist(err) {
return config, nil
}
return config, err
}
return config, nil
}
func (config *Config) GetFileConfig(path string) (FileConfig, error) {
var (
match FileConfig
found bool
)
for key, candidate := range config.Files {
pattern, err := glob.Compile(key, '/')
if err != nil {
return FileConfig{}, NewError(
hierr.Errorf(
err,
`unable to compile pattern from config file (key "%s")`,
key,
),
`File match pattern is malformed. Check out help for more `+
`information on globbing patterns.`,
)
}
if pattern.Match(path) {
match = candidate
found = true
}
}
defaults := config.Files["default"]
if !found {
return defaults, nil
}
err := mergo.Merge(&match, defaults)
if err != nil {
return FileConfig{}, NewError(
hierr.Errorf(err, "unable to merge file config options"),
`It's internal error. Consider reporting bug.`,
)
}
return match, nil
}