-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
254 lines (228 loc) · 5.54 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
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
249
250
251
252
253
254
package main
import (
"errors"
"fmt"
"log"
"os"
"reflect"
tv "tv/lib"
"github.com/AlecAivazis/survey/v2"
"github.com/iancoleman/orderedmap"
"github.com/urfave/cli"
)
var version string
var versionInfos *orderedmap.OrderedMap
func init() {
versionInfos, _ = tv.ReadVersionFile(tv.SemverFilePath)
}
// parse the cli arguments and return a list of apps that should be updated.
func getTargetApps(c *cli.Context) []string {
if !c.Bool("all") {
targetApp := c.String("target")
if targetApp == "" {
// user does not input target app. try to be clever:
// 1. if there is only one app in semver.json, then we simply choose it for the user.
// 2. if multiple apps, then launch a select panel for the user to choose the app he/she wants to tag.
if len(versionInfos.Keys()) == 1 {
targetApp = versionInfos.Keys()[0]
} else {
question := []*survey.Question{
{
Name: "app",
Prompt: &survey.Select{
Message: "choose an app you want to tag:",
Options: versionInfos.Keys(),
},
},
}
answer := struct {
App string
}{}
_ = survey.Ask(question, &answer)
targetApp = answer.App
}
}
return []string{targetApp}
} else {
return versionInfos.Keys()
}
}
func doAction(c *cli.Context, action string) error {
if c.Bool("dry-run") {
fmt.Println("start dry-run...")
}
appsToUpdate := getTargetApps(c)
highestVersion := "0.0.0"
// find the highest version of these apps.
for _, app := range appsToUpdate {
if version, ok := versionInfos.Get(app); ok {
result, _ := tv.Compare(version.(string), highestVersion)
if result == 1 {
highestVersion = version.(string)
}
} else {
return fmt.Errorf("cannot find target app: %s", app)
}
}
v, err := tv.Make(highestVersion)
if err != nil {
return err
}
in := []reflect.Value{reflect.ValueOf(c.Args())}
result := reflect.ValueOf(v).MethodByName(action).Call(in)
if result[0].Interface() != nil {
return result[0].Interface().(error)
}
err = updateTags(c, v, appsToUpdate)
if err != nil {
return err
}
return nil
}
func updateTags(c *cli.Context, v *tv.Version, appsToUpdate []string) error {
preid := c.String("preid")
if preid != "" {
v.UpdatePreid(preid)
}
nextVer := v.String()
for _, app := range appsToUpdate {
versionInfos.Set(app, nextVer)
}
if !c.Bool("dry-run") {
err := tv.WriteVersionFile(versionInfos, tv.SemverFilePath)
if err != nil {
return err
}
}
var tags []string
if c.Bool("pure") {
tags = append(tags, nextVer)
} else {
for _, app := range appsToUpdate {
tags = append(tags, v.GetTagStr(app))
}
}
if !c.Bool("dry-run") {
if len(tags) == 1 {
_ = tv.TagVersion(tags[0])
} else {
_ = tv.TagVersions(nextVer, tags)
}
}
fmt.Printf("git tag generated: %s\n", tags)
return nil
}
func main() {
app := cli.NewApp()
app.Name = "tv"
app.Usage = "tag version for ur f** awesome project"
if version != "" {
app.Version = version
}
commandFlags := []cli.Flag{
cli.StringFlag{Name: "target, t", Usage: "set target app"},
cli.StringFlag{Name: "preid", Usage: "prerelease identifier, like the rc in 1.2.0-rc.8."},
cli.BoolFlag{Name: "pure, p", Usage: "create tag without app name"},
cli.BoolFlag{Name: "all, a", Usage: "upgrade version of all apps"},
cli.BoolFlag{Name: "dry-run", Usage: "do a fake action, won't create real tag"},
}
app.Commands = []cli.Command{
{
Name: "init",
Usage: "init tv for ur project",
Action: func(c *cli.Context) error {
args := c.Args()
if len(args) == 0 {
return errors.New("init need at least one module name")
}
var o *orderedmap.OrderedMap
if tv.IsFileExist(tv.SemverFilePath) {
o = versionInfos
} else {
o = orderedmap.New()
}
for _, moduleName := range args {
o.Set(moduleName, "0.0.0")
}
tv.WriteVersionFile(o, tv.SemverFilePath)
return nil
},
},
{
Name: "patch",
Usage: "patch version, v0.0.1 -> v0.0.2",
Flags: commandFlags,
Action: func(c *cli.Context) error {
return doAction(c, "Patch")
},
},
{
Name: "major",
Usage: "major version, v0.0.1 -> v1.0.0",
Flags: commandFlags,
Action: func(c *cli.Context) error {
return doAction(c, "Major")
},
},
{
Name: "minor",
Usage: "minor version, v0.0.1 -> v0.1.0",
Flags: commandFlags,
Action: func(c *cli.Context) error {
return doAction(c, "Minor")
},
},
{
Name: "prerelease",
Usage: "prerelease version, v0.0.1-alpha.1 -> v0.0.1-alpha.2",
Flags: commandFlags,
Action: func(c *cli.Context) error {
return doAction(c, "Prerelease")
},
},
{
Name: "release",
Usage: "release version, v0.0.1-alpha.1 -> v0.0.1",
Flags: commandFlags,
Action: func(c *cli.Context) error {
return doAction(c, "Release")
},
},
{
Name: "prepatch",
Usage: "prepatch version, v0.0.1 -> v0.0.2-alpha.0",
Flags: commandFlags,
Action: func(c *cli.Context) error {
return doAction(c, "Prepatch")
},
},
{
Name: "preminor",
Usage: "preminor version, v0.0.1 -> v0.1.0-alpha.0",
Flags: commandFlags,
Action: func(c *cli.Context) error {
return doAction(c, "Preminor")
},
},
{
Name: "premajor",
Usage: "preminor version, v0.0.1 -> v1.0.0-alpha.0",
Flags: commandFlags,
Action: func(c *cli.Context) error {
return doAction(c, "Premajor")
},
},
{
Name: "version",
Usage: "set specific version",
Flags: commandFlags,
Action: func(c *cli.Context) error {
return doAction(c, "SpecificVersion")
},
},
}
err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}