From faa8a926bea66ed1207e32d6aca0fb9720c4bb9d Mon Sep 17 00:00:00 2001 From: thorgull Date: Mon, 24 Jan 2022 10:55:05 +0100 Subject: [PATCH] Add prerelease-prefix parameter --- cmd/root.go | 2 ++ cmd/root_test.go | 8 ++++---- internal/generate.go | 3 ++- internal/generate_test.go | 33 ++++++++++++++++++--------------- 4 files changed, 26 insertions(+), 20 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 2e11fcf..fd8082c 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -30,6 +30,7 @@ func Execute(version FullVersion) error { fallbackFlag := flag.String("fallback", "", "The first version to fallback to should there be no tag") dropPrefixFlag := flag.Bool("drop-prefix", false, "Drop prefix from output") prereleaseSuffixFlag := flag.String("prerelease-suffix", "", "Suffix to add to prereleases") + prereleasePrefixFlag := flag.String("prerelease-prefix", "dev", "Prefix to use as start of prerelease (default to \"dev\"))") formatFlag := flag.String("format", "", "Format of output") flag.Parse() @@ -41,6 +42,7 @@ func Execute(version FullVersion) error { FallbackTagName: *fallbackFlag, DropTagNamePrefix: *dropPrefixFlag, PrereleaseSuffix: *prereleaseSuffixFlag, + PrereleasePrefix: *prereleasePrefixFlag, Format: *formatFlag, } result, err := run(dir, opts) diff --git a/cmd/root_test.go b/cmd/root_test.go index 463ddef..e35cbee 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -14,24 +14,24 @@ func TestRun(t *testing.T) { assert := assert.New(t) dir, _ := ioutil.TempDir("", "example") author := object.Signature{Name: "Test", Email: "test@test.com"} - _, err := run(dir, internal.GenerateVersionOptions{}) + _, err := run(dir, internal.GenerateVersionOptions{PrereleasePrefix: "dev"}) assert.Error(err) repo, _ := git.PlainInit(dir, false) worktree, _ := repo.Worktree() - _, err = run(dir, internal.GenerateVersionOptions{}) + _, err = run(dir, internal.GenerateVersionOptions{PrereleasePrefix: "dev"}) assert.Error(err) commit1, _ := worktree.Commit("first", &git.CommitOptions{Author: &author}) repo.CreateTag("invalid", commit1, nil) - _, err = run(dir, internal.GenerateVersionOptions{}) + _, err = run(dir, internal.GenerateVersionOptions{PrereleasePrefix: "dev"}) assert.Error(err) commit2, _ := worktree.Commit("first", &git.CommitOptions{Author: &author}) repo.CreateTag("v1.0.0", commit2, nil) commit3, _ := worktree.Commit("second", &git.CommitOptions{Author: &author}) - result, err := run(dir, internal.GenerateVersionOptions{}) + result, err := run(dir, internal.GenerateVersionOptions{PrereleasePrefix: "dev"}) assert.NoError(err) assert.Equal("v1.0.1-dev.1.g"+commit3.String()[0:7], *result) } diff --git a/internal/generate.go b/internal/generate.go index 411ab2a..388d715 100644 --- a/internal/generate.go +++ b/internal/generate.go @@ -11,12 +11,13 @@ type GenerateVersionOptions struct { FallbackTagName string DropTagNamePrefix bool PrereleaseSuffix string + PrereleasePrefix string Format string } // GenerateVersion ... func GenerateVersion(tagName string, counter int, headHash string, opts GenerateVersionOptions) (*string, error) { - devPrerelease := []string{"dev", strconv.Itoa(counter), "g" + (headHash)[0:7]} + devPrerelease := []string{opts.PrereleasePrefix, strconv.Itoa(counter), "g" + (headHash)[0:7]} if opts.PrereleaseSuffix != "" { devPrerelease[len(devPrerelease)-1] = devPrerelease[len(devPrerelease)-1] + "-" + opts.PrereleaseSuffix } diff --git a/internal/generate_test.go b/internal/generate_test.go index 23ca627..e826401 100644 --- a/internal/generate_test.go +++ b/internal/generate_test.go @@ -15,24 +15,27 @@ func TestGenerateVersion(t *testing.T) { } } - test("0.0.0", 0, "abc1234", GenerateVersionOptions{}, "0.0.0") - test("0.0.0", 1, "abc1234", GenerateVersionOptions{}, "0.0.1-dev.1.gabc1234") - test("0.0.0-rc1", 1, "abc1234", GenerateVersionOptions{}, "0.0.0-rc1.dev.1.gabc1234") - test("0.0.0-rc.1", 1, "abc1234", GenerateVersionOptions{}, "0.0.0-rc.1.dev.1.gabc1234") - test("0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{}, "0.0.0-rc.1.dev.1.gabc1234+foobar") - test("v0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{}, "v0.0.0-rc.1.dev.1.gabc1234+foobar") + test("0.0.0", 0, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.0") + test("0.0.0", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.1-dev.1.gabc1234") + test("0.0.0-rc1", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.0-rc1.dev.1.gabc1234") + test("0.0.0-rc.1", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.0-rc.1.dev.1.gabc1234") + test("0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "0.0.0-rc.1.dev.1.gabc1234+foobar") + test("v0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}, "v0.0.0-rc.1.dev.1.gabc1234+foobar") - test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "0.0.0"}, "0.0.0-dev.1.gabc1234") - test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0"}, "v0.0.0-dev.1.gabc1234") + test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "0.0.0", PrereleasePrefix: "dev"}, "0.0.0-dev.1.gabc1234") + test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", PrereleasePrefix: "dev"}, "v0.0.0-dev.1.gabc1234") - test("v0.0.0", 0, "abc1234", GenerateVersionOptions{PrereleaseSuffix: "SNAPSHOT"}, "v0.0.0") - test("v0.0.0", 1, "abc1234", GenerateVersionOptions{PrereleaseSuffix: "SNAPSHOT"}, "v0.0.1-dev.1.gabc1234-SNAPSHOT") + test("v0.0.0", 0, "abc1234", GenerateVersionOptions{PrereleaseSuffix: "SNAPSHOT", PrereleasePrefix: "dev"}, "v0.0.0") + test("v0.0.0", 1, "abc1234", GenerateVersionOptions{PrereleaseSuffix: "SNAPSHOT", PrereleasePrefix: "dev"}, "v0.0.1-dev.1.gabc1234-SNAPSHOT") - test("v0.0.0", 0, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true}, "0.0.0") - test("v0.0.0-rc.1", 1, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true}, "0.0.0-rc.1.dev.1.gabc1234") - test("v0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true}, "0.0.0-rc.1.dev.1.gabc1234+foobar") - test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", DropTagNamePrefix: true}, "0.0.0-dev.1.gabc1234") + test("v0.0.0", 0, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true, PrereleasePrefix: "dev"}, "0.0.0") + test("v0.0.0-rc.1", 1, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true, PrereleasePrefix: "dev"}, "0.0.0-rc.1.dev.1.gabc1234") + test("v0.0.0-rc.1+foobar", 1, "abc1234", GenerateVersionOptions{DropTagNamePrefix: true, PrereleasePrefix: "dev"}, "0.0.0-rc.1.dev.1.gabc1234+foobar") + test("", 1, "abc1234", GenerateVersionOptions{FallbackTagName: "v0.0.0", DropTagNamePrefix: true, PrereleasePrefix: "dev"}, "0.0.0-dev.1.gabc1234") - _, err := GenerateVersion("", 1, "abc1234", GenerateVersionOptions{}) + test("0.0.0", 0, "abc1234", GenerateVersionOptions{PrereleasePrefix: "custom"}, "0.0.0") + test("0.0.0", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "custom"}, "0.0.1-custom.1.gabc1234") + + _, err := GenerateVersion("", 1, "abc1234", GenerateVersionOptions{PrereleasePrefix: "dev"}) assert.Error(err) }