diff --git a/debian.go b/debian.go index 15b7213..03a8629 100644 --- a/debian.go +++ b/debian.go @@ -1,12 +1,9 @@ package main import ( - "time" - "github.com/rs/zerolog/log" "code.dumpstack.io/tools/out-of-tree/distro/debian" - "code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot" ) type DebianCmd struct { @@ -26,52 +23,14 @@ func (cmd *DebianCacheCmd) Run() (err error) { } defer c.Close() - versions, err := snapshot.SourcePackageVersions("linux") + log.Info().Msg("Fetching kernels...") + + _, err = debian.GetKernels(c, cmd.Refetch) if err != nil { - log.Error().Err(err).Msg("get source package versions") + log.Error().Err(err).Msg("") return } - for i, version := range versions { - slog := log.With().Str("version", version).Logger() - slog.Info().Msgf("%03d/%03d", i, len(versions)) - - var dk debian.DebianKernel - - dk, err = c.Get(version) - if err == nil && !dk.Internal.Invalid { - slog.Info().Msgf("found in cache") - continue - } - - if dk.Internal.Invalid { - refetch := dk.Internal.LastFetch.AddDate(0, 0, cmd.Refetch) - if refetch.After(time.Now()) { - slog.Info().Msgf("refetch at %v", refetch) - continue - } - } - - dk, err = debian.GetDebianKernel(version) - if err != nil { - if err == debian.ErrNoBinaryPackages { - slog.Warn().Err(err).Msg("") - } else { - slog.Error().Err(err).Msg("get debian kernel") - } - - dk.Internal.Invalid = true - dk.Internal.LastFetch = time.Now() - } - - err = c.Put(dk) - if err != nil { - slog.Error().Err(err).Msg("put to cache") - return - } - - slog.Info().Msgf("%s cached", version) - } - + log.Info().Msg("Success") return } diff --git a/distro/debian/kernel.go b/distro/debian/kernel.go index a25360a..e083339 100644 --- a/distro/debian/kernel.go +++ b/distro/debian/kernel.go @@ -6,6 +6,8 @@ import ( "strings" "time" + "github.com/rs/zerolog/log" + "code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot" ) @@ -100,3 +102,55 @@ func GetDebianKernel(version string) (dk DebianKernel, err error) { return } + +func GetKernels(c *Cache, refetchDays int) (kernels []DebianKernel, err error) { + versions, err := snapshot.SourcePackageVersions("linux") + if err != nil { + log.Error().Err(err).Msg("get source package versions") + return + } + + for i, version := range versions { + slog := log.With().Str("version", version).Logger() + slog.Debug().Msgf("%03d/%03d", i, len(versions)) + + var dk DebianKernel + + dk, err = c.Get(version) + if err == nil && !dk.Internal.Invalid { + slog.Debug().Msgf("found in cache") + kernels = append(kernels, dk) + continue + } + + if dk.Internal.Invalid { + refetch := dk.Internal.LastFetch.AddDate(0, 0, refetchDays) + if refetch.After(time.Now()) { + slog.Debug().Msgf("refetch at %v", refetchDays) + continue + } + } + + dk, err = GetDebianKernel(version) + if err != nil { + if err == ErrNoBinaryPackages { + slog.Warn().Err(err).Msg("") + } else { + slog.Error().Err(err).Msg("get debian kernel") + } + + dk.Internal.Invalid = true + dk.Internal.LastFetch = time.Now() + } + + err = c.Put(dk) + if err != nil { + slog.Error().Err(err).Msg("put to cache") + return + } + + slog.Debug().Msgf("%s cached", version) + } + + return +}