Skip to content

Commit

Permalink
feat: simplify metadata/build api (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 authored Mar 7, 2023
1 parent de12426 commit e67acc8
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 60 deletions.
63 changes: 19 additions & 44 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,11 @@ var (
minorCmd = app.Command("minor", "new minor version").Alias("m")
patchCmd = app.Command("patch", "new patch version").Alias("p")
currentCmd = app.Command("current", "prints current version").Alias("c")
metadata = app.Flag("metadata", "discards pre-release and build metadata if disabled (--no-metadata)").Default("true").Bool()
pattern = app.Flag("pattern", "limits calculations to be based on tags matching the given pattern").String()
preRelease = app.Flag("pre-release", "discards pre-release metadata if disabled (--no-pre-release)").Default("true").Bool()
build = app.Flag("build", "discards build metadata if disabled (--no-build)").Default("true").Bool()
prefix = app.Flag("prefix", "set a custom prefix").Default("v").String()
suffix = app.Flag("suffix", "set a custom a custom suffix (metadata and/or prerelease)").String()
stripPrefix = app.Flag("strip-prefix", "strips the prefix from the tag").Default("false").Bool()
preRelease = app.Flag("pre-release", "adds a pre-release suffix to the version, without the semver mandatory dash prefix").String()
build = app.Flag("build", "adds a build suffix to the version, without the semver mandatory plug prefix").String()
tagMode = app.Flag("tag-mode", "determines if latest tag of the current or all branches will be used").Default("current-branch").Enum("current-branch", "all-branches")
forcePatchIncrement = nextCmd.Flag("force-patch-increment", "forces a patch version increment regardless of the commit message content").Default("false").Bool()
)
Expand All @@ -43,18 +41,16 @@ func main() {
current, err := getCurrentVersion(tag)
app.FatalIfError(err, "could not get current version from tag: '%s'", tag)

if !*metadata {
current = unsetMetadata(current)
}

if !*preRelease {
current = unsetPreRelease(current)
}
result, err := nextVersion(cmd, current, tag, *preRelease, *build)
app.FatalIfError(err, "could not get next tag: '%s'", tag)

if !*build {
current = unsetBuild(current)
if *stripPrefix {
fmt.Println(result.String())
}
fmt.Println(*prefix + result.String())
}

func nextVersion(cmd string, current *semver.Version, tag, preRelease, build string) (semver.Version, error) {
var result semver.Version
switch cmd {
case nextCmd.FullCommand():
Expand All @@ -69,9 +65,16 @@ func main() {
result = *current
}

result.SetMetadata(current.Metadata())
result.SetPrerelease(current.Prerelease())
fmt.Println(getVersion(tag, *prefix, result.String(), *suffix, *stripPrefix))
var err error
result, err = result.SetPrerelease(preRelease)
if err != nil {
return result, err
}
result, err = result.SetMetadata(build)
if err != nil {
return result, err
}
return result, nil
}

func getCurrentVersion(tag string) (*semver.Version, error) {
Expand All @@ -85,34 +88,6 @@ func getCurrentVersion(tag string) (*semver.Version, error) {
return current, err
}

func getVersion(tag, prefix, result, suffix string, stripPrefix bool) string {
if stripPrefix {
prefix = ""
}

if suffix != "" {
result = result + "-" + suffix
}

return prefix + result
}

func unsetPreRelease(current *semver.Version) *semver.Version {
newV, _ := current.SetPrerelease("")

return &newV
}

func unsetBuild(current *semver.Version) *semver.Version {
newV, _ := current.SetMetadata("")

return &newV
}

func unsetMetadata(current *semver.Version) *semver.Version {
return unsetBuild(unsetPreRelease(current))
}

func findNext(current *semver.Version, tag string) semver.Version {
log, err := git.Changelog(tag)
app.FatalIfError(err, "failed to get changelog")
Expand Down
144 changes: 128 additions & 16 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,134 @@ built by: goreleaser`, buildVersion("v1.2.3", "a123cd", "2021-01-02", "gorelease
})
}

func TestUnsetMetadata(t *testing.T) {
is.New(t).True(semver.MustParse("v2.3.4").Equal(unsetMetadata(semver.MustParse("v2.3.4-beta+asd123"))))
}

func TestStripPrefixReturnsVersionOnly(t *testing.T) {
is.New(t).True(getVersion("v2.3.4", "v", "4.5.6", "", true) == "4.5.6")
}

func TestStripPrefixWhenNoPrefixReturnsVersionOnly(t *testing.T) {
is.New(t).True(getVersion("2.3.4", "v", "4.5.6", "", true) == "4.5.6")
}
func TestCmd(t *testing.T) {
ver := func() *semver.Version { return semver.MustParse("1.2.3-pre+123") }
t.Run(currentCmd.FullCommand(), func(t *testing.T) {
cmd := currentCmd.FullCommand()
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "")
is.NoErr(err)
is.Equal("1.2.3", v.String())
})
t.Run("build", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124")
is.NoErr(err)
is.Equal("1.2.3+124", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "")
is.NoErr(err)
is.Equal("1.2.3-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125")
is.NoErr(err)
is.Equal("1.2.3-alpha.2+125", v.String())
})
})

func TestNoStripPrefixReturnsPrefixAndVersion(t *testing.T) {
is.New(t).True(getVersion("v2.3.4", "v", "4.5.6", "", false) == "v4.5.6")
}
t.Run(minorCmd.FullCommand(), func(t *testing.T) {
cmd := minorCmd.FullCommand()
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "")
is.NoErr(err)
is.Equal("1.3.0", v.String())
})
t.Run("build", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124")
is.NoErr(err)
is.Equal("1.3.0+124", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "")
is.NoErr(err)
is.Equal("1.3.0-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125")
is.NoErr(err)
is.Equal("1.3.0-alpha.2+125", v.String())
})
})
t.Run(patchCmd.FullCommand(), func(t *testing.T) {
cmd := patchCmd.FullCommand()
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "", "")
is.NoErr(err)
is.Equal("1.2.4", v.String())
})
t.Run("original had no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3-alpha.1+1"), "v1.2.3", "", "")
is.NoErr(err)
is.Equal("1.2.3", v.String())
})
t.Run("build", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "", "124")
is.NoErr(err)
is.Equal("1.2.4+124", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "alpha.1", "")
is.NoErr(err)
is.Equal("1.2.4-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, semver.MustParse("1.2.3"), "v1.2.3", "alpha.2", "125")
is.NoErr(err)
is.Equal("1.2.4-alpha.2+125", v.String())
})
})
t.Run(majorCmd.FullCommand(), func(t *testing.T) {
cmd := majorCmd.FullCommand()
t.Run("no meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "")
is.NoErr(err)
is.Equal("2.0.0", v.String())
})
t.Run("build", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "", "124")
is.NoErr(err)
is.Equal("2.0.0+124", v.String())
})
t.Run("prerel", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.1", "")
is.NoErr(err)
is.Equal("2.0.0-alpha.1", v.String())
})
t.Run("all meta", func(t *testing.T) {
is := is.New(t)
v, err := nextVersion(cmd, ver(), "v1.2.3", "alpha.2", "125")
is.NoErr(err)
is.Equal("2.0.0-alpha.2+125", v.String())
})
})

func TestSuffix(t *testing.T) {
is.New(t).True(getVersion("v2.3.4", "v", "4.5.6", "dev", false) == "v4.5.6-dev")
t.Run("errors", func(t *testing.T) {
t.Run("invalid build", func(t *testing.T) {
is := is.New(t)
_, err := nextVersion(minorCmd.FullCommand(), semver.MustParse("1.2.3"), "v1.2.3", "", "+125")
is.True(err != nil)
})
t.Run("invalid prerelease", func(t *testing.T) {
is := is.New(t)
_, err := nextVersion(minorCmd.FullCommand(), semver.MustParse("1.2.3"), "v1.2.3", "+aaa", "")
is.True(err != nil)
})
})
}

0 comments on commit e67acc8

Please sign in to comment.