-
Notifications
You must be signed in to change notification settings - Fork 4
/
cli.go
195 lines (168 loc) Β· 4.24 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
package semv
import (
"fmt"
"io"
"reflect"
"strings"
flags "github.com/jessevdk/go-flags"
)
const (
// ExitOK for exit code
ExitOK int = 0
// ExitErr for exit code
ExitErr int = 1
)
// Env struct
type Env struct {
Out, Err io.Writer
Args []string
Version string
Commit string
Date string
}
type cli struct {
env Env
command string
Pre bool `long:"pre" short:"p" description:"Pre-Release version indicates(ex: 0.0.1-rc.0)"`
PreName string `long:"pre-name" description:"Specify pre-release version name"`
Build bool `long:"build" short:"b" description:"Build version indicates(ex: 0.0.1+3222d31.foo)"`
BuildName string `long:"build-name" description:"Specify build version name"`
All bool `long:"all" short:"a" description:"Include everything such as pre-release and build versions in list"`
Bump bool `long:"bump" short:"B" description:"Create tag and Push to origin"`
Prefix string `long:"prefix" short:"x" description:"Prefix for version and tag(default: v)"`
Help bool `long:"help" short:"h" description:"Show this help message and exit"`
Version bool `long:"version" short:"v" description:"Prints the version number"`
}
var gitTagCmder Cmder
var gitPushTagCmder Cmder
// RunCLI runs for CLI
func RunCLI(env Env) int {
return (&cli{env: env}).run()
}
func (c *cli) buildHelp(names []string) []string {
var help []string
t := reflect.TypeOf(cli{})
for _, name := range names {
f, ok := t.FieldByName(name)
if !ok {
continue
}
tag := f.Tag
if tag == "" {
continue
}
var o, a string
if a = tag.Get("arg"); a != "" {
a = fmt.Sprintf("=%s", a)
}
if s := tag.Get("short"); s != "" {
o = fmt.Sprintf("-%s, --%s%s", tag.Get("short"), tag.Get("long"), a)
} else {
o = fmt.Sprintf(" --%s%s", tag.Get("long"), a)
}
desc := tag.Get("description")
help = append(help, fmt.Sprintf(" %-18s %s", o, desc))
}
return help
}
func (c *cli) showHelp() {
opts := strings.Join(c.buildHelp([]string{
"Pre",
"PreRelease",
"Build",
"BuildName",
"All",
"Bump",
"Prefix",
"Help",
"Version",
}), "\n")
help := `
Usage: git-semv [--version] [--help] command <options>
Commands:
list Sorted versions
now, latest Latest version
major Next major version: vX.0.0
minor Next minor version: v0.X.0
patch Next patch version: v0.0.X
Options:
%s
`
fmt.Fprintf(c.env.Out, help, opts)
}
func (c *cli) run() int {
p := flags.NewParser(c, flags.PassDoubleDash)
args, err := p.ParseArgs(c.env.Args)
if err != nil {
fmt.Fprintf(c.env.Err, "Error: %s\n", err)
return ExitErr
}
if c.Help {
c.showHelp()
return ExitErr
}
if c.Version {
fmt.Fprintf(c.env.Err, "git-semv version %s [%v, %v]\n", c.env.Version, c.env.Commit, c.env.Date)
return ExitOK
}
if len(args) > 0 {
c.command = args[0]
} else {
c.command = "list"
}
switch c.command {
case "list":
list, err := GetList()
if err != nil {
fmt.Fprintf(c.env.Err, "Error: %s\n", err)
}
if !c.All {
list = list.WithoutPreRelease()
}
fmt.Fprintf(c.env.Out, "%s\n", list)
case "now", "latest":
latest, err := Latest()
if err != nil {
fmt.Fprintf(c.env.Err, "Error: %s\n", err)
}
fmt.Fprintf(c.env.Out, "%s\n", latest)
case "major", "minor", "patch":
latest, err := Latest()
if err != nil {
fmt.Fprintf(c.env.Err, "Error: %s\n", err)
}
next := latest.Next(c.command)
if c.Pre || c.PreName != "" {
_, _ = next.PreRelease(c.PreName)
}
if c.Build || c.BuildName != "" {
_, _ = next.Build(c.BuildName)
}
if c.Bump {
if gitTagCmder == nil {
gitTagCmder = Cmd{}
}
_, err = gitTagCmder.Do("git", "tag", next.String())
if err != nil {
fmt.Fprintf(c.env.Err, "Error: %s\n", err)
return ExitErr
}
if gitPushTagCmder == nil {
gitPushTagCmder = Cmd{}
}
_, err = gitPushTagCmder.Do("git", "push", "origin", next.String())
if err != nil {
fmt.Fprintf(c.env.Err, "Error: %s\n", err)
return ExitErr
}
fmt.Fprintf(c.env.Out, "Bumped version to %s\n", next)
} else {
fmt.Fprintf(c.env.Out, "%s\n", next)
}
default:
fmt.Fprintf(c.env.Err, "Error: command is not available: %s\n", c.command)
c.showHelp()
return ExitErr
}
return ExitOK
}