From 7942bd22fa944325269afc77e953f9860276a70e Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Mon, 29 May 2023 10:19:29 +0000 Subject: [PATCH] feat: implement parse debian kernel version --- distro/debian/kernel.go | 29 +++++++++++++++++++++++++++++ distro/debian/kernel_test.go | 20 ++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/distro/debian/kernel.go b/distro/debian/kernel.go index 6a95134..630d7bc 100644 --- a/distro/debian/kernel.go +++ b/distro/debian/kernel.go @@ -29,6 +29,35 @@ type DebianKernelVersion struct { ABI string } +func ParseKernelVersion(pkg string) (dkv DebianKernelVersion, err error) { + // -> 4.11.0-trunk-amd64_4.11-1~exp2_amd64.deb + pkg = strings.Replace(pkg, "linux-image-", "", -1) + + // -> [4.11.0-trunk-amd64 4.11-1~exp2 amd64.deb] + fields := strings.Split(pkg, "_") + + if len(fields) != 3 { + err = errors.New("incorrect input format") + return + } + + // 4.11.0-trunk-amd64 -> 4.11.0-trunk + // TODO other archs? + dkv.ABI = strings.Split(fields[0], "-amd64")[0] + if dkv.ABI == "" { + err = errors.New("incorrect input format") + return + } + + dkv.Package = fields[1] + if dkv.Package == "" { + err = errors.New("incorrect input format") + return + } + + return +} + type DebianKernel struct { Version DebianKernelVersion Image snapshot.Package diff --git a/distro/debian/kernel_test.go b/distro/debian/kernel_test.go index c627c9b..058cfa7 100644 --- a/distro/debian/kernel_test.go +++ b/distro/debian/kernel_test.go @@ -17,3 +17,23 @@ func TestGetDebianKernel(t *testing.T) { t.Logf("%s", spew.Sdump(dk)) } + +func TestParseKernelVersion(t *testing.T) { + assert := assert.New(t) + + kernels, err := GetKernels() + assert.Nil(err) + assert.NotEmpty(kernels) + + versions := make(map[string]bool) + + for _, dk := range kernels { + dkv, err := ParseKernelVersion(dk.Image.Deb.Name) + assert.Nil(err) + + _, found := versions[dkv.Package] + assert.True(!found) + + versions[dkv.Package] = true + } +}