Skip to content

Commit

Permalink
Merge pull request #3 from haya14busa/support-current
Browse files Browse the repository at this point in the history
Support showing current latest version
  • Loading branch information
haya14busa authored May 9, 2020
2 parents 4db9843 + bf29875 commit 9ab5412
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
16 changes: 12 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const (
PATCH Level = iota
MINOR
MAJOR
CURRENT // Do not bump and show the latest version.
)

func main() {
Expand All @@ -33,6 +34,7 @@ func main() {
func usage() {
fmt.Fprintln(os.Stderr, `Usage: bump [major,minor,patch (default=patch)]
bump returns next semantic version tag`)
flag.PrintDefaults()
fmt.Fprintln(os.Stderr, "https://github.com/haya14busa/bump")
os.Exit(2)
}
Expand All @@ -48,20 +50,21 @@ func run(w io.Writer) error {
if len(tags) == 0 {
return errors.New("existing tag not found")
}
next, err := nextTag(tags, bumpLevel(flag.Args()))
latest := latestSemVer(tags)
next, err := nextTag(latest, bumpLevel(flag.Args()))
if err != nil {
return err
}
fmt.Fprintln(w, next)
return nil
}

func nextTag(tags []string, level Level) (string, error) {
latest, err := semver.NewVersion(latestSemVer(tags))
func nextTag(latest string, level Level) (string, error) {
latestVer, err := semver.NewVersion(latest)
if err != nil {
return "", err
}
next := nextSemVer(latest, level)
next := nextSemVer(latestVer, level)
return next.Original(), nil
}

Expand Down Expand Up @@ -95,6 +98,9 @@ func nextSemVer(v *semver.Version, level Level) semver.Version {
return v.IncMinor()
case MAJOR:
return v.IncMajor()
case CURRENT:
// Do nothing.
return *v
}
log.Fatalf("unknown level: %v", level)
return v.IncPatch()
Expand All @@ -111,6 +117,8 @@ func bumpLevel(args []string) Level {
return MINOR
case "major":
return MAJOR
case "current":
return CURRENT
}
return PATCH
}
4 changes: 2 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
)

func TestNextTag(t *testing.T) {
got, err := nextTag([]string{"v1.0.1", "v1.11.0"}, PATCH)
got, err := nextTag("v1.11.0", PATCH)
if err != nil {
t.Fatal(err)
}
if want := "v1.11.1"; got != want {
t.Errorf("nextTag({v1.0.1, v1.11.0}, PATCH) = %v, want %v", got, want)
t.Errorf("nextTag('v1.11.0', PATCH) = %v, want %v", got, want)
}
}

0 comments on commit 9ab5412

Please sign in to comment.