-
Notifications
You must be signed in to change notification settings - Fork 6
/
manifest.go
268 lines (220 loc) · 6.47 KB
/
manifest.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package manifest
import (
"encoding/json"
"fmt"
"log"
"os"
"reflect"
"regexp"
"strconv"
"strings"
"github.com/fatih/color"
"github.com/servehub/serve/manifest/processor"
"github.com/servehub/utils"
"github.com/servehub/utils/gabs"
)
type Manifest struct {
tree *gabs.Container
}
func (m Manifest) String() string {
return m.tree.StringIndent("", " ")
}
func (m Manifest) Unwrap() interface{} {
return m.tree.Data()
}
func (m Manifest) Has(path string) bool {
v := m.tree.Path(path).Data()
return v != nil && v != ""
}
func (m Manifest) GetString(path string) string {
return fmt.Sprintf("%v", m.tree.Path(path).Data())
}
func (m Manifest) GetStringOr(path string, defaultVal string) string {
if m.tree.ExistsP(path) {
return m.GetString(path)
}
return defaultVal
}
func (m Manifest) GetFloat(path string) float64 {
f, err := strconv.ParseFloat(m.GetString(path), 64)
if err != nil {
log.Fatalf("Error on parse float64 '%v' from: %v", path, m.GetString(path))
}
return f
}
func (m Manifest) GetInt(path string) int {
i, err := strconv.Atoi(m.GetString(path))
if err != nil {
log.Fatalf("Error on parse integer '%v' from: %v", path, m.GetString(path))
}
return i
}
func (m Manifest) GetIntOr(path string, defaultVal int) int {
if m.tree.ExistsP(path) {
return m.GetInt(path)
}
return defaultVal
}
func (m Manifest) GetBool(path string) bool {
return strings.ToLower(m.GetString(path)) == "true"
}
func (m Manifest) GetMap(path string) map[string]Manifest {
out := make(map[string]Manifest)
tree := m.tree
if len(path) > 0 && path != "." && path != "/" {
tree = m.tree.Path(path)
}
mmap, err := tree.ChildrenMap()
if err != nil {
log.Fatalf("Error get map '%v' from: %v. Error: %s", path, m.tree.Path(path).Data(), err)
}
for k, v := range mmap {
out[k] = Manifest{v}
}
return out
}
func (m Manifest) GetArray(path string) []Manifest {
out := make([]Manifest, 0)
arr, err := m.tree.Path(path).Children()
if err != nil {
log.Fatalf("Error get array `%v` from: %v", path, m.tree.Path(path).Data())
}
for _, v := range arr {
out = append(out, Manifest{v})
}
return out
}
func (m Manifest) GetArrayForce(path string) []interface{} {
out := make([]interface{}, 0)
arr, err := m.tree.Path(path).Children()
if err != nil && m.tree.ExistsP(path) {
arr = append(arr, m.tree.Path(path))
}
for _, v := range arr {
out = append(out, v.Data())
}
return out
}
func (m Manifest) GetTree(path string) Manifest {
return Manifest{m.tree.Path(path)}
}
func (m Manifest) Set(path string, value interface{}) {
m.tree.SetP(value, path)
}
func (m Manifest) ArrayAppend(path string, value interface{}) {
m.tree.ArrayAppendP(value, path)
}
func (m Manifest) FindPlugins(plugin string) ([]PluginData, error) {
tree := m.tree.Path(plugin)
result := make([]PluginData, 0)
if tree.Data() == nil {
return nil, fmt.Errorf("Plugin `%s` not found in manifest!", plugin)
}
if _, ok := tree.Data().([]interface{}); ok {
arr, _ := tree.Children()
for _, item := range arr {
if _, ok := item.Data().(string); ok {
result = append(result, makePluginPair(plugin, item))
} else if res, err := item.ChildrenMap(); err == nil {
if len(res) == 1 && !strings.Contains(plugin, ".") {
for subplugin, data := range res {
result = append(result, makePluginPair(plugin+"."+subplugin, data))
break
}
} else if len(res) == 0 && !PluginRegestry.Has(plugin) {
// skip sub-plugin with empty data
} else {
result = append(result, makePluginPair(plugin, item))
}
}
}
} else if PluginRegestry.Has(plugin) {
result = append(result, makePluginPair(plugin, tree))
} else if strings.HasPrefix(plugin, "command.") {
result = append(result, makePluginPair("command", tree))
} else {
log.Println(color.YellowString("Plugins for `%s` section not specified, skip...", plugin))
}
return result, nil
}
func (m Manifest) DelTree(path string) error {
return m.tree.DeleteP(path)
}
func (m Manifest) GetPluginWithData(plugin string) PluginData {
return makePluginPair(plugin, m.tree)
}
var envNameRegex = regexp.MustCompile("\\W")
func (m Manifest) ToEnvMap(prefix string) map[string]string {
result := make(map[string]string)
if children, err := m.tree.ChildrenMap(); err == nil {
for k, child := range children {
result = utils.MergeMaps(result, Manifest{child}.ToEnvMap(prefix+strings.ToUpper(envNameRegex.ReplaceAllString(k, "_"))+"_"))
}
} else if children, err := m.tree.Children(); err == nil {
for i, child := range children {
result = utils.MergeMaps(result, Manifest{child}.ToEnvMap(prefix+strconv.Itoa(i)+"_"))
}
} else if m.tree.Data() != nil {
result[prefix[:len(prefix)-1]] = fmt.Sprintf("%v", m.tree.Data())
}
return result
}
func Load(path string, plugin string, vars map[string]string) *Manifest {
tree, err := gabs.LoadYamlFile(path)
if err != nil {
log.Fatalln("Error on load file:", err)
}
for k, v := range vars {
tree.Set(v, "vars", k)
}
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
tree.Set(pair[1], "envs", pair[0])
}
// automatically append plugin with this name, if not specified explicitly
if !tree.ExistsP(plugin) && strings.Contains(plugin, ".") {
ns := strings.SplitN(plugin, ".", 2)
if array, ok := tree.Path(ns[0]).Data().([]interface{}); ok {
tmpM := &Manifest{tree}
if result, err := tmpM.FindPlugins(plugin); err != nil || len(result) == 0 {
obj := make(map[string]interface{})
obj[ns[1]] = nil
tree.Set(append(array, obj), ns[0])
}
}
}
for _, proc := range processor.GetAll() {
if err := proc.Process(tree); err != nil {
log.Fatalf("Error in processor '%v': %v. \n\nManifest: %s", reflect.ValueOf(proc).Type().Name(), err, tree.StringIndent("", " "))
}
}
return &Manifest{tree}
}
func LoadJSON(path string) *Manifest {
tree, err := gabs.ParseJSONFile(path)
if err != nil {
log.Fatalf("Error on load json file '%s': %v\n", path, err)
}
return &Manifest{tree}
}
func ParseJSON(json string) *Manifest {
tree, err := gabs.ParseJSON([]byte(json))
if err != nil {
log.Fatalf("Error on parse json '%s': %v\n", json, err)
}
return &Manifest{tree}
}
func makePluginPair(plugin string, data *gabs.Container) PluginData {
if s, ok := data.Data().(string); ok {
obj := gabs.New()
ns := strings.Split(plugin, ".")
obj.Set(s, ns[len(ns)-1])
data = obj
} else {
var cpy interface{}
bs, _ := json.Marshal(data.Data())
json.Unmarshal(bs, &cpy)
data.Set(cpy)
}
return PluginData{plugin, PluginRegestry.Get(plugin), Manifest{data}}
}