Skip to content

Commit

Permalink
helpers/common: move CompareKernelRelease to common
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaeldtinoco authored and Rafael David Tinoco committed Sep 2, 2021
1 parent afc530e commit a453321
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions helpers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package helpers
import (
"fmt"
"os"
"strconv"
"strings"
"syscall"
)
Expand Down Expand Up @@ -35,4 +36,40 @@ func UnameRelease() (string, error) {
ver = strings.Trim(ver, "\x00")

return ver, nil
}

// CompareKernelRelease will compare two given kernel version/release
// strings and return -1, 0 or 1 if given version is less, equal or bigger,
// respectively, than the given one
//
// Examples of $(uname -r):
//
// 5.11.0-31-generic (ubuntu)
// 4.18.0-305.12.1.el8_4.x86_64 (alma)
// 4.18.0-338.el8.x86_64 (stream8)
// 4.18.0-305.7.1.el8_4.centos.x86_64 (centos)
// 4.18.0-305.7.1.el8_4.centos.plus.x86_64 (centos + plus repo)
// 5.13.13-arch1-1 (archlinux)
//
func CompareKernelRelease(base, given string) int {
b := strings.Split(base, "-") // [base]-xxx
b = strings.Split(b[0], ".") // [major][minor][patch]

g := strings.Split(given, "-")
g = strings.Split(g[0], ".")

for n := 0; n <= 2; n++ {
i, _ := strconv.Atoi(g[n])
j, _ := strconv.Atoi(b[n])

if i > j {
return 1 // given is bigger
} else if i < j {
return -1 // given is less
} else {
continue // equal
}
}

return 0 // equal
}

0 comments on commit a453321

Please sign in to comment.