This repository has been archived by the owner on May 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.go
172 lines (152 loc) · 5.15 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strings"
//"github.com/mitchellh/mapstructure"
)
// Config contains the settings from the user
type Config struct {
GitHubToken string `json:"github_token"`
GitHubOrganisations []string `json:"github_organisations"`
GitHubUsers []string `json:"github_users"`
GitHubRepos []string `json:"github_repos"`
GitLabToken string `json:"gitlab_token"`
GitLabRepos []string `json:"gitlab_repos"`
GitlabURL string `json:"gitlab_url"`
SlackToken string `json:"slack_token"`
SlackChannel string `json:"slack_channel"`
Filters *Filters `json:"filters"`
}
func newConfig(filePath string) (*Config, error) {
config := &Config{
Filters: &Filters{},
}
// the config Filters is an slice of interfaces, so we need to manually set defaults and add them to the Config
type filters struct {
Users UserFilter `json:"users"`
WIP WIPFilter `json:"wip"`
Review ReviewFilter `json:"review"`
}
filterConfig := struct {
Filters filters
}{
// default filters
Filters: filters{
WIP: true,
Review: true,
},
}
if filePath != "" {
file, err := ioutil.ReadFile(filePath)
if err != nil {
return config, fmt.Errorf("Error during config read: %s", err)
}
// populate as much as possible into the config
if err := json.Unmarshal(file, &config); err != nil {
return config, fmt.Errorf("Error during config read: %s", err)
}
if err := json.Unmarshal(file, &filterConfig); err != nil {
return config, fmt.Errorf("Error during config read: %s", err)
}
}
if os.Getenv("GITHUB_TOKEN") != "" {
config.GitHubToken = os.Getenv("GITHUB_TOKEN")
}
if os.Getenv("GITHUB_ORGANISATIONS") != "" {
config.GitHubOrganisations = strings.Split(os.Getenv("GITHUB_ORGANISATIONS"), ",")
}
if os.Getenv("GITHUB_USERS") != "" {
config.GitHubUsers = strings.Split(os.Getenv("GITHUB_USERS"), ",")
}
if os.Getenv("GITHUB_REPOS") != "" {
config.GitHubRepos = strings.Split(os.Getenv("GITHUB_REPOS"), ",")
}
if os.Getenv("GITLAB_TOKEN") != "" {
config.GitLabToken = os.Getenv("GITLAB_TOKEN")
}
if os.Getenv("GITLAB_REPOS") != "" {
config.GitLabRepos = strings.Split(os.Getenv("GITLAB_REPOS"), ",")
}
if os.Getenv("GITLAB_URL") != "" {
config.GitlabURL = os.Getenv("GITLAB_URL")
}
if os.Getenv("SLACK_TOKEN") != "" {
config.SlackToken = os.Getenv("SLACK_TOKEN")
}
if os.Getenv("SLACK_CHANNEL") != "" {
config.SlackChannel = os.Getenv("SLACK_CHANNEL")
}
if os.Getenv("FILTER_USERS") != "" {
filterConfig.Filters.Users = strings.Split(os.Getenv("FILTER_USERS"), ",")
}
if os.Getenv("FILTER_WIP") != "" {
filterConfig.Filters.WIP = os.Getenv("FILTER_WIP") == "true"
}
if os.Getenv("FILTER_REVIEW") != "" {
filterConfig.Filters.Review = os.Getenv("FILTER_REVIEW") == "true"
}
config.Filters.Add(filterConfig.Filters.Users)
config.Filters.Add(filterConfig.Filters.Review)
config.Filters.Add(filterConfig.Filters.WIP)
config.GitHubRepos = deduplicate(config.GitHubRepos)
config.GitLabRepos = deduplicate(config.GitLabRepos)
return config, nil
}
func deduplicate(s []string) []string {
m := make(map[string]bool)
for _, v := range s {
if _, seen := m[v]; !seen {
s[len(m)] = v
m[v] = true
}
}
s = s[:len(m)]
return s
}
func (c *Config) validate() []error {
var errors []error
if c.SlackToken == "" {
errors = append(errors, fmt.Errorf("Slack token cannot be empty"))
}
if c.SlackChannel == "" {
errors = append(errors, fmt.Errorf("Slack channel cannot be empty"))
}
return errors
}
func configHelp() {
fmt.Fprintln(os.Stderr, "\npurr requrires configuration to be either in a config file or set the ENV")
fmt.Fprintln(os.Stderr, "\nThe configuration file (--config) looks like this:")
exampleConfig := &Config{
GitHubToken: "secret_token",
GitHubOrganisations: []string{"facebook"},
GitHubUsers: []string{"stojg"},
GitHubRepos: []string{"user1/repo1", "user2/repo1"},
GitLabToken: "secret_token",
GitLabRepos: []string{"project1/repo1", "project2/repo1"},
GitlabURL: "https://www.example.com",
SlackToken: "secret_token",
SlackChannel: "myteamchat",
Filters: &Filters{},
}
b, err := json.MarshalIndent(exampleConfig, "", " ")
if err != nil {
fmt.Fprintf(os.Stderr, "error: %s", err)
}
fmt.Fprintf(os.Stderr, "\n%s\n\n", b)
fmt.Fprint(os.Stderr, "The above configuration can be overridden with ENV variables:\n\n")
fmt.Fprintln(os.Stderr, " * GITHUB_TOKEN")
fmt.Fprintln(os.Stderr, " * GITHUB_ORGANISATIONS - comma separated list")
fmt.Fprintln(os.Stderr, " * GITHUB_USERS - comma separated list")
fmt.Fprintln(os.Stderr, " * GITHUB_REPOS - comma separated list")
fmt.Fprintln(os.Stderr, " * GITLAB_TOKEN")
fmt.Fprintln(os.Stderr, " * GITLAB_URL")
fmt.Fprintln(os.Stderr, " * GITLAB_REPOS - comma separated list")
fmt.Fprintln(os.Stderr, " * SLACK_TOKEN")
fmt.Fprintln(os.Stderr, " * SLACK_CHANNEL")
fmt.Fprintln(os.Stderr, " * FILTER_USERS - comma separated list")
fmt.Fprintln(os.Stderr, " * FILTER_WIP - 'true' or 'false'")
fmt.Fprintln(os.Stderr, " * FILTER_REVIEW - 'true' or 'false'")
}