-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
load.go
63 lines (54 loc) · 1017 Bytes
/
load.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
package config
import (
"os"
"path/filepath"
"github.com/zoncoen/scenarigo/schema"
)
var (
// These values will be set by the root command.
ConfigPath string
Root string
)
// Load loads configuration.
func Load() (*schema.Config, error) {
root := Root
var err error
if root != "" {
root, err = filepath.Abs(Root)
if err != nil {
return nil, err
}
}
if ConfigPath == "-" {
if root == "" {
root, err = os.Getwd()
if err != nil {
return nil, err
}
}
return schema.LoadConfigFromReader(os.Stdin, root)
}
c, err := load(root)
if err != nil {
if ConfigPath == "" && os.IsNotExist(err) {
return nil, nil //nolint:nilnil
}
return nil, err
}
return c, nil
}
func load(root string) (*schema.Config, error) {
path := ConfigPath
if path == "" {
path = DefaultConfigFileName
}
if root == "" {
return schema.LoadConfig(path)
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
return schema.LoadConfigFromReader(f, root)
}