-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
167 lines (148 loc) · 3.79 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
package main
import (
"fmt"
"io/ioutil"
"os"
"regexp"
"time"
"github.com/codegangsta/cli"
"github.com/pkg/errors"
)
func checkRequiredArguments(c *cli.Context) error {
if c.String("user") == "" {
return errors.New("please set user name")
}
if c.String("repository") == "" {
return errors.New("Please set repository name")
}
if c.String("github_access_token") == "" {
return errors.New("Please set GitHub access token")
}
return nil
}
func commandFlags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "user, u",
Value: "",
Usage: "GitHub user name",
},
cli.StringFlag{
Name: "repository, r",
Value: "",
Usage: "GitHub repository name",
},
cli.StringFlag{
Name: "report_file, f",
Value: "",
Usage: "The generated JSON file in Gradle Versions Plugin.",
},
cli.StringFlag{
Name: "weekday",
Value: "",
Usage: "Weekday of run the command('Sunday', 'Monday', ...)",
EnvVar: "WEEKDAY",
},
cli.StringFlag{
Name: "github_access_token",
Value: "",
Usage: "GitHub access token",
EnvVar: "GITHUB_ACCESS_TOKEN",
},
cli.StringFlag{
Name: "exclude, e",
Value: "",
Usage: "Exclude /regexp/ from report.",
},
cli.StringFlag{
Name: "circleci_api_token",
Value: "",
Usage: "CiecleCI API token",
EnvVar: "CIRCLECI_API_TOKEN",
},
}
}
func isRunDay(today, weekday string) bool {
if weekday == "" || today == weekday {
return true
}
return false
}
func main() {
var reportData []byte
var excludePattern *regexp.Regexp
app := cli.NewApp()
app.Name = "gradle-update-notifier"
app.Usage = "notify gradle update"
app.Version = "0.2.0"
app.Flags = commandFlags()
app.Action = func(c *cli.Context) error {
weekday := c.String("weekday")
today := time.Now().Weekday().String()
if !isRunDay(today, weekday) {
fmt.Printf("Today is %s. It is set to be executed in %s.\n", today, weekday)
return nil
}
err := checkRequiredArguments(c)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
if c.String("exclude") != "" {
excludePattern, err = regexp.Compile(c.String("exclude"))
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
}
if c.String("report_file") == "" {
if c.String("circleci_api_token") == "" {
return cli.NewExitError("Please set CircleCI API token.", 1)
}
reportData, err = readReportFileFromCircleCI(c.String("circleci_api_token"), c.String("user"), c.String("repository"))
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
} else {
reportData, err = ioutil.ReadFile(c.String("report_file"))
if err != nil {
return cli.NewExitError(errors.Wrap(err, "report file read error").Error(), 1)
}
}
report, err := parse(reportData, excludePattern)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
err = reportToGithub(report, c.String("github_access_token"), c.String("user"), c.String("repository"))
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}
app.Commands = []cli.Command{
{
Name: "build",
Usage: "Triggers a new CircleCI build",
Flags: commandFlags(),
Action: func(c *cli.Context) error {
weekday := c.String("weekday")
today := time.Now().Weekday().String()
if !isRunDay(today, weekday) {
fmt.Printf("Today is %s. It is set to be executed in %s.\n", today, weekday)
return nil
}
err := checkRequiredArguments(c)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
if c.String("circleci_api_token") == "" {
return cli.NewExitError("Please set CircleCI API token.", 1)
}
err = triggerBuild(c.String("circleci_api_token"), c.String("user"), c.String("repository"))
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
},
},
}
app.Run(os.Args)
}