-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathconfig.go
164 lines (148 loc) · 3.84 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
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
package ecschedule
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"strings"
"text/template"
"github.com/goccy/go-yaml"
"github.com/google/go-jsonnet"
gc "github.com/kayac/go-config"
"github.com/winebarrel/cronplan"
)
const defaultRole = "ecsEventsRole"
const (
jsonnetExt = ".jsonnet"
jsonExt = ".json"
)
// BaseConfig baseconfig
type BaseConfig struct {
Region string `yaml:"region" json:"region"`
Cluster string `yaml:"cluster" json:"cluster"`
AccountID string `yaml:"-" json:"-"`
}
// Config config
type Config struct {
Role string `yaml:"role,omitempty" json:"role,omitempty"`
*BaseConfig `yaml:",inline" json:",inline"`
Rules []*Rule `yaml:"rules" json:"rules"`
Plugins []*Plugin `yaml:"plugins,omitempty" json:"plugins,omitempty"`
templateFuncs []template.FuncMap
dir string
}
// GetRuleByName gets rule by name
func (c *Config) GetRuleByName(name string) *Rule {
for _, r := range c.Rules {
if r.Name == name {
return r
}
}
return nil
}
func (c *Config) setupPlugins(ctx context.Context) error {
for _, p := range c.Plugins {
if err := p.setup(ctx, c); err != nil {
return err
}
}
return nil
}
func (c *Config) cronValidate() error {
// XXX: I'd like to use multiple errors here and format the error messages at the very end.
var errMsgs []string
for _, r := range c.Rules {
err := validateCronExpression(r.ScheduleExpression)
if err != nil {
errMsgs = append(errMsgs, fmt.Sprintf("\trule %q: %s", r.Name, err))
}
}
if len(errMsgs) > 0 {
return fmt.Errorf("schedule expression validation errors:\n%s", strings.Join(errMsgs, "\n"))
}
return nil
}
func validateCronExpression(exp string) error {
if strings.HasPrefix(exp, "rate(") && strings.HasSuffix(exp, ")") {
return nil
}
strippedExp := strings.TrimSuffix(strings.TrimPrefix(exp, "cron("), ")")
// 6 means `len("cron(") + len("(")`
if len(strippedExp)+6 != len(exp) {
return fmt.Errorf("invalid expression: %q", exp)
}
if strippedExp != strings.TrimSpace(strippedExp) {
return fmt.Errorf(
"trailing or leading spaces are not allowed inside parentheses: %q", exp)
}
_, err := cronplan.Parse(strippedExp)
if err != nil {
return err
}
return nil
}
// LoadConfig loads config
func LoadConfig(ctx context.Context, r io.Reader, accountID string, confPath string) (*Config, error) {
c := Config{}
bs, ext, err := readConfigFile(r, confPath)
if err != nil {
return nil, err
}
bs, err = envReplacer(bs)
if err != nil {
return nil, err
}
if err := unmarshalConfig(bs, &c, ext); err != nil {
return nil, err
}
if err := c.cronValidate(); err != nil {
return nil, err
}
c.AccountID = accountID
if err := c.setupPlugins(ctx); err != nil {
return nil, err
}
c.dir = filepath.Dir(confPath)
loader := gc.New()
for _, f := range c.templateFuncs {
loader.Funcs(f)
}
// recover tfstate variable
bs = tfstateRecover(bs)
// recover ssm variable
bs = ssmRecover(bs)
bs, err = loader.ReadWithEnvBytes(bs)
if err != nil {
return nil, err
}
if err := unmarshalConfig(bs, &c, ext); err != nil {
return nil, err
}
for _, r := range c.Rules {
r.mergeBaseConfig(c.BaseConfig, c.Role)
}
return &c, nil
}
// unmarshalConfig unmarshal json or yaml file
func unmarshalConfig(bs []byte, c *Config, ext string) error {
if ext == jsonExt {
return json.Unmarshal(bs, c)
}
// as a YAML file if the file type cannot be determined from the extension (e.g. .ecschedule, ecschedule.cfg)
return yaml.Unmarshal(bs, c)
}
func readConfigFile(r io.Reader, confPath string) ([]byte, string, error) {
ext := filepath.Ext(confPath)
if ext == jsonnetExt {
vm := jsonnet.MakeVM()
bs, err := vm.EvaluateFile(confPath)
if err != nil {
return nil, ext, fmt.Errorf("failed to evaluate jsonnet file: %w", err)
}
return []byte(bs), jsonExt, err
}
bs, err := ioutil.ReadAll(r)
return bs, ext, err
}