Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup version.go #52

Merged
merged 2 commits into from
Sep 10, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions pkg/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,20 @@ const positionMajor = 0
const positionMinor = 1
const positionPatch = 2

const incrementChar = "x"
const wildcardChar = "?"
const vPrefix = "v"

var versionDecreaseError = fmt.Errorf("version decrease in combination with auto increment is not supported")

func Next(current string, patternNext string) (string, error) {
_, current = splitPrefix("v", current)
_, current = splitPrefix(vPrefix, current)
currentCore, _ := coreAndExtension(current)

nextPrefix, patternNext := splitPrefix("v", patternNext)
nextPrefix, patternNext := splitPrefix(vPrefix, patternNext)
nextCore, nextExtension := coreAndExtension(patternNext)

positionX, err := findX(nextCore)
positionIncrement, err := findIncrementChar(nextCore)
if err != nil {
return "", err
}
Expand All @@ -46,7 +50,7 @@ func Next(current string, patternNext string) (string, error) {
if err != nil {
return "", err
}
nextMajorMinorPatchList, err = findAndReplaceQuestionMarksAndXs(nextMajorMinorPatchList, currentMajorMinorPatchList)
nextMajorMinorPatchList, err = replaceWildcardsAndIncrements(nextMajorMinorPatchList, currentMajorMinorPatchList)
if err != nil {
return "", err
}
Expand All @@ -59,7 +63,7 @@ func Next(current string, patternNext string) (string, error) {
return "", err
}

switch positionX {
switch positionIncrement {
case positionMajor:
nextMajor = currentMajor + 1
case positionMinor:
Expand All @@ -81,11 +85,11 @@ func Next(current string, patternNext string) (string, error) {
return fmt.Sprintf("%v%v.%v.%v%v", nextPrefix, nextMajor, nextMinor, nextPatch, nextExtension), nil
}

func findAndReplaceQuestionMarksAndXs(nextCore []string, currentCore []string) ([]string, error) {
func replaceWildcardsAndIncrements(nextCore []string, currentCore []string) ([]string, error) {
for i := 0; i < 3; i++ {
if nextCore[i] == "?" {
if nextCore[i] == wildcardChar {
nextCore[i] = currentCore[i]
} else if nextCore[i] == "x" {
} else if nextCore[i] == incrementChar {
nextCore[i] = "0"
}
}
Expand All @@ -101,47 +105,45 @@ func majorMinorPatchList(versionCore string) ([]string, error) {
}

func majorMinorPatchListToInt(versionCore []string) (int, int, int, error) {
major, err := strconv.Atoi(versionCore[0])
major, err := strconv.Atoi(versionCore[positionMajor])
if err != nil {
return 0, 0, 0, err
}
minor, err := strconv.Atoi(versionCore[1])
minor, err := strconv.Atoi(versionCore[positionMinor])
if err != nil {
return 0, 0, 0, err
}
patch, err := strconv.Atoi(versionCore[2])
patch, err := strconv.Atoi(versionCore[positionPatch])
if err != nil {
return 0, 0, 0, err
}
return major, minor, patch, nil
}

func splitPrefix(prefix string, str string) (string, string) {
var foundPrefix string
if strings.HasPrefix(str, prefix) {
str = str[len(prefix):]
foundPrefix = prefix
if !strings.HasPrefix(str, prefix) {
return "", str
}
return foundPrefix, str
return prefix, str[len(prefix):]
}

// returns 0 if major is x
// returns 1 if minor is x
// returns 2 if patch is x
// returns an error if there is more than one x in major, minor, and patch combined
func findX(versionCore string) (int, error) {
positionX := -1
var countX int
// Returns 0 if major is incrementChar
// Returns 1 if minor is incrementChar
// Returns 2 if patch is incrementChar
// Returns an error if there is more than one incrementChar in major, minor, and patch combined
func findIncrementChar(versionCore string) (int, error) {
positionIncrement := -1
var countIncrement int
for i, s := range strings.Split(versionCore, ".") {
if s == "x" {
positionX = i
countX++
if s == incrementChar {
positionIncrement = i
countIncrement++
}
if countX > 1 {
return 0, fmt.Errorf("only one x allowed")
if countIncrement > 1 {
return 0, fmt.Errorf("only one %s allowed", incrementChar)
}
}
return positionX, nil
return positionIncrement, nil
}

func coreAndExtension(version string) (string, string) {
Expand Down