From 2452b090b063ef0d6aa827fb6334b48ac9f43ac0 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Sun, 14 May 2023 17:14:05 +0000 Subject: [PATCH] feat: use semver wrapper to match debian release --- distro/debian/debian.go | 81 +++++++++--------------------------- distro/debian/debian_test.go | 25 ----------- 2 files changed, 20 insertions(+), 86 deletions(-) diff --git a/distro/debian/debian.go b/distro/debian/debian.go index 9c41b91..8858458 100644 --- a/distro/debian/debian.go +++ b/distro/debian/debian.go @@ -4,7 +4,6 @@ import ( "errors" "fmt" "regexp" - "strconv" "strings" "github.com/rs/zerolog/log" @@ -69,78 +68,38 @@ func releaseFromString(s string) (r Release) { return } -func parseKernelMajorMinor(deb string) (major, minor int, err error) { +func kernelRelease(deb string) (r Release, err error) { // linux-image-4.17.0-2-amd64 -> 4.17 re := regexp.MustCompile(`([0-9]*\.[0-9]*)`) - - s := re.FindString(deb) - if s == "" { + sver := re.FindString(deb) + if sver == "" { err = errors.New("empty result") return } + version := kver(sver) - split := strings.Split(s, ".") - if len(split) != 2 { - err = errors.New("unexpected input") - return - } - - major, err = strconv.Atoi(split[0]) - if err != nil { - return - } - - minor, err = strconv.Atoi(split[1]) - if err != nil { - return - } - - return -} - -func kernelRelease(deb string) (r Release, err error) { - major, minor, err := parseKernelMajorMinor(deb) - if err != nil { - return - } - - // Wheezy 3.2 - // Jessie 3.16 - // Stretch 4.9 - // Buster 4.19 - // Bullseye 5.10 - - if major < 3 { + if version.LessThan(kver("3.0-rc0")) { err = errors.New("not supported") return } - switch major { - case 3: - //if minor < 16 { + if version.LessThan(kver("3.8-rc0")) { + // Wheezy 3.2 // >=3.8 breaks initramfs-tools << 0.110~ // Wheezy initramfs-tools version is 0.109.1 - if minor < 8 { - r = Wheezy - } else { - r = Jessie - } - case 4: - if minor < 9 { - r = Jessie - } else if minor < 19 { - r = Stretch - } else { - r = Buster - } - case 5: - if minor < 10 { - r = Buster - } else { - r = Bullseye - } - default: - r = Bullseye // latest release + r = Wheezy + } else if version.LessThan(kver("4.9-rc0")) { + // Jessie 3.16 + r = Jessie + } else if version.LessThan(kver("4.19-rc0")) { + // Stretch 4.9 + r = Stretch + } else if version.LessThan(kver("5.10-rc0")) { + // Buster 4.19 + r = Buster + } else { + // Bullseye 5.10 + r = Bullseye } return diff --git a/distro/debian/debian_test.go b/distro/debian/debian_test.go index 1b9c13c..7ce0727 100644 --- a/distro/debian/debian_test.go +++ b/distro/debian/debian_test.go @@ -34,31 +34,6 @@ func TestMatchImagePkg(t *testing.T) { } } -func TestParseKernelMajorMinor(t *testing.T) { - type testcase struct { - Deb string - Major, Minor int - } - - for _, tc := range []testcase{ - testcase{"linux-image-4.17.0-2-amd64", 4, 17}, - testcase{"linux-image-6.1.0-8-amd64-unsigned", 6, 1}, - testcase{"linux-image-6.1.0-0.deb11.7-amd64-unsigned", 6, 1}, - testcase{"linux-image-3.2.0-0.bpo.4-amd64_3.2.41-2+deb7u2~bpo60+1_amd64", 3, 2}, - testcase{"linux-image-5.16.0-rc5-amd64-unsigned_5.16~rc5-1~exp1_amd64", 5, 16}, - testcase{"linux-image-3.6-trunk-amd64_3.8.4-1~experimental.1_amd64", 3, 6}, - } { - major, minor, err := parseKernelMajorMinor(tc.Deb) - if err != nil { - t.Fatal(err) - } - if major != tc.Major || minor != tc.Minor { - t.Fatalf("%v -> %v.%v != %v.%v", tc.Deb, major, minor, - tc.Major, tc.Minor) - } - } -} - func TestKernelRelease(t *testing.T) { type testcase struct { Deb string