-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
93 lines (77 loc) · 1.9 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
package main
import (
"gopkg.in/yaml.v2"
"io/ioutil"
"sort"
"strings"
)
type Config interface {
AddDefinedTask(DefinedTask)
DefinedTasks() []DefinedTask
ShowAllDefinedTasks()
}
type ConfigImpl struct {
path string
definedTasks []DefinedTask
}
type Node interface{}
type Document map[string]Node
func (c *ConfigImpl) AddDefinedTask(task DefinedTask) {
c.definedTasks = append(c.definedTasks, task)
}
func (c *ConfigImpl) DefinedTasks() []DefinedTask {
return c.definedTasks
}
func (c *ConfigImpl) ShowAllDefinedTasks() {
Printf("All defined tasks:\n")
for _, task := range c.DefinedTasks() {
Printf("%s", task.Name())
}
}
func (c *ConfigImpl) sortDefinedTasks() {
sort.Slice(c.definedTasks, func(i int, j int) bool {
return c.definedTasks[i].Name() < c.definedTasks[j].Name()
})
}
var ReadConfig = func(path string) (string, error) {
if buf, err := ioutil.ReadFile(path); err != nil {
Error("Config file read error. path: %s", path)
return "", err
} else {
return string(buf), nil
}
}
var ParseConfig = func(buf string) (Config, error) {
config := &ConfigImpl{}
var document = make(Document)
if err := yaml.Unmarshal([]byte(buf), &document); err != nil {
Error(err.Error())
return nil, err
}
var task DefinedTask
for taskName, rootNode := range document {
if strings.HasPrefix(taskName, "_") {
continue
}
if command, ok := rootNode.(string); ok {
task = NewDefinedTask(taskName)
task.AddCommand(command)
config.AddDefinedTask(task)
} else if node, ok := rootNode.(Node); ok {
task = NewDefinedTask(taskName)
parseNode(task, node)
config.AddDefinedTask(task)
}
}
config.sortDefinedTasks()
return config, nil
}
func parseNode(task DefinedTask, node Node) {
if command, ok := node.(string); ok {
task.AddCommand(command)
} else if list, ok := node.([]interface{}); ok {
for _, childNode := range list {
parseNode(task, childNode)
}
}
}