1
0

Implement match for Debian packages

This commit is contained in:
dump_stack() 2023-05-12 17:26:53 +00:00
parent b5d4bdd5f4
commit 43aa116682
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
2 changed files with 74 additions and 0 deletions

View File

@ -1,5 +1,15 @@
package debian package debian
import (
"os/user"
"regexp"
"strings"
"github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/config"
)
type CodeName int type CodeName int
const ( const (
@ -23,3 +33,47 @@ var CodeNameStrings = [...]string{
func (cn CodeName) String() string { func (cn CodeName) String() string {
return CodeNameStrings[cn] 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
}

View File

@ -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")
}
}