-
Notifications
You must be signed in to change notification settings - Fork 2
/
config.go
139 lines (115 loc) · 2.81 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
package main
import (
"fmt"
"os"
"strconv"
"strings"
)
type Config struct {
GitLab struct {
URL string `yaml:"url"`
Token string `yaml:"token"`
} `yaml:"gitlab"`
Slack struct {
WebhookURL string `yaml:"webhook_url"`
} `yaml:"slack"`
Projects []ConfigProject
Groups []ConfigGroup
}
type ConfigGroup struct {
ID int `yaml:"id"`
}
type ConfigProject struct {
ID int `yaml:"id"`
}
type Env interface {
Getenv(key string) string
}
type OsEnv struct{}
func (e *OsEnv) Getenv(key string) string {
return os.Getenv(key)
}
func loadConfig(env Env) (*Config, error) {
configPath := env.Getenv("CONFIG_PATH")
if configPath == "" {
configPath = "config.yaml"
}
config := &Config{}
var err error
if _, err := os.Stat(configPath); err == nil {
config, err = readConfig(configPath)
if err != nil {
return nil, fmt.Errorf("Error reading configuration file: %v\n", err)
}
}
if env := env.Getenv("PROJECTS"); env != "" {
config.Projects, err = parseIDsAsConfigProjects(env)
if err != nil {
return nil, fmt.Errorf("Error parsing PROJECTS environment variable: %v\n", err)
}
}
if env := env.Getenv("GROUPS"); env != "" {
config.Groups, err = parseIDsAsConfigGroups(env)
if err != nil {
return nil, fmt.Errorf("Error parsing GROUPS environment variable: %v\n", err)
}
}
gitlabURL := env.Getenv("GITLAB_URL")
if gitlabURL != "" {
config.GitLab.URL = gitlabURL
}
if config.GitLab.URL == "" {
config.GitLab.URL = "https://gitlab.com"
}
gitlabToken := env.Getenv("GITLAB_TOKEN")
if gitlabToken != "" {
config.GitLab.Token = gitlabToken
}
if config.GitLab.Token == "" {
return nil, fmt.Errorf("GITLAB_TOKEN environment variable is required")
}
slackWebhookURL := env.Getenv("SLACK_WEBHOOK_URL")
if slackWebhookURL != "" {
config.Slack.WebhookURL = slackWebhookURL
}
if config.Slack.WebhookURL == "" {
return nil, fmt.Errorf("SLACK_WEBHOOK_URL environment variable is required")
}
if len(config.Projects) == 0 && len(config.Groups) == 0 {
return nil, fmt.Errorf("Neither groups nor projects were provided")
}
return config, nil
}
func parseIDsAsConfigProjects(env string) ([]ConfigProject, error) {
ids, err := parseIDs(env)
if err != nil {
return nil, err
}
var projects []ConfigProject
for _, id := range ids {
projects = append(projects, ConfigProject{ID: id})
}
return projects, nil
}
func parseIDsAsConfigGroups(env string) ([]ConfigGroup, error) {
ids, err := parseIDs(env)
if err != nil {
return nil, err
}
var groups []ConfigGroup
for _, id := range ids {
groups = append(groups, ConfigGroup{ID: id})
}
return groups, nil
}
func parseIDs(env string) ([]int, error) {
var ids []int
for _, idStr := range strings.Split(env, ",") {
id, err := strconv.Atoi(idStr)
if err != nil {
return nil, err
}
ids = append(ids, id)
}
return ids, nil
}