-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
83 lines (69 loc) · 1.96 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
package main
import (
"io/ioutil"
"github.com/pkg/errors"
"gopkg.in/yaml.v2"
)
// DefaultPort is used whtn no valid port is found on the config file
const DefaultPort = 5000
// Replacement defines the source and target URL host, along with an optional path
type Replacement struct {
HostSource string `yaml:"source"`
HostTarget string `yaml:"target"`
Path string `yaml:"path"`
}
// Config holds the trigger definitions
type Config struct {
Cmd struct {
Port int `yaml:"port"`
Verbose bool `yaml:"verbose"`
} `yaml:"cmd"`
Replacements []Replacement `yaml:"replacements"`
}
// ReadConfig parses `config.yaml` and returns a struct with the desired config
func ReadConfig(filePath string) (Config, error) {
conf := Config{}
// Read config.yaml
data, err := ioutil.ReadFile(filePath)
if err != nil {
return conf, err
}
err = yaml.Unmarshal([]byte(data), &conf)
if err != nil {
return conf, err
}
if conf.Cmd.Port == 0 {
conf.Cmd.Port = DefaultPort
}
if len(conf.Replacements) < 1 {
return conf, errors.Errorf("No replacements are defined")
}
// Append / at the end of the paths
for _, replacement := range conf.Replacements {
if replacement.Path == "" {
replacement.Path = "/"
}
}
err = checkConfig(conf)
if err != nil {
return conf, err
}
return conf, nil
}
func checkConfig(conf Config) error {
var srcReplacements []string
for idx, replacement := range conf.Replacements {
if replacement.HostSource == "" {
return errors.Errorf("[CONFIG] The source replacement at index %d is empty", idx)
} else if replacement.HostTarget == "" {
return errors.Errorf("[CONFIG] The target replacement at index %d is empty", idx)
}
for _, prevSrcReplacement := range srcReplacements {
if prevSrcReplacement == replacement.HostSource {
return errors.Errorf("[CONFIG] The source replacement %s is defined multiple times", prevSrcReplacement)
}
}
srcReplacements = append(srcReplacements, replacement.HostSource)
}
return nil
}