-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcli.go
executable file
·217 lines (184 loc) · 5.03 KB
/
cli.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
package main
import (
"bufio"
"flag"
"fmt"
"io"
"os"
"time"
"github.com/mitchellh/colorstring"
)
// CLI has stdout/stderr's writer and Gdp's interface.
type CLI struct {
outStream io.Writer
errStream io.Writer
gdp Gdp
}
// Exit code.
const (
ExitSuccess = iota
ExitError
)
// Sub command name.
const (
CommandDeploy = "deploy"
CommandPublish = "publish"
)
// Safety Hour.
const (
SafetyHourStart = 9
SafetyHourEnd = 19
)
// Run invokes deploy and publish's process.
func (cli *CLI) Run(args []string) int {
var version bool
var dryRun bool
var force bool
var tag string
flags := flag.NewFlagSet("gdp", flag.ContinueOnError)
flags.SetOutput(cli.errStream)
flags.Usage = func() {
fmt.Fprintln(cli.errStream, Usage)
}
flags.BoolVar(&version, "version", false, "")
flags.BoolVar(&version, "v", false, "")
flags.BoolVar(&dryRun, "dry-run", false, "")
flags.BoolVar(&dryRun, "d", false, "")
flags.BoolVar(&force, "force", false, "")
flags.BoolVar(&force, "f", false, "")
flags.StringVar(&tag, "tag", "", "")
flags.StringVar(&tag, "t", "", "")
if len(args) < 2 {
printError(cli.errStream, "Too few argument.")
printError(cli.errStream, Usage)
return ExitError
}
parseIndex := 1
if args[1] == CommandDeploy || args[1] == CommandPublish {
parseIndex++
}
if err := flags.Parse(args[parseIndex:]); err != nil {
return ExitError
}
if version {
fmt.Fprintf(cli.outStream, "gdp version %s\n", Version)
return ExitSuccess
}
parsedArgs := flags.Args()
len := len(parsedArgs)
if len > 1 {
printError(cli.errStream, "Too many argument.")
printError(cli.errStream, Usage)
return ExitError
}
subCommand := args[1]
if subCommand != CommandDeploy && subCommand != CommandPublish {
printError(cli.errStream, "Invalid sub command.")
printError(cli.errStream, Usage)
return ExitError
}
if tag == "" {
latestTag := cli.gdp.GetLatestTag()
if subCommand == CommandDeploy {
next, err := GetNextVersion(latestTag)
if err != nil {
printError(cli.errStream, fmt.Sprintf("Getting release tag error: %s.", err.Error()))
return ExitError
}
latestTag = next
}
tag = latestTag
}
// validation
if !force && !validate(cli, subCommand, tag) {
return ExitError
}
toTag := "HEAD"
if subCommand == CommandPublish {
toTag = tag
}
// show release note
list, err := cli.gdp.GetMergeCommitList(toTag)
if err != nil {
printError(cli.errStream, fmt.Sprintf("Getting merge commit error: %s.", err.Error()))
return ExitError
}
note := GetReleaseNote(tag, list)
fmt.Fprintln(cli.outStream, "The release note is as follows.")
fmt.Fprintln(cli.outStream, "====================================")
fmt.Fprintln(cli.outStream, note)
fmt.Fprintln(cli.outStream, "====================================")
if dryRun {
printSuccess(cli.outStream, fmt.Sprintf("gdp %s done(dry-run mode).", subCommand))
return ExitSuccess
}
// execution
if subCommand == CommandDeploy {
if !IsSafetyHour() {
fmt.Fprintln(cli.outStream, "It's past the regular time. Is this a hot-fix release?")
fmt.Fprint(cli.outStream, "> ")
if !yesOrNo(cli) {
return ExitError
}
}
if err := cli.gdp.Deploy(tag); err != nil {
printError(cli.errStream, fmt.Sprintf("Deploy execution error: %s.", err.Error()))
return ExitError
}
} else {
if err := cli.gdp.Publish(tag, note); err != nil {
printError(cli.errStream, fmt.Sprintf("Publish execution error: %s.", err.Error()))
return ExitError
}
}
printSuccess(cli.outStream, fmt.Sprintf("gdp %s done.", subCommand))
message := "Do not be satisfied with 'released', let's face user's feedback in sincerity!"
printSuccess(cli.outStream, message)
return ExitSuccess
}
func printSuccess(w io.Writer, message string, args ...interface{}) {
message = fmt.Sprintf("[green]%s[reset]", message)
fmt.Fprintln(w, colorstring.Color(fmt.Sprintf(message, args...)))
}
func printError(w io.Writer, message string, args ...interface{}) {
message = fmt.Sprintf("[red]%s[reset]", message)
fmt.Fprintln(w, colorstring.Color(fmt.Sprintf(message, args...)))
}
func validate(cli *CLI, subCommand string, tag string) bool {
if subCommand == CommandDeploy {
if !cli.gdp.IsMasterOrMainBranch() {
printError(cli.errStream, "Branch is not master or main.")
return false
}
if cli.gdp.IsExistTagInLocal(tag) {
printError(cli.errStream, "Tag is already exist in local.")
return false
}
} else {
if !cli.gdp.IsExistTagInRemote(tag) {
printError(cli.errStream, "Tag is not exist in remote.")
return false
}
}
return true
}
var now = time.Now
func IsSafetyHour() bool {
return now().Hour() >= SafetyHourStart && now().Hour() < SafetyHourEnd
}
func yesOrNo(cli *CLI) bool {
reader := bufio.NewReader(os.Stdin)
s, err := reader.ReadByte()
if err != nil {
return false
}
if s == []byte("Y")[0] || s == []byte("y")[0] {
fmt.Fprintln(cli.outStream, "OK. Take time.")
return true
} else if s == []byte("N")[0] || s == []byte("n")[0] {
printError(cli.errStream, "Good choice.")
return false
}
printError(cli.errStream, "Please enter y or n.")
return false
}