This repository has been archived by the owner on Mar 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.go
248 lines (221 loc) · 6.05 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package config
import (
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"gopkg.in/yaml.v2"
)
// Config is for tfnotify config structure
type Config struct {
CI string `yaml:"ci"`
Notifier Notifier `yaml:"notifier"`
Terraform Terraform `yaml:"terraform"`
path string
}
// Notifier is a notification notifier
type Notifier struct {
Github GithubNotifier `yaml:"github"`
Gitlab GitlabNotifier `yaml:"gitlab"`
Slack SlackNotifier `yaml:"slack"`
Typetalk TypetalkNotifier `yaml:"typetalk"`
}
// GithubNotifier is a notifier for GitHub
type GithubNotifier struct {
Token string `yaml:"token"`
BaseURL string `yaml:"base_url"`
Repository Repository `yaml:"repository"`
}
// GitlabNotifier is a notifier for GitLab
type GitlabNotifier struct {
Token string `yaml:"token"`
BaseURL string `yaml:"base_url"`
Repository Repository `yaml:"repository"`
}
// Repository represents a GitHub repository
type Repository struct {
Owner string `yaml:"owner"`
Name string `yaml:"name"`
}
// SlackNotifier is a notifier for Slack
type SlackNotifier struct {
Token string `yaml:"token"`
Channel string `yaml:"channel"`
Bot string `yaml:"bot"`
}
// TypetalkNotifier is a notifier for Typetalk
type TypetalkNotifier struct {
Token string `yaml:"token"`
TopicID string `yaml:"topic_id"`
}
// Terraform represents terraform configurations
type Terraform struct {
Default Default `yaml:"default"`
Fmt Fmt `yaml:"fmt"`
Plan Plan `yaml:"plan"`
Apply Apply `yaml:"apply"`
UseRawOutput bool `yaml:"use_raw_output,omitempty"`
}
// Default is a default setting for terraform commands
type Default struct {
Template string `yaml:"template"`
}
// Fmt is a terraform fmt config
type Fmt struct {
Template string `yaml:"template"`
}
// Plan is a terraform plan config
type Plan struct {
Template string `yaml:"template"`
WhenAddOrUpdateOnly WhenAddOrUpdateOnly `yaml:"when_add_or_update_only,omitempty"`
WhenDestroy WhenDestroy `yaml:"when_destroy,omitempty"`
WhenNoChanges WhenNoChanges `yaml:"when_no_changes,omitempty"`
WhenPlanError WhenPlanError `yaml:"when_plan_error,omitempty"`
}
// WhenAddOrUpdateOnly is a configuration to notify the plan result contains new or updated in place resources
type WhenAddOrUpdateOnly struct {
Label string `yaml:"label,omitempty"`
Color string `yaml:"label_color,omitempty"`
}
// WhenDestroy is a configuration to notify the plan result contains destroy operation
type WhenDestroy struct {
Label string `yaml:"label,omitempty"`
Template string `yaml:"template,omitempty"`
Color string `yaml:"label_color,omitempty"`
}
// WhenNoChange is a configuration to add a label when the plan result contains no change
type WhenNoChanges struct {
Label string `yaml:"label,omitempty"`
Color string `yaml:"label_color,omitempty"`
}
// WhenPlanError is a configuration to notify the plan result returns an error
type WhenPlanError struct {
Label string `yaml:"label,omitempty"`
Color string `yaml:"label_color,omitempty"`
}
// Apply is a terraform apply config
type Apply struct {
Template string `yaml:"template"`
}
// LoadFile binds the config file to Config structure
func (cfg *Config) LoadFile(path string) error {
cfg.path = path
_, err := os.Stat(cfg.path)
if err != nil {
return fmt.Errorf("%s: no config file", cfg.path)
}
raw, _ := ioutil.ReadFile(cfg.path)
return yaml.Unmarshal(raw, cfg)
}
// Validation validates config file
func (cfg *Config) Validation() error {
switch strings.ToLower(cfg.CI) {
case "":
return errors.New("ci: need to be set")
case "circleci", "circle-ci":
// ok pattern
case "gitlabci", "gitlab-ci":
// ok pattern
case "travis", "travisci", "travis-ci":
// ok pattern
case "codebuild":
// ok pattern
case "teamcity":
// ok pattern
case "drone":
// ok pattern
case "jenkins":
// ok pattern
case "github-actions":
// ok pattern
case "cloud-build", "cloudbuild":
// ok pattern
default:
return fmt.Errorf("%s: not supported yet", cfg.CI)
}
if cfg.isDefinedGithub() {
if cfg.Notifier.Github.Repository.Owner == "" {
return fmt.Errorf("repository owner is missing")
}
if cfg.Notifier.Github.Repository.Name == "" {
return fmt.Errorf("repository name is missing")
}
}
if cfg.isDefinedGitlab() {
if cfg.Notifier.Gitlab.Repository.Owner == "" {
return fmt.Errorf("repository owner is missing")
}
if cfg.Notifier.Gitlab.Repository.Name == "" {
return fmt.Errorf("repository name is missing")
}
}
if cfg.isDefinedSlack() {
if cfg.Notifier.Slack.Channel == "" {
return fmt.Errorf("slack channel id is missing")
}
}
if cfg.isDefinedTypetalk() {
if cfg.Notifier.Typetalk.TopicID == "" {
return fmt.Errorf("Typetalk topic id is missing")
}
}
notifier := cfg.GetNotifierType()
if notifier == "" {
return fmt.Errorf("notifier is missing")
}
return nil
}
func (cfg *Config) isDefinedGithub() bool {
// not empty
return cfg.Notifier.Github != (GithubNotifier{})
}
func (cfg *Config) isDefinedGitlab() bool {
// not empty
return cfg.Notifier.Gitlab != (GitlabNotifier{})
}
func (cfg *Config) isDefinedSlack() bool {
// not empty
return cfg.Notifier.Slack != (SlackNotifier{})
}
func (cfg *Config) isDefinedTypetalk() bool {
// not empty
return cfg.Notifier.Typetalk != (TypetalkNotifier{})
}
// GetNotifierType return notifier type described in Config
func (cfg *Config) GetNotifierType() string {
if cfg.isDefinedGithub() {
return "github"
}
if cfg.isDefinedGitlab() {
return "gitlab"
}
if cfg.isDefinedSlack() {
return "slack"
}
if cfg.isDefinedTypetalk() {
return "typetalk"
}
return ""
}
// Find returns config path
func (cfg *Config) Find(file string) (string, error) {
var files []string
if file == "" {
files = []string{
"tfnotify.yaml",
"tfnotify.yml",
".tfnotify.yaml",
".tfnotify.yml",
}
} else {
files = []string{file}
}
for _, file := range files {
_, err := os.Stat(file)
if err == nil {
return file, nil
}
}
return "", errors.New("config for tfnotify is not found at all")
}