forked from mercari/tfnotify
/
main.go
212 lines (196 loc) · 4.65 KB
/
main.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
package main
import (
"fmt"
"os"
"strings"
"github.com/mercari/tfnotify/config"
"github.com/mercari/tfnotify/notifier"
"github.com/mercari/tfnotify/notifier/github"
"github.com/mercari/tfnotify/notifier/slack"
"github.com/mercari/tfnotify/terraform"
"github.com/urfave/cli"
)
const (
name = "tfnotify"
description = "Notify the execution result of terraform command"
version = "0.1.3"
)
type tfnotify struct {
config config.Config
context *cli.Context
parser terraform.Parser
template terraform.Template
}
// Run sends the notification with notifier
func (t *tfnotify) Run() error {
ciname := t.config.CI
if t.context.GlobalString("ci") != "" {
ciname = t.context.GlobalString("ci")
}
ciname = strings.ToLower(ciname)
var ci CI
var err error
switch ciname {
case "circleci", "circle-ci":
ci, err = circleci()
if err != nil {
return err
}
case "travis", "travisci", "travis-ci":
ci, err = travisci()
if err != nil {
return err
}
case "":
return fmt.Errorf("CI service: required (e.g. circleci)")
default:
return fmt.Errorf("CI service %v: not supported yet", ci)
}
selectedNotifier := t.config.GetNotifierType()
if t.context.GlobalString("notifier") != "" {
selectedNotifier = t.context.GlobalString("notifier")
}
var notifier notifier.Notifier
switch selectedNotifier {
case "github":
client, err := github.NewClient(github.Config{
Token: t.config.Notifier.Github.Token,
Owner: t.config.Notifier.Github.Repository.Owner,
Repo: t.config.Notifier.Github.Repository.Name,
PR: github.PullRequest{
Revision: ci.PR.Revision,
Number: ci.PR.Number,
Message: t.context.String("message"),
},
CI: ci.URL,
Parser: t.parser,
Template: t.template,
})
if err != nil {
return err
}
notifier = client.Notify
case "slack":
client, err := slack.NewClient(slack.Config{
Token: t.config.Notifier.Slack.Token,
Channel: t.config.Notifier.Slack.Channel,
Botname: t.config.Notifier.Slack.Bot,
Message: t.context.String("message"),
CI: ci.URL,
Parser: t.parser,
Template: t.template,
})
if err != nil {
return err
}
notifier = client.Notify
case "":
return fmt.Errorf("notifier is missing")
default:
return fmt.Errorf("%s: not supported notifier yet", t.context.GlobalString("notifier"))
}
if notifier == nil {
return fmt.Errorf("no notifier specified at all")
}
return NewExitError(notifier.Notify(tee(os.Stdin, os.Stdout)))
}
func main() {
app := cli.NewApp()
app.Name = name
app.Usage = description
app.Version = version
app.Flags = []cli.Flag{
cli.StringFlag{Name: "ci", Usage: "name of CI to run tfnotify"},
cli.StringFlag{Name: "config", Usage: "config path"},
cli.StringFlag{Name: "notifier", Usage: "notification destination"},
}
app.Commands = []cli.Command{
{
Name: "fmt",
Usage: "Parse stdin as a fmt result",
Action: cmdFmt,
Flags: []cli.Flag{
cli.StringFlag{
Name: "message, m",
Usage: "Specify the message to use for notification",
},
},
},
{
Name: "plan",
Usage: "Parse stdin as a plan result",
Action: cmdPlan,
Flags: []cli.Flag{
cli.StringFlag{
Name: "message, m",
Usage: "Specify the message to use for notification",
},
},
},
{
Name: "apply",
Usage: "Parse stdin as a apply result",
Action: cmdApply,
Flags: []cli.Flag{
cli.StringFlag{
Name: "message, m",
Usage: "Specify the message to use for notification",
},
},
},
}
err := app.Run(os.Args)
os.Exit(HandleExit(err))
}
func newConfig(ctx *cli.Context) (cfg config.Config, err error) {
confPath, err := cfg.Find(ctx.GlobalString("config"))
if err != nil {
return cfg, err
}
if err := cfg.LoadFile(confPath); err != nil {
return cfg, err
}
if err := cfg.Validation(); err != nil {
return cfg, err
}
return cfg, nil
}
func cmdFmt(ctx *cli.Context) error {
cfg, err := newConfig(ctx)
if err != nil {
return err
}
t := &tfnotify{
config: cfg,
context: ctx,
parser: terraform.NewFmtParser(),
template: terraform.NewFmtTemplate(cfg.Terraform.Fmt.Template),
}
return t.Run()
}
func cmdPlan(ctx *cli.Context) error {
cfg, err := newConfig(ctx)
if err != nil {
return err
}
t := &tfnotify{
config: cfg,
context: ctx,
parser: terraform.NewPlanParser(),
template: terraform.NewPlanTemplate(cfg.Terraform.Plan.Template),
}
return t.Run()
}
func cmdApply(ctx *cli.Context) error {
cfg, err := newConfig(ctx)
if err != nil {
return err
}
t := &tfnotify{
config: cfg,
context: ctx,
parser: terraform.NewApplyParser(),
template: terraform.NewApplyTemplate(cfg.Terraform.Apply.Template),
}
return t.Run()
}