-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
49 lines (38 loc) · 1018 Bytes
/
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
package main
import (
"bytes"
validation "github.com/go-ozzo/ozzo-validation"
"github.com/go-ozzo/ozzo-validation/is"
"gopkg.in/yaml.v2"
)
type Config struct {
Commands []Command `yaml:"commands"`
}
func (c Config) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.Commands, validation.Required),
)
}
type Command struct {
Command string `yaml:"command"`
ApiUrl string `yaml:"apiUrl"`
Template string `yaml:"template"`
}
func (c Command) Validate() error {
return validation.ValidateStruct(&c,
validation.Field(&c.Command, validation.Required, is.Alphanumeric),
validation.Field(&c.ApiUrl, validation.Required, is.URL),
validation.Field(&c.Template, validation.Required),
)
}
func parseConfig(contents []byte) (*Config, error) {
cfg := &Config{}
contents = bytes.TrimPrefix(contents, []byte("---"))
if err := yaml.Unmarshal(contents, &cfg); err != nil {
return cfg, err
}
if err := cfg.Validate(); err != nil {
return cfg, err
}
return cfg, nil
}