-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
130 lines (108 loc) · 4.33 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
package main
import (
"fmt"
"log"
"os"
"github.com/alecthomas/kingpin"
"github.com/kyoh86/git-vertag/internal"
)
// nolint
var (
version = "snapshot"
commit = "snapshot"
date = "snapshot"
)
func main() {
// Set command name and description
app := kingpin.New(
"git-vertag",
"A tool to manage version-tag with the semantic versioning specification.",
).Author("kyoh86").Version(version)
var cwd string
var dryRun bool
var fetch bool
var prefix string
var ancestors bool
app.Flag("current-directory", "Run as if git was started in <path> instead of the current working directory.").Short('C').PlaceHolder("<path>").ExistingDirVar(&cwd)
app.Flag("dry-run", "Without creating nor deleting tag, show git command.").Envar("GIT_VERTAG_DRYRUN").BoolVar(&dryRun)
app.Flag("fetch", "Fetch tags first").Envar("GIT_VERTAG_FETCH").Default("true").BoolVar(&fetch)
app.Flag("prefix", "Prefix for tag").Envar("GIT_VERTAG_PREFIX").Default("v").StringVar(&prefix)
app.Flag("ancestors", "With ancestor versions (vN and vN.N)").Envar("GIT_VERTAG_ANCESTORS").BoolVar(&ancestors)
getCmd := app.Command("get", "Gets the current version tag.").Default()
deleteCmd := app.Command("delete", "Deletes the current version tag.")
majorCmd := app.Command("major", "Creates a tag for the next major version and prints it.")
minorCmd := app.Command("minor", "Creates a tag for the next minor version and prints it.")
patchCmd := app.Command("patch", "Creates a tag for the next patch version and prints it.")
releaseCmd := app.Command("release", "Creates a tag to remove pre-release meta information.")
preCmd := app.Command("pre", "Creates a tag for the next pre-release version and prints it.")
buildCmd := app.Command("build", "Creates a tag for the next build version and prints it.")
var message []string
var file string
var pushTo string
for _, c := range []*kingpin.CmdClause{majorCmd, minorCmd, patchCmd, preCmd, buildCmd, releaseCmd} {
c.Flag("message", "Use the given tag message (instead of prompting). If multiple -m options are given, their values are concatenated as separate paragraphs.").Short('m').StringsVar(&message)
c.Flag("file", "Take the tag message from the given file. Use - to read the message from the standard input").Short('F').StringVar(&file)
c.Flag("push-to", "The remote repository that is destination of a push operation. This parameter can be either a URL or the name of a remote.").PlaceHolder("REPOSITORY").StringVar(&pushTo)
}
var pre internal.PreReleaseFlag
for _, c := range []*kingpin.CmdClause{majorCmd, minorCmd, patchCmd} {
c.Flag("pre", "Update pre-release notation. It accepts only alphanumeric or numeric identities.").SetValue(&pre)
}
preCmd.Arg("pre", "Pre-release notation. It accepts only alphanumeric or numeric identities.").SetValue(&pre)
var build internal.BuildFlag
for _, c := range []*kingpin.CmdClause{majorCmd, minorCmd, patchCmd, preCmd, releaseCmd} {
c.Flag("build", "Update build notation. It accepts only alphanumeric or numeric identities.").SetValue(&build)
}
buildCmd.Arg("build", "Update build notation. It accepts only alphanumeric or numeric identities.").Required().SetValue(&build)
cmd, err := app.Parse(os.Args[1:])
if err != nil {
app.FatalUsage("%s", err)
}
runner := internal.NewGitRunner()
if dryRun {
runner = internal.NewDryRunner()
}
tag := internal.Tagger{
Runner: runner,
Workdir: cwd,
PushTo: pushTo,
}
mgr := internal.Manager{
Prefix: prefix,
Tagger: tag,
Fetch: fetch,
Ancestors: ancestors,
}
switch cmd {
case getCmd.FullCommand():
v, err := mgr.GetVer()
if err != nil {
return
}
fmt.Println(v)
case deleteCmd.FullCommand():
v, err := mgr.DeleteVer()
if err != nil {
return
}
fmt.Println(v)
case majorCmd.FullCommand():
printResult(mgr.UpdateMajor(pre, build, message, file))
case minorCmd.FullCommand():
printResult(mgr.UpdateMinor(pre, build, message, file))
case patchCmd.FullCommand():
printResult(mgr.UpdatePatch(pre, build, message, file))
case preCmd.FullCommand():
printResult(mgr.UpdatePre(pre, build, message, file))
case releaseCmd.FullCommand():
printResult(mgr.Release(build, message, file))
case buildCmd.FullCommand():
printResult(mgr.Build(build, message, file))
}
}
func printResult(cur, next string, err error) {
if err != nil {
log.Fatal(err)
}
fmt.Printf("update %s to %s\n", cur, next)
}