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

add VersionNumFromSemver for builds #66

Merged
merged 1 commit into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
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
17 changes: 13 additions & 4 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,17 @@ const (
patchVersionMultiplier int = 1
)

// VersionNum parses the semver version string to look for only the major.minor.patch portion,
// VersionNumFromSemver parses the semver version string to look for only the major.minor.patch portion,
// splits that into 3 parts (disregarding any extra portions), and applies a multiplier to each
// part to generate a total int value representing the semver.
// note that this will generate a sortable, and reversible integer as long as all parts remain less than 1000
// This is currently intended for use in generating comparable versions to set within windows registry entries,
// allowing for an easy "upgrade-only" detection configuration within intune.
// Zero is returned for any case where the version cannot be reliably translated
func VersionNum() int {
semverMatch := semverRegexp.FindStringSubmatch(version)
// Zero is returned for any case where the version cannot be reliably translated.
// VersionNumFromSemver should be used where the build time version value cannot be controlled- to restrict
// translations to the internally set version, use VersionNum
func VersionNumFromSemver(semver string) int {
semverMatch := semverRegexp.FindStringSubmatch(semver)
// expect the leftmost match as semverMatch[0] and the semver substring as semverMatch[1]
if semverMatch == nil || len(semverMatch) != 2 {
return 0
Expand Down Expand Up @@ -142,6 +144,13 @@ func VersionNum() int {
return versionNum
}

// VersionNum returns an integer representing the version value set at build time.
// see VersionNumFromSemver for additional details regarding the general translation process
// and limitations. this will return 0 if version is unset/unknown
func VersionNum() int {
return VersionNumFromSemver(version)
}

// SemverFromVersionNum provides the inverse functionality of VersionNum, allowing us
// to collect and report the integer version in a readable semver format
func SemverFromVersionNum(versionNum int) string {
Expand Down
8 changes: 8 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ func Test_VersionNum(t *testing.T) {
semver: "",
expectedVersionNum: 0,
},
"unset version": {
semver: "unknown",
expectedVersionNum: 0,
},
"basic version": {
semver: "1.2.3",
expectedVersionNum: 1002003,
},
"4_part_version": {
semver: "1.2.3.4",
expectedVersionNum: 1002003,
},
"max version": {
semver: "999.999.999",
expectedVersionNum: 999999999,
Expand Down
Loading