1
0

refactor: move cache-related functions

This commit is contained in:
dump_stack() 2023-05-14 12:37:45 +00:00
parent 0fd9d80940
commit f8f3424e1e
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC
3 changed files with 38 additions and 41 deletions

View File

@ -16,16 +16,12 @@ type DebianCacheCmd struct {
}
func (cmd *DebianCacheCmd) Run() (err error) {
c, err := debian.NewCache(cmd.Path)
if err != nil {
log.Error().Err(err).Msg("cache")
return
}
defer c.Close()
debian.CachePath = cmd.Path
debian.RefetchDays = cmd.Refetch
log.Info().Msg("Fetching kernels...")
_, err = debian.GetKernels(c, cmd.Refetch)
_, err = debian.GetKernels()
if err != nil {
log.Error().Err(err).Msg("")
return

View File

@ -9,10 +9,8 @@ import (
"github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/cache"
"code.dumpstack.io/tools/out-of-tree/config"
"code.dumpstack.io/tools/out-of-tree/container"
"code.dumpstack.io/tools/out-of-tree/fs"
)
type Release int
@ -148,36 +146,8 @@ func kernelRelease(deb string) (r Release, err error) {
return
}
var (
CachePath string
RefetchDays int = 7
)
func MatchImagePkg(km config.KernelMask) (pkgs []string, err error) {
if CachePath == "" {
CachePath = config.File("debian.cache")
log.Debug().Msgf("Use default kernels cache path: %s", CachePath)
if !fs.PathExists(CachePath) {
log.Debug().Msgf("No cache, download")
err = cache.DownloadDebianCache(CachePath)
if err != nil {
log.Debug().Err(err).Msg(
"No remote cache, will take some time")
}
}
} 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)
kernels, err := GetKernels()
if err != nil {
log.Error().Err(err).Msg("get kernels")
return

View File

@ -7,7 +7,10 @@ import (
"github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/cache"
"code.dumpstack.io/tools/out-of-tree/config"
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
"code.dumpstack.io/tools/out-of-tree/fs"
)
type DebianKernelVersion struct {
@ -128,7 +131,35 @@ func GetCachedKernel(deb string) (dk DebianKernel, err error) {
return
}
func GetKernels(c *Cache, refetchDays int) (kernels []DebianKernel, err error) {
var (
CachePath string
RefetchDays int = 7
)
func GetKernels() (kernels []DebianKernel, err error) {
if CachePath == "" {
CachePath = config.File("debian.cache")
log.Debug().Msgf("Use default kernels cache path: %s", CachePath)
if !fs.PathExists(CachePath) {
log.Debug().Msgf("No cache, download")
err = cache.DownloadDebianCache(CachePath)
if err != nil {
log.Debug().Err(err).Msg(
"No remote cache, will take some time")
}
}
} 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()
versions, err := snapshot.SourcePackageVersions("linux")
if err != nil {
log.Error().Err(err).Msg("get source package versions")
@ -155,9 +186,9 @@ func GetKernels(c *Cache, refetchDays int) (kernels []DebianKernel, err error) {
}
if dk.Internal.Invalid {
refetch := dk.Internal.LastFetch.AddDate(0, 0, refetchDays)
refetch := dk.Internal.LastFetch.AddDate(0, 0, RefetchDays)
if refetch.After(time.Now()) {
slog.Debug().Msgf("refetch at %v", refetchDays)
slog.Debug().Msgf("refetch at %v", RefetchDays)
continue
}
}