-
Notifications
You must be signed in to change notification settings - Fork 1
/
input.go
120 lines (102 loc) · 2.55 KB
/
input.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"strings"
)
func findDefaults(folder string) (string, bool) {
// log.Debugf("? findDefaults( %s ) ", folder)
if len(folder) <= 1 {
return "", false
}
foo := 0
for folder != "/" {
foo++
if foo > 30 {
// log.Fatal("to deep folders, or possible loop")
return "", false
}
folder = path.Dir(folder)
// log.Debugf("? checking for .sk8default in %q", folder)
folder2 := path.Join(folder, ".sk8default")
fi, err := os.Stat(folder2)
if err != nil {
// log.Debugf("? -> %s", err.Error())
} else {
// log.Debugf("? => %+v", fi)
if fi != nil && fi.IsDir() {
// log.Debugf("Found .sk8default in %q as %q", folder, fi.Name())
return folder2, true
}
}
}
return "", false
}
func mergeSettingsFrom(dirname string, base, o *SK8config) {
files, err := ioutil.ReadDir(dirname)
if err == nil {
for _, def := range files {
if strings.HasSuffix(def.Name(), ".yaml") {
defName := path.Join(dirname, def.Name())
log.Debugf("Import defaults: %s", defName)
overrides, err := loadFile(defName, 0, o)
if err != nil {
panic(err)
}
if len(overrides) > 1 {
panic(fmt.Errorf("multiple documents in %s (%s)", def.Name(), dirname))
}
base.mergeWith(overrides[0])
}
}
}
}
// LoadInput -
func LoadInput() []*SK8config {
var cfgs []*SK8config
currdir, _ := os.Getwd()
for _, f := range args.Files {
dir := path.Dir(f)
item := path.Base(f)
log.Debugf("File %s: folder %s, name %s", f, dir, item)
_ = os.Chdir(dir)
docs, err := loadFile(item, 0, nil)
if err != nil {
log.Fatalf("loadFile error: %v", err.Error())
return nil
}
fullpath, err := os.Getwd()
if err == nil {
fullpath = path.Join(fullpath, item)
}
for _, o := range docs {
log.Debugf("doc(%s) = %v", o.Namespace, o.cfgType)
if o.cfgType == typeSK8 {
base := &SK8config{}
if defaults, found := findDefaults(fullpath); found {
mergeSettingsFrom(defaults, base, o)
}
mergeSettingsFrom(".sk8", base, o)
o = base.mergeWith(o)
overrideFile := strings.Replace(item, ".yaml", ".override", -1)
if _, err := os.Stat(overrideFile); err == nil {
log.Debugf("Applying override: %s", overrideFile)
overrides, err := loadFile(overrideFile, 0, o)
if err != nil {
panic(err)
}
if len(overrides) > 1 {
panic(fmt.Errorf("multiple documents in override %s", overrideFile))
}
o.mergeWith(overrides[0])
}
o.fixFile()
}
cfgs = append(cfgs, o)
_ = os.Chdir(currdir)
}
}
return cfgs
}