-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
87 lines (70 loc) · 2.35 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
package main
import (
"github.com/BurntSushi/toml"
"log"
)
type HandlerType string
const (
EMAIL HandlerType = "email"
FOGBUGZ_RESOLVE HandlerType = "fogbugz_resolve"
)
type config struct {
BindAddress string `toml:"bind_address"`
BindPort int `toml:"bind_port"`
RepoConfigs map[string]repoConfig `toml:"repos"`
FogbugzDefaultConfig FogbugzConfig `toml:"fogbugz_default_config"`
}
type repoConfig struct {
Name string `toml:"name"`
FullName string `toml:"full_name"`
ReleaseHandlerConfigs map[string]handlerConfig `toml:"release_handlers"`
PushHandlerConfigs map[string]handlerConfig `toml:"push_handlers"`
}
type handlerConfig struct {
HandlerType HandlerType `toml:"type"`
HandlerConfig toml.Primitive `toml:"config"`
}
func (repoConfig *repoConfig) buildRepo(fogbugzDefaultConfig FogbugzConfig) (*Repo, error) {
releaseHandlers := make([]ReleaseHandler, 0)
for _, releaseHandlerConfig := range repoConfig.ReleaseHandlerConfigs {
switch releaseHandlerConfig.HandlerType {
case EMAIL:
releaseHandler, err := NewEmailReleaseHandler(releaseHandlerConfig.HandlerConfig)
if err != nil {
return nil, err
}
releaseHandlers = append(releaseHandlers, releaseHandler)
}
}
pushHandlers := make([]PushHandler, 0)
for _, pushHandlerConfig := range repoConfig.PushHandlerConfigs {
switch pushHandlerConfig.HandlerType {
case FOGBUGZ_RESOLVE:
pushHandler, err := NewFogbugzResolveHandler(fogbugzDefaultConfig, pushHandlerConfig.HandlerConfig)
if err != nil {
return nil, err
}
pushHandlers = append(pushHandlers, pushHandler)
}
}
return &Repo{repoConfig.Name, repoConfig.FullName, releaseHandlers, pushHandlers}, nil
}
func (handler *HookHubHandler) ParseConfig(configFile string) error {
var config config
if _, err := toml.DecodeFile(configFile, &config); err != nil {
return err
}
repos := make(map[string]*Repo)
for _, repoConfig := range config.RepoConfigs {
repo, err := repoConfig.buildRepo(config.FogbugzDefaultConfig)
if err != nil {
return err
}
log.Printf("Configuring %s", repoConfig.FullName)
repos[repoConfig.FullName] = repo
}
handler.bindAddress = config.BindAddress
handler.bindPort = config.BindPort
handler.repos = repos
return nil
}