Skip to content

Commit 45a1d55

Browse files
committed
Fix builder internal naming
Improve the readability of the builder-bumper by using semver naming for Go version components. Signed-off-by: SuperQ <[email protected]>
1 parent f77765f commit 45a1d55

File tree

1 file changed

+46
-46
lines changed

1 file changed

+46
-46
lines changed

cmd/builder-bumper/main.go

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ func init() {
4242
}
4343

4444
type goVersion struct {
45-
major int
4645
minor int
46+
patch int
4747
}
4848

4949
func newGoVersion(v string) *goVersion {
@@ -52,32 +52,32 @@ func newGoVersion(v string) *goVersion {
5252
log.Fatalf("bad version: %s", v)
5353
}
5454
m := strings.Split(c, ".")
55-
major, err := strconv.Atoi(string(m[1]))
55+
minor, err := strconv.Atoi(string(m[1]))
5656
if err != nil {
5757
log.Fatal(err)
5858
}
59-
minor, err := strconv.Atoi(string(m[2]))
59+
patch, err := strconv.Atoi(string(m[2]))
6060
if err != nil {
6161
log.Fatal(err)
6262
}
6363
return &goVersion{
64-
major: major,
6564
minor: minor,
65+
patch: patch,
6666
}
6767
}
6868

69-
// major returns the version string without the minor version.
70-
func (g *goVersion) Major() string {
71-
return fmt.Sprintf("1.%d", g.major)
69+
// Minor returns the version string without the patch version.
70+
func (g *goVersion) Minor() string {
71+
return fmt.Sprintf("1.%d", g.minor)
7272
}
7373

7474
// golangVersion returns the full version string but without the leading '.0'
75-
// for the initial revision of a major release.
75+
// for the initial revision of a minor release.
7676
func (g *goVersion) golangVersion() string {
77-
if g.major < 21 && g.minor == 0 {
78-
return g.Major()
77+
if g.minor < 21 && g.patch == 0 {
78+
return g.Minor()
7979
}
80-
return fmt.Sprintf("1.%d.%d", g.major, g.minor)
80+
return fmt.Sprintf("1.%d.%d", g.minor, g.patch)
8181
}
8282

8383
// String returns the full version string.
@@ -86,14 +86,14 @@ func (g *goVersion) String() string {
8686
}
8787

8888
func (g *goVersion) less(o *goVersion) bool {
89-
if g.major == o.major {
90-
return g.minor < o.minor
89+
if g.minor == o.minor {
90+
return g.patch < o.patch
9191
}
92-
return g.major < o.major
92+
return g.minor < o.minor
9393
}
9494

9595
func (g *goVersion) equal(o *goVersion) bool {
96-
return g.major == o.major && g.minor == o.minor
96+
return g.minor == o.minor && g.patch == o.patch
9797
}
9898

9999
// url returns the URL of the Go archive.
@@ -115,12 +115,12 @@ func (g *goVersion) getSHA256() (string, error) {
115115
return strings.TrimSpace(string(b)), nil
116116
}
117117

118-
// getLastMinorVersion returns the last minor version for a given Go version.
119-
func (g *goVersion) getLastMinorVersion() (*goVersion, error) {
118+
// getLastPatchVersion returns the last patch version for a given Go version.
119+
func (g *goVersion) getLastPatchVersion() (*goVersion, error) {
120120
last := *g
121121
for {
122122
next := last
123-
next.minor++
123+
next.patch++
124124
resp, err := http.Head(next.url())
125125
if err != nil {
126126
return nil, err
@@ -134,11 +134,11 @@ func (g *goVersion) getLastMinorVersion() (*goVersion, error) {
134134
}
135135
}
136136

137-
// getNextMajor returns the next Go major version for a given Go version.
137+
// getNextMinor returns the next Go minor version for a given Go version.
138138
// It returns nil if the current version is already the latest.
139-
func (g *goVersion) getNextMajor() *goVersion {
140-
version := newGoVersion(g.Major() + ".0")
141-
version.major++
139+
func (g *goVersion) getNextMinor() *goVersion {
140+
version := newGoVersion(g.Minor() + ".0")
141+
version.minor++
142142

143143
resp, err := http.Head(version.url())
144144
if err != nil {
@@ -202,9 +202,9 @@ func shaReplacer(old, new *goVersion) func(string) (string, error) {
202202
}
203203
}
204204

205-
func majorVersionReplacer(old, new *goVersion) func(string) (string, error) {
205+
func minorVersionReplacer(old, new *goVersion) func(string) (string, error) {
206206
return func(out string) (string, error) {
207-
return strings.ReplaceAll(out, old.Major(), new.Major()), nil
207+
return strings.ReplaceAll(out, old.Minor(), new.Minor()), nil
208208
}
209209
}
210210

@@ -220,10 +220,10 @@ func fullVersionReplacer(old, new *goVersion) func(string) (string, error) {
220220
}
221221
}
222222

223-
// replaceMajor switches the versions from [1.(N-1), 1.N] to [1.N, 1.(N+1)].
224-
func replaceMajor(old, current, next *goVersion) error {
223+
// replaceMinor switches the versions from [1.(N-1), 1.N] to [1.N, 1.(N+1)].
224+
func replaceMinor(old, current, next *goVersion) error {
225225
// Replace the old version by the next one.
226-
err := filepath.Walk(old.Major(), func(path string, info os.FileInfo, err error) error {
226+
err := filepath.Walk(old.Minor(), func(path string, info os.FileInfo, err error) error {
227227
if err != nil {
228228
return err
229229
}
@@ -248,22 +248,22 @@ func replaceMajor(old, current, next *goVersion) error {
248248
return replace(path,
249249
[]func(string) (string, error){
250250
golangVersionReplacer("", old, next),
251-
majorVersionReplacer(old, next),
251+
minorVersionReplacer(old, next),
252252
},
253253
)
254254
})
255255
if err != nil {
256256
return err
257257
}
258-
if err := os.Rename(old.Major(), next.Major()); err != nil {
258+
if err := os.Rename(old.Minor(), next.Minor()); err != nil {
259259
return fmt.Errorf("failed to create new version directory: %w", err)
260260
}
261261

262262
// Update CircleCI.
263263
err = replace(".circleci/config.yml",
264264
[]func(string) (string, error){
265-
majorVersionReplacer(current, next),
266-
majorVersionReplacer(old, current),
265+
minorVersionReplacer(current, next),
266+
minorVersionReplacer(old, current),
267267
},
268268
)
269269
if err != nil {
@@ -273,8 +273,8 @@ func replaceMajor(old, current, next *goVersion) error {
273273
// Update Makefile.
274274
err = replace("Makefile",
275275
[]func(string) (string, error){
276-
majorVersionReplacer(current, next),
277-
majorVersionReplacer(old, current),
276+
minorVersionReplacer(current, next),
277+
minorVersionReplacer(old, current),
278278
},
279279
)
280280
if err != nil {
@@ -285,22 +285,22 @@ func replaceMajor(old, current, next *goVersion) error {
285285
return replace("README.md",
286286
[]func(string) (string, error){
287287
fullVersionReplacer(current, next),
288-
majorVersionReplacer(current, next),
288+
minorVersionReplacer(current, next),
289289
fullVersionReplacer(old, current),
290-
majorVersionReplacer(old, current),
290+
minorVersionReplacer(old, current),
291291
},
292292
)
293293
}
294294

295-
// updateNextMinor bumps the given directory to the next minor version.
295+
// updateNextPatch bumps the given directory to the next patch version.
296296
// It returns nil if no new version exists.
297-
func updateNextMinor(dir string) (*goVersion, error) {
297+
func updateNextPatch(dir string) (*goVersion, error) {
298298
current, err := getExactVersionFromDir(dir)
299299
if err != nil {
300300
return nil, fmt.Errorf("failed to detect current version of %s: %w", dir, err)
301301
}
302302

303-
next, err := current.getLastMinorVersion()
303+
next, err := current.getLastPatchVersion()
304304
if err != nil {
305305
return nil, err
306306
}
@@ -309,7 +309,7 @@ func updateNextMinor(dir string) (*goVersion, error) {
309309
return nil, nil
310310
}
311311

312-
err = replace(filepath.Join(current.Major(), "base/Dockerfile"),
312+
err = replace(filepath.Join(current.Minor(), "base/Dockerfile"),
313313
[]func(string) (string, error){
314314
golangVersionReplacer("GOLANG_VERSION ", current, next),
315315
shaReplacer(current, next),
@@ -319,7 +319,7 @@ func updateNextMinor(dir string) (*goVersion, error) {
319319
return nil, err
320320
}
321321

322-
err = replace(filepath.Join(current.Major(), "Makefile.COMMON"),
322+
err = replace(filepath.Join(current.Minor(), "Makefile.COMMON"),
323323
[]func(string) (string, error){
324324
fullVersionReplacer(current, next),
325325
},
@@ -373,10 +373,10 @@ func run() error {
373373
return fmt.Errorf("Expected 2 versions of Go but got %d\n", len(dirs))
374374
}
375375

376-
// Check if a new major Go version exists.
376+
// Check if a new minor Go version exists.
377377
nexts := make([]*goVersion, 0)
378-
if next := newGoVersion(dirs[1] + ".0").getNextMajor(); next != nil {
379-
log.Printf("found a new major version of Go: %s", next)
378+
if next := newGoVersion(dirs[1] + ".0").getNextMinor(); next != nil {
379+
log.Printf("found a new minor version of Go: %s", next)
380380
old, err := getExactVersionFromDir(dirs[0])
381381
if err != nil {
382382
return err
@@ -385,15 +385,15 @@ func run() error {
385385
if err != nil {
386386
return err
387387
}
388-
if err = replaceMajor(old, current, next); err != nil {
388+
if err = replaceMinor(old, current, next); err != nil {
389389
return err
390390
}
391391
nexts = append(nexts, next)
392392
} else {
393-
// Otherwise check for new minor versions.
393+
// Otherwise check for new patch versions.
394394
for _, d := range dirs {
395395
log.Printf("processing %s", d)
396-
next, err := updateNextMinor(d)
396+
next, err := updateNextPatch(d)
397397
if err != nil {
398398
return err
399399
}

0 commit comments

Comments
 (0)