Skip to content

Commit

Permalink
Allow to bump custom versions
Browse files Browse the repository at this point in the history
Signed-off-by: Jose Luis Vazquez Gonzalez <[email protected]>
  • Loading branch information
Jose Luis Vazquez Gonzalez committed Dec 16, 2021
1 parent d61d3cb commit 699f3c0
Show file tree
Hide file tree
Showing 6 changed files with 178 additions and 17 deletions.
4 changes: 3 additions & 1 deletion bump-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

set -euo pipefail

desired_version=${1:-}

old_version=$(cat version)
(cd next-semver && go build .)
version=$(./next-semver/next-semver "${old_version}")
version=$(./next-semver/next-semver "${old_version}" "${desired_version}")
echo "${version}" > version
git add version
git commit -s -m "Bump version to ${version}"
Expand Down
5 changes: 0 additions & 5 deletions next-semver/go.mod

This file was deleted.

2 changes: 0 additions & 2 deletions next-semver/go.sum

This file was deleted.

59 changes: 51 additions & 8 deletions next-semver/main.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,62 @@
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause

package main

import (
"errors"
"fmt"
"os"

semverv3 "github.com/Masterminds/semver/v3"
semver3 "github.com/Masterminds/semver/v3"
)

func main() {
if len(os.Args) != 2 {
fmt.Printf("Usage: %s {semver-version}\n$ %s 1.2.3\n", os.Args[0], os.Args[0])
os.Exit(-1)
var (
// ErrBadInputs when the number of CLI inputs are wrong
ErrBadInputs = errors.New("bad inputs")

// ErrBadVersionBump when the desired version is not after the previous one
ErrBadVersionBump = errors.New("bad version bump")
)

func nextSemVer(previous, desired *semver3.Version) (*semver3.Version, error) {
if desired == nil {
next := previous.IncPatch()
return &next, nil
}
if !desired.GreaterThan(previous) {
return nil, fmt.Errorf("%w: %v is not after %v", ErrBadVersionBump, desired, previous)
}
return desired, nil
}

func mainE(args ...string) error {
if len(args) < 2 || len(args) > 3 {
return fmt.Errorf("%w\nusage: %s {semver-version} [{desired_semver}]\n$ %s 1.2.3",
ErrBadInputs, args[0], args[0])
}
previous, err := semver3.NewVersion(args[1])
if err != nil {
return fmt.Errorf("failed to parse previous version %q: %w", args[1], err)
}
var desired *semver3.Version
if len(args) > 2 && args[2] != "" {
desired, err = semver3.NewVersion(args[2])
if err != nil {
return fmt.Errorf("failed to parse desired version %q: %w", args[2], err)
}
}
next, err := nextSemVer(previous, desired)
if err != nil {
return err
}
v := os.Args[1]
version := semverv3.MustParse(v)
next := version.IncPatch()
fmt.Println(next)
return nil
}

func main() {
if err := mainE(os.Args...); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
123 changes: 123 additions & 0 deletions next-semver/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2021 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause

package main

import (
"errors"
"fmt"
"testing"

semver3 "github.com/Masterminds/semver/v3"
)

var nextSemVerTests = []struct {
previous, desired string
expected string
}{
{
previous: "0.0.0",
desired: "",
expected: "0.0.1",
},
{
previous: "0.0.1",
desired: "",
expected: "0.0.2",
},
{
previous: "0.0.1",
desired: "0.1.0",
expected: "0.1.0",
},
{
previous: "0.9.1",
desired: "1.0.0",
expected: "1.0.0",
},
{
previous: "0.9.9",
desired: "",
expected: "0.9.10",
},
{
previous: "0.9.9-rc",
desired: "",
expected: "0.9.9",
},
}

func version(v string) *semver3.Version {
if v == "" {
return nil
}
return semver3.MustParse(v)
}

func TestNextSemVer(t *testing.T) {
for _, tc := range nextSemVerTests {
testName := fmt.Sprintf("(%q, %q)->%q", tc.previous, tc.desired, tc.expected)
t.Run(testName, func(t *testing.T) {
next, err := nextSemVer(version(tc.previous), version(tc.desired))
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
got := next.String()
want := tc.expected
if got != want {
t.Fatalf("want %s got %s", want, got)
}
})
}
}

var mainEIssues = []struct {
args []string
expected error
}{
{
args: []string{"next-semver", "0.0.0"},
expected: nil,
},
{
args: []string{"next-semver", "0.0.1", "0.0.0"},
expected: ErrBadVersionBump,
},
{
args: []string{"next-semver", "0.0.1", "0.1.0"},
expected: nil,
},
{
args: []string{"next-semver", "1.0.1", "0.9.0"},
expected: ErrBadVersionBump,
},
{
args: []string{"next-semver", "blah1.0.1", "0.9.0"},
expected: semver3.ErrInvalidSemVer,
},
{
args: []string{"next-semver", "1.0.1", "blah0.9.0"},
expected: semver3.ErrInvalidSemVer,
},
{
args: []string{"next-semver", "1.0.1", "0.9.0.1"},
expected: semver3.ErrInvalidSemVer,
},
{
args: []string{"next-semver"},
expected: ErrBadInputs,
},
}

func TestMainE(t *testing.T) {
for _, tc := range mainEIssues {
testName := fmt.Sprintf("%v->%v", tc.args, tc.expected)
t.Run(testName, func(t *testing.T) {
got := mainE(tc.args...)
want := tc.expected
if !errors.Is(got, want) {
t.Fatalf("want %v got %v", want, got)
}
})
}
}
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.3.58
0.3.59

0 comments on commit 699f3c0

Please sign in to comment.