Skip to content

Commit

Permalink
add VersionNumFromSemver for builds (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
zackattack01 authored Nov 26, 2024
1 parent c647b1a commit fbf6f0f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
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

0 comments on commit fbf6f0f

Please sign in to comment.