-
Notifications
You must be signed in to change notification settings - Fork 55
/
load.go
136 lines (112 loc) · 3.12 KB
/
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
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
package config
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/ghodss/yaml"
"github.com/hashicorp/go-multierror"
)
var sep = regexp.MustCompile("(?:^|\\s*\n)---\\s*")
// LoadClusterFile loads a ClusterConfig from a path to a YAML file.
func LoadClusterFile(path string, expandEnv bool) (ClusterConfig, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return ClusterConfig{}, err
}
if expandEnv {
contents = []byte(os.ExpandEnv(string(contents)))
}
absPath, err := filepath.Abs(path)
if err != nil {
return ClusterConfig{}, err
}
config, err := LoadClusterBytes(contents)
if err != nil {
return ClusterConfig{}, err
}
config.RootDir = filepath.Dir(absPath)
return config, nil
}
// LoadClusterBytes loads a ClusterConfig from YAML bytes.
func LoadClusterBytes(contents []byte) (ClusterConfig, error) {
config := ClusterConfig{}
err := unmarshalYAMLStrict(contents, &config)
return config, err
}
// LoadTopicsFile loads one or more TopicConfigs from a path to a YAML file.
func LoadTopicsFile(path string) ([]TopicConfig, error) {
contents, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
contents = []byte(os.ExpandEnv(string(contents)))
trimmedFile := strings.TrimSpace(string(contents))
topicStrs := sep.Split(trimmedFile, -1)
topicConfigs := []TopicConfig{}
for _, topicStr := range topicStrs {
topicStr = strings.TrimSpace(topicStr)
if isEmpty(topicStr) {
continue
}
topicConfig, err := LoadTopicBytes([]byte(topicStr))
if err != nil {
return nil, err
}
topicConfigs = append(topicConfigs, topicConfig)
}
return topicConfigs, nil
}
// LoadTopicBytes loads a TopicConfig from YAML bytes.
func LoadTopicBytes(contents []byte) (TopicConfig, error) {
config := TopicConfig{}
err := unmarshalYAMLStrict(contents, &config)
return config, err
}
// CheckConsistency verifies that the argument topic config is consistent with the argument
// cluster, e.g. has the same environment and region, etc.
func CheckConsistency(topicConfig TopicConfig, clusterConfig ClusterConfig) error {
var err error
if topicConfig.Meta.Cluster != clusterConfig.Meta.Name {
err = multierror.Append(
err,
errors.New("Topic cluster name does not match name in cluster config"),
)
}
if topicConfig.Meta.Environment != clusterConfig.Meta.Environment {
err = multierror.Append(
err,
errors.New("Topic environment does not match cluster environment"),
)
}
if topicConfig.Meta.Region != clusterConfig.Meta.Region {
err = multierror.Append(
err,
errors.New("Topic region does not match cluster region"),
)
}
return err
}
func isEmpty(contents string) bool {
lines := strings.Split(contents, "\n")
for _, line := range lines {
trimmedLine := strings.TrimSpace(line)
if len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") {
return false
}
}
return true
}
func unmarshalYAMLStrict(y []byte, o interface{}) error {
jsonBytes, err := yaml.YAMLToJSON(y)
if err != nil {
return err
}
dec := json.NewDecoder(bytes.NewReader(jsonBytes))
dec.DisallowUnknownFields()
return dec.Decode(o)
}