diff --git a/api-server b/api-server index f94f0b9..8a43700 100755 Binary files a/api-server and b/api-server differ diff --git a/go.mod b/go.mod index f02dd1b..41fb80a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23.3 require ( github.com/gin-gonic/gin v1.10.0 github.com/go-resty/resty/v2 v2.16.2 + golang.org/x/mod v0.17.0 ) require ( diff --git a/go.sum b/go.sum index d782a5b..f6dd26f 100644 --- a/go.sum +++ b/go.sum @@ -68,6 +68,8 @@ golang.org/x/arch v0.12.0 h1:UsYJhbzPYGsT0HbEdmYcqtCv8UNGvnaL561NnIUvaKg= golang.org/x/arch v0.12.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U= golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk= +golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA= +golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I= golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index f5d28e2..af26449 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,13 @@ package main import ( + "bufio" "fmt" "net/http" "os" "os/exec" + "strconv" + "strings" "github.com/gin-gonic/gin" ) @@ -12,12 +15,12 @@ import ( func updateVersion(repoPath string, serviceName string) error { versionPath := fmt.Sprintf("%s/version.txt", repoPath) versionFile := "version.txt" - newVersion := getVersion(serviceName) if repoPath == "/data/alaskartv/androidtv-ci" { versionPath = fmt.Sprintf("%s/release.txt", repoPath) versionFile = "release.txt" + bumpVersionTv(repoPath) } os.WriteFile(versionPath, []byte(newVersion), 0644) @@ -37,6 +40,40 @@ func updateVersion(repoPath string, serviceName string) error { return nil } +func bumpVersionTv(repoPath string) { + filePath := fmt.Sprintf("%s/version.txt", repoPath) + file, _ := os.Open(filePath) + defer file.Close() + + var updatedContent string + scanner := bufio.NewScanner(file) + for scanner.Scan() { + line := scanner.Text() + + switch { + case strings.HasPrefix(line, "VERSION_NAME="): + parts := strings.Split(line, "=") + currentPatch := parts[1] + newVersion, _ := Bump(currentPatch, "patch") + updatedContent += fmt.Sprintf("VERSION_NAME=%s\n", newVersion) + + case strings.HasPrefix(line, "VERSION_CODE="): + parts := strings.Split(line, "=") + currentCode, _ := strconv.Atoi(parts[1]) + newVersionCode := currentCode + 1 + updatedContent += fmt.Sprintf("VERSION_CODE=%d\n", newVersionCode) + + default: + updatedContent += line + "\n" + } + } + + if err := scanner.Err(); err != nil { + fmt.Errorf("error reading %s: %w", filePath, err) + } + +} + func main() { router := gin.Default() diff --git a/semverbump.go b/semverbump.go new file mode 100644 index 0000000..39e1d8b --- /dev/null +++ b/semverbump.go @@ -0,0 +1,77 @@ +// Package semverbump provides a function to bump a semVer tag, extending golang.org/x/mod/semver library. +// +// The function Bump() takes a semVer tag and a bump type (major, minor, patch) and returns the new semVer tag. +// For example if the current semVer tag is v1.0.0 and the bump type is patch, the new semVer tag will be v1.0.1. +// If the current semVer tag is v1.0.1 and the bump type is minor, the new semVer tag will be v1.1.0 +// (note that the patch version is reset to 0). +// +// Shortened semVer tags are supported, for example if the current semVer tag is v1.0 and the bump type is patch, +// the bumped semVer tag will be v1.0.1. +// +// If the current semVer tag does not have a leading v, it will be added. +// If it is an empty string, it will behave as if the current semVer tag is v0.0.0, e.g. if the bump type is patch, +// the bumped semVer tag will be v0.0.1. +// +// If the current semVer tag is invalid, such as "x.y.z", an error will be returned. +// +// The library currently does not handle pre-release or build metadata. +package main + +import ( + "fmt" + "golang.org/x/mod/semver" + "strconv" + "strings" +) + +// Bump takes a semVer tag and a bump type (major, minor, patch) and returns a new semVer tag +// whose value is incremented according to the bump type. If the current semVer tag does not have a leading v, +// it will be added. If it is an empty string, it will behave as if the current semVer tag is v0.0.0 +// Invalid semVer tags will return an error. +func Bump(currentVersion, majorMinorPatch string) (string, error) { + if currentVersion == "" { + currentVersion = "v0.0.0" + } else { + if !semver.IsValid(currentVersion) { + if semver.IsValid("v" + currentVersion) { + currentVersion = "v" + currentVersion + } else { + return "", fmt.Errorf("invalid semVer tag '%s'", currentVersion) + } + } + // semVer lib allows v1 and v1.0, so check for that + if currentVersion == semver.Major(currentVersion) { + currentVersion = currentVersion + ".0.0" + } + if currentVersion == semver.MajorMinor(currentVersion) { + currentVersion = currentVersion + ".0" + } + } + // now we have a valid currentVersion + newMajor, newMinor, newPatch := majorMinorPatchFromSemVer(currentVersion) + if majorMinorPatch == "major" { + newMajor++ + newMinor = 0 + newPatch = 0 + } else if majorMinorPatch == "minor" { + newMinor++ + newPatch = 0 + } else if majorMinorPatch == "patch" { + newPatch++ + } else { + return "", fmt.Errorf("invalid arg for majorMinorPatch, expect 'major', 'minor' or 'patch', got '%s'", majorMinorPatch) + } + return semVerFromMajorMinorPatch(newMajor, newMinor, newPatch), nil +} + +func majorMinorPatchFromSemVer(semVer string) (int, int, int) { + parts := strings.Split(semVer, ".") + major, _ := strconv.Atoi(strings.TrimPrefix(parts[0], "v")) + minor, _ := strconv.Atoi(parts[1]) + patch, _ := strconv.Atoi(parts[2]) + return major, minor, patch +} + +func semVerFromMajorMinorPatch(major, minor, patch int) string { + return "v" + strconv.Itoa(major) + "." + strconv.Itoa(minor) + "." + strconv.Itoa(patch) +}