Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
197 lines (182 loc) · 3.25 KB
/
Copy pathmain.go
File metadata and controls
197 lines (182 loc) · 3.25 KB
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
package main
import (
"flag"
"fmt"
"log/slog"
"os/exec"
"strconv"
"strings"
)
type VersionType string
const (
Major VersionType = "major"
Minor VersionType = "minor"
Patch VersionType = "patch"
)
func (v *VersionType) String() string {
return string(*v)
}
func (v *VersionType) Set(s string) error {
switch s {
case string(Major), string(Minor), string(Patch):
*v = VersionType(s)
return nil
default:
return fmt.Errorf("invalid version type: %s. Available: major, minor or patch", s)
}
}
type TagVersion struct {
Major int
Minor int
Patch int
}
func (t *TagVersion) String() string {
return fmt.Sprintf(
"v%d.%d.%d",
t.Major,
t.Minor,
t.Patch,
)
}
func (t *TagVersion) Bump(bumpType VersionType) {
switch bumpType {
case Major:
t.Major++
t.Minor = 0
t.Patch = 0
case Minor:
t.Minor++
t.Patch = 0
case Patch:
t.Patch++
}
}
func mustAtoi(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
panic(err)
}
return i
}
func NewTag(s string) *TagVersion {
s = strings.TrimSpace(s)
_p := strings.Split(s, "v")
versions := strings.Split(_p[1], ".")
return &TagVersion{
Major: mustAtoi(versions[0]),
Minor: mustAtoi(versions[1]),
Patch: mustAtoi(versions[2]),
}
}
func gitTag(tag TagVersion) {
cmd := exec.Command(
"git", "tag", tag.String(),
)
_, err := cmd.CombinedOutput()
if err != nil {
slog.Error(
"failed to git tag",
"err", err,
)
panic(err)
}
}
func gitTagsPush() {
cmd := exec.Command(
"git", "push", "origin", "--tags",
)
_, err := cmd.CombinedOutput()
if err != nil {
slog.Error(
"failed to git push tags",
"err", err,
)
panic(err)
}
}
func getTags() []TagVersion {
var tags []TagVersion
cmd := exec.Command(
"git", "tag",
)
out, err := cmd.CombinedOutput()
if err != nil {
slog.Error(
"getting tags failed",
"err", err,
)
panic(err)
}
tagsRaw := string(out)
for _, line := range strings.Split(tagsRaw, "\n") {
if strings.TrimSpace(line) == "" {
continue
}
tags = append(
tags,
*NewTag(line),
)
}
return tags
}
func getLatestTag(tags []TagVersion) TagVersion {
var latest TagVersion
highestMajor := -1
for _, t := range tags {
if t.Major > highestMajor {
highestMajor = t.Major
latest = t
}
}
highestMinor := -1
for _, t := range tags {
if highestMajor > t.Major {
continue
}
if t.Minor > highestMinor {
highestMinor = t.Minor
latest = t
}
}
highestPatch := -1
for _, t := range tags {
if highestMajor > t.Major || highestMinor > t.Minor {
continue
}
if t.Patch > highestPatch {
highestPatch = t.Patch
latest = t
}
}
return latest
}
func main() {
var version VersionType = Patch
flag.Var(&version, "b", "What to bump: major, minor or patch (default: patch)")
isDryRun := flag.Bool("dry", false, "If true, only logs will be shown, an actual git tag and push won't be executed")
flag.Parse()
if *isDryRun {
slog.Info("dry run found, won't git tag nor git push")
}
tags := getTags()
latest := getLatestTag(tags)
slog.Debug(
"git information",
"found tags", len(tags),
"latest tag", latest.String(),
)
latest.Bump(version)
slog.Debug(
"git tagging",
"version", latest.String(),
)
if !*isDryRun {
gitTag(latest)
}
slog.Debug(
"git pushing",
)
if !*isDryRun {
gitTagsPush()
}
}