-
Notifications
You must be signed in to change notification settings - Fork 57
/
tag_version.go
92 lines (74 loc) · 1.98 KB
/
tag_version.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
//go:build ignore
// +build ignore
// This is a tool to assist with tagging versions correctly.
// It updates version/version.go and produces the commands
// to run for git.
//
// To run: go run tag_version.go
package main
import (
"flag"
"fmt"
"log"
"os/exec"
"strconv"
"strings"
"github.com/psanford/wormhole-william/version"
)
var updateMajor = flag.Bool("major", false, "update major component")
var updateMinor = flag.Bool("minor", false, "update minor component")
var updatePatch = flag.Bool("patch", true, "update patch component")
func main() {
flag.Parse()
err := exec.Command("git", "diff", "--quiet").Run()
if err != nil {
log.Fatalf("Cannot run tag_version with pending changes to your working directory: %s", err)
}
v := version.AgentVersion
parts := strings.Split(v, ".")
if len(parts) != 3 {
log.Fatalf("Unexpected version format %s", v)
}
majorStr := parts[0]
if majorStr[0] != 'v' {
log.Fatalf("Unexpected version format (major) %s", v)
}
major, err := strconv.Atoi(majorStr[1:])
if err != nil {
panic(err)
}
minorStr := parts[1]
minor, err := strconv.Atoi(minorStr)
if err != nil {
panic(err)
}
patchStr := parts[2]
patch, err := strconv.Atoi(patchStr)
if err != nil {
panic(err)
}
if *updateMajor {
major++
minor = 0
patch = 0
} else if *updateMinor {
minor++
patch = 0
} else if *updatePatch {
patch++
} else {
log.Fatal("No update flag specified")
}
newVersion := fmt.Sprintf("v%d.%d.%d", major, minor, patch)
fmt.Printf("newVersion: %s\n", newVersion)
out, err := exec.Command("gofmt", "-w", "-r", fmt.Sprintf("\"%s\" -> \"%s\"", v, newVersion), "version/version.go").CombinedOutput()
if err != nil {
log.Fatalf("Failed to gofmt: %s, %s", err, out)
}
fmt.Println("Run:\n")
fmt.Println("go test ./... &&\\")
fmt.Printf("git add version/version.go && git commit -m \"Bump version %s => %s\" &&\\\n", v, newVersion)
fmt.Printf("git tag %s\n", newVersion)
fmt.Println("\nThen:\n")
fmt.Println("git push --tags")
}