-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
59 lines (54 loc) · 1.29 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
package main
import (
"fmt"
"github.com/gonuts/yaml"
"io/ioutil"
"os"
)
type ProcessConfig struct {
Name string `name`
Command string `command`
Streams []string `streams`
Restart bool `restart`
After []string `after`
}
type Config struct {
Processes []ProcessConfig `processes`
}
func ParseConfig(filename string) (config *Config, err error) {
f, err := os.Open(filename)
if err != nil {
return nil, NewError("Could not open file: "+filename, err)
}
bytes, err := ioutil.ReadAll(f)
if err != nil {
return nil, NewError("Could not read file: "+filename, err)
}
config = new(Config)
err = yaml.Unmarshal(bytes, config)
if err != nil {
return nil, NewError("Could not parse YAML config: "+filename, err)
}
return
}
func validateConfig(config *Config) error {
for _, process := range config.Processes {
for _, depend := range process.After {
n, t := parseAfter(depend)
if !(t == "started" || t == "finished") {
return NewError(fmt.Sprintf("Invalid dependency type %s for %s", t, process.Name), nil)
}
valid := false
for _, process := range config.Processes {
if n == process.Name {
valid = true
break
}
}
if !valid {
return NewError(fmt.Sprintf("Non-existent dependency %s for %s", n, process.Name), nil)
}
}
}
return nil
}