-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
134 lines (115 loc) · 2.82 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
131
132
133
134
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"sort"
"strconv"
"strings"
)
func parseTag(in string) (int, int, int, error) {
parts := strings.Split(strings.TrimPrefix(in, "v"), ".")
if len(parts) != 3 {
return 0, 0, 0, fmt.Errorf("latest tag does not follow semantic versioning")
}
major, _ := strconv.Atoi(parts[0])
minor, _ := strconv.Atoi(parts[1])
patch, _ := strconv.Atoi(parts[2])
return major, minor, patch, nil
}
func main() {
// Pull the latest tags from Git
cmd := exec.Command("git", "fetch", "--tags")
err := cmd.Run()
if err != nil {
fmt.Println("Error fetching tags:", err)
return
}
// Get the latest tag
cmd = exec.Command("git", "tag")
output, err := cmd.Output()
if err != nil {
fmt.Println("Error getting tags:", err)
return
}
tags := strings.Split(string(output), "\n")
sort.Strings(tags)
if len(tags) < 2 {
fmt.Println("Warn: No tags found: using 0.0.0")
tags = append(tags, "v0.0.0")
}
latestTag := tags[len(tags)-1]
fmt.Println("Latest tag:", latestTag)
reader := bufio.NewReader(os.Stdin)
// Parse the latest tag and increment version
var major, minor, patch int
for {
if major, minor, patch, err = parseTag(latestTag); err != nil {
fmt.Println("Error parsing latest tag:", err)
fmt.Println("Manually enter last tag? ([tag]/n)")
newTag, _ := reader.ReadString('\n')
newTag = strings.TrimSpace(newTag)
if newTag == "n" {
fmt.Println("Tag creation aborted.")
return
}
latestTag = newTag
} else {
break
}
}
// Ask user for version bump type
fmt.Println("Bump ma(j)or, mi(n)or, or (p)atch? Enter q to (q)uit.")
bumpType, _ := reader.ReadString('\n')
bumpType = strings.TrimSpace(bumpType)
if bumpType == "q" {
fmt.Println("Tag creation aborted.")
return
}
switch bumpType {
case "j":
major++
minor = 0
patch = 0
case "n":
minor++
patch = 0
case "p":
patch++
default:
fmt.Println("Invalid option")
return
}
newTag := fmt.Sprintf("v%d.%d.%d", major, minor, patch)
fmt.Println("New tag:", newTag)
fmt.Println("Create the tag? (y/n)")
create, _ := reader.ReadString('\n')
create = strings.TrimSpace(create)
if create != "y" {
fmt.Println("Tag creation aborted.")
return
}
// Create tag with current commit
cmd = exec.Command("git", "tag", newTag)
err = cmd.Run()
if err != nil {
fmt.Println("Error creating new tag:", err)
return
}
// Ask user if they want to push the commit with the tag
fmt.Println("Push the commit with the tag? (y/n)")
push, _ := reader.ReadString('\n')
push = strings.TrimSpace(push)
if push == "y" {
cmd = exec.Command("git", "push", "--atomic", "origin", "HEAD", newTag)
err = cmd.Run()
if err != nil {
fmt.Println("Error pushing new tag:", err)
return
}
fmt.Println("Tag pushed successfully.")
} else {
fmt.Println("Tag creation completed without pushing.")
}
}