forked from Shyp/bump_version
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
169 lines (157 loc) · 3.71 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
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
package main
import (
"errors"
"flag"
"fmt"
"os"
"strconv"
"strings"
)
type VersionType string
const Major = VersionType("major")
const Minor = VersionType("minor")
const Patch = VersionType("patch")
const VERSION_FILE_LOCATION = "VERSION"
func main() {
flag.Parse()
args := flag.Args()
switch len(args) {
case 1:
bumpType := args[0]
handleResult(func() (*Version, error) {
return changeVersionInFile(VersionType(bumpType))
})
case 2:
bumpType := args[0]
version := args[1]
handleResult(func() (*Version, error) {
return changeVersion(VersionType(bumpType), version)
})
default:
os.Stdout.Write([]byte("Usage: go run <VERSION> <major|minor|patch>"))
}
}
func handleResult(fn func() (*Version, error)) {
v, err := fn()
if err != nil {
os.Stderr.Write([]byte(err.Error()))
return
}
os.Stdout.Write([]byte(v.String()))
}
func changeVersionInFile(vType VersionType) (*Version, error) {
fileContent, err := os.ReadFile(VERSION_FILE_LOCATION)
if err != nil {
return nil, err
}
return changeVersion(vType, strings.TrimSpace(string(fileContent)))
}
// changeVersion takes a basic literal representing a string version, and
// increments the version number per the given VersionType.
func changeVersion(vtype VersionType, value string) (*Version, error) {
versionNoQuotes := strings.Replace(value, "\"", "", -1)
version, err := Parse(versionNoQuotes)
if err != nil {
return nil, err
}
if vtype == Major {
version.Major++
if version.Minor != -1 {
version.Minor = 0
}
if version.Patch != -1 {
version.Patch = 0
}
} else if vtype == Minor {
if version.Minor == -1 {
version.Minor = 0
}
if version.Patch != -1 {
version.Patch = 0
}
version.Minor++
} else if vtype == Patch {
if version.Patch == -1 {
version.Patch = 0
}
version.Patch++
} else {
return nil, fmt.Errorf("Invalid version type: %s", vtype)
}
return version, nil
}
type Version struct {
Major int64
Minor int64
Patch int64
}
func (v *Version) String() string {
if v.Major >= 0 && v.Minor >= 0 && v.Patch >= 0 {
return fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
} else if v.Major >= 0 && v.Minor >= 0 {
return fmt.Sprintf("%d.%d", v.Major, v.Minor)
} else if v.Major >= 0 {
return fmt.Sprintf("%d", v.Major)
} else {
return "%!s(INVALID_VERSION)"
}
}
// ParseVersion parses a version string of the forms "2", "2.3", or "0.10.11".
// Any information after the third number ("2.0.0-beta") is discarded. Very
// little effort is taken to validate the input.
//
// If a field is omitted from the string version (e.g. "0.2"), it's stored in
// the Version string as the integer -1.
func Parse(version string) (*Version, error) {
if len(version) == 0 {
return nil, errors.New("Empty version string")
}
parts := strings.SplitN(version, ".", 3)
if len(parts) == 1 {
major, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return nil, err
}
return &Version{
Major: major,
Minor: -1,
Patch: -1,
}, nil
}
if len(parts) == 2 {
major, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return nil, err
}
minor, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return nil, err
}
return &Version{
Major: major,
Minor: minor,
Patch: -1,
}, nil
}
if len(parts) == 3 {
major, err := strconv.ParseInt(parts[0], 10, 64)
if err != nil {
return nil, err
}
minor, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return nil, err
}
patchParts := strings.SplitN(parts[2], "-", 2)
patch, err := strconv.ParseInt(patchParts[0], 10, 64)
if err != nil {
return nil, err
}
return &Version{
Major: major,
Minor: minor,
Patch: patch,
}, nil
}
return nil, fmt.Errorf("Invalid version string: %s", version)
}