-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
73 lines (65 loc) · 1.68 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
package main
import (
"flag"
"fmt"
"os"
"os/exec"
"github.com/Shyp/bump_version/lib"
)
const VERSION = "1.3"
func usage() {
fmt.Fprintf(os.Stderr, "Usage: bump_version [--version=<version>] [<major|minor|patch>] <filename>\n")
flag.PrintDefaults()
}
// runCommand execs the given command and exits if it fails.
func runCommand(binary string, args ...string) {
out, err := exec.Command(binary, args...).CombinedOutput()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when running command: %s.\nOutput was:\n%s", err.Error(), string(out))
os.Exit(2)
}
}
var vsn = flag.String("version", "", "Set this version in the file (don't increment whatever version is present)")
func main() {
flag.Usage = usage
flag.Parse()
args := flag.Args()
var filename string
var version *bump_version.Version
if *vsn != "" {
// no "minor"
if len(args) != 1 {
flag.Usage()
return
}
var err error
version, err = bump_version.Parse(*vsn)
if err != nil {
os.Stderr.WriteString(err.Error())
os.Exit(2)
}
filename = args[0]
setErr := bump_version.SetInFile(version, filename)
if setErr != nil {
os.Stderr.WriteString(setErr.Error() + "\n")
os.Exit(2)
}
} else {
if len(args) != 2 {
flag.Usage()
return
}
versionTypeStr := args[0]
filename = args[1]
var err error
version, err = bump_version.BumpInFile(bump_version.VersionType(versionTypeStr), filename)
if err != nil {
os.Stderr.WriteString(err.Error() + "\n")
os.Exit(2)
}
}
runCommand("git", "add", filename)
runCommand("git", "commit", "-m", version.String())
runCommand("git", "tag", version.String(), "--annotate", "--message", version.String())
os.Stdout.WriteString(version.String() + "\n")
}