Skip to content

Commit

Permalink
Rename Version function to Bump and make it return another SemVer ins…
Browse files Browse the repository at this point in the history
…tance
  • Loading branch information
choffmeister committed Dec 9, 2023
1 parent 6d9dd91 commit 0caa980
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
9 changes: 3 additions & 6 deletions internal/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type GenerateVersionOptions struct {
PrereleaseSuffix string
PrereleasePrefix string
PrereleaseTimestamped bool
NextRelease string
NextRelease string
Format string
}

Expand Down Expand Up @@ -65,15 +65,12 @@ func GenerateVersion(tagName string, counter int, headHash string, timestamp tim
}
}
}
version = version.Bump(opts.NextRelease)
if opts.DropTagNamePrefix {
version.Prefix = ""
}
var result string
if opts.NextRelease != "" {
result = version.Version(opts.NextRelease)
} else {
result = version.String()
}
result = version.String()
if opts.Format != "" {
result = strings.ReplaceAll(opts.Format, "<version>", result)
}
Expand Down
23 changes: 17 additions & 6 deletions internal/semver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,14 @@ func (v SemVer) Equal(v2 SemVer) bool {
equalStringSlice(v.BuildMetadata, v2.BuildMetadata)
}

func (v SemVer) Version(nextRelease string) string {
patch := v.Patch
// Bump ...
func (v SemVer) Bump(nextRelease string) *SemVer {
if nextRelease == "" {
return &v
}

isPrerelease := len(v.Prerelease) > 0
patch := v.Patch
if nextRelease == "patch" && !isPrerelease {
patch++
}
Expand All @@ -49,17 +54,23 @@ func (v SemVer) Version(nextRelease string) string {
minor = 0
patch = 0
}
return fmt.Sprintf("%s%d.%d.%d", v.Prefix, major, minor, patch)
v2 := SemVer{
Prefix: v.Prefix,
Major: major,
Minor: minor,
Patch: patch,
}
return &v2
}

// String ...
func (v SemVer) String() string {
str := v.Version("")
str := fmt.Sprintf("%s%d.%d.%d", v.Prefix, v.Major, v.Minor, v.Patch)
if len(v.Prerelease) > 0 {
str += "-" + strings.Join(v.Prerelease, ".")
str = str + "-" + strings.Join(v.Prerelease, ".")
}
if len(v.BuildMetadata) > 0 {
str += "+" + strings.Join(v.BuildMetadata, ".")
str = str + "+" + strings.Join(v.BuildMetadata, ".")
}
return str
}
Expand Down

0 comments on commit 0caa980

Please sign in to comment.