/
config.go
48 lines (41 loc) · 1.07 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
package plugins
import (
"os"
"github.com/go-playground/validator/v10"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
)
// representing the configuration file
type Config struct {
Plugins Plugin `yaml:"plugins"`
RescheduleEvery int `yaml:"reschedule_seconds" validate:"min=60,max=1200"`
Database string `yaml:"database"`
Size int64 `yaml:"num_notifications" validate:"min=5,max=60"`
}
// plugins
type Plugin struct {
GitHub GitHubConfig `yaml:"github,omitempty"`
URLs []URLConfig `yaml:"webpages,omitempty"`
Jira JiraConfig `yaml:"jira,omitempty"`
}
type PluginBase struct {
Name string `yaml:"name" validate:"required"`
}
// NewConfigFromBytes returns a Config object, read from the YAML byte stream
func NewConfigFromBytes(b []byte) (*Config, error) {
cfg := Config{}
err := yaml.Unmarshal(b, &cfg)
if err != nil {
return nil, err
}
v := validator.New()
err = v.Struct(cfg)
if err != nil {
errors := err.(validator.ValidationErrors)
for _, e := range errors {
log.Error(e)
}
os.Exit(3)
}
return &cfg, err
}