From 43aa116682d85ad7fbd724d88a7452ed113dd387 Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Fri, 12 May 2023 17:26:53 +0000 Subject: [PATCH] Implement match for Debian packages --- distro/debian/debian.go | 54 ++++++++++++++++++++++++++++++++++++ distro/debian/debian_test.go | 20 +++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 distro/debian/debian_test.go diff --git a/distro/debian/debian.go b/distro/debian/debian.go index c5f3088..a89aa33 100644 --- a/distro/debian/debian.go +++ b/distro/debian/debian.go @@ -1,5 +1,15 @@ package debian +import ( + "os/user" + "regexp" + "strings" + + "github.com/rs/zerolog/log" + + "code.dumpstack.io/tools/out-of-tree/config" +) + type CodeName int const ( @@ -23,3 +33,47 @@ var CodeNameStrings = [...]string{ func (cn CodeName) String() string { return CodeNameStrings[cn] } + +var ( + CachePath string + RefetchDays int = 7 +) + +func MatchImagePkg(km config.KernelMask) (pkgs []string, err error) { + if CachePath == "" { + var usr *user.User + usr, err = user.Current() + if err != nil { + return + } + + CachePath = usr.HomeDir + "/.out-of-tree/debian.cache" + log.Debug().Msgf("Use default kernels cache path: %s", CachePath) + } else { + log.Debug().Msgf("Debian kernels cache path: %s", CachePath) + } + + c, err := NewCache(CachePath) + if err != nil { + log.Error().Err(err).Msg("cache") + return + } + defer c.Close() + + kernels, err := GetKernels(c, RefetchDays) + if err != nil { + log.Error().Err(err).Msg("get kernels") + return + } + + r := regexp.MustCompile(km.ReleaseMask) + + for _, dk := range kernels { + p := strings.Replace(dk.Image.Deb.Name, ".deb", "", -1) + if r.MatchString(p) { + pkgs = append(pkgs, p) + } + } + + return +} diff --git a/distro/debian/debian_test.go b/distro/debian/debian_test.go new file mode 100644 index 0000000..8ac138e --- /dev/null +++ b/distro/debian/debian_test.go @@ -0,0 +1,20 @@ +package debian + +import ( + "testing" + + "code.dumpstack.io/tools/out-of-tree/config" +) + +func TestMatchImagePkg(t *testing.T) { + km := config.KernelMask{ReleaseMask: "3.2.0-4"} + + pkgs, err := MatchImagePkg(km) + if err != nil { + t.Fatal(err) + } + + if len(pkgs) == 0 { + t.Fatal("no packages") + } +}