From 689bf1098acba1bd3953e7eeb5b6a9896a76783e Mon Sep 17 00:00:00 2001 From: Mikhail Klementev Date: Mon, 15 May 2023 11:35:15 +0000 Subject: [PATCH] feat: command to download debian packages --- distro.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 57 insertions(+), 1 deletion(-) diff --git a/distro.go b/distro.go index 024efdc..a910947 100644 --- a/distro.go +++ b/distro.go @@ -1,9 +1,15 @@ package main import ( + "os" + "path/filepath" + "regexp" + + "github.com/cavaliergopher/grab/v3" "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 DistroCmd struct { @@ -11,7 +17,8 @@ type DistroCmd struct { } type DebianCmd struct { - Cache DebianCacheCmd `cmd:"" help:"populate cache"` + Cache DebianCacheCmd `cmd:"" help:"populate cache"` + GetDeb DebianGetDebCmd `cmd:"" help:"download deb packages"` } type DebianCacheCmd struct { @@ -36,3 +43,52 @@ func (cmd *DebianCacheCmd) Run() (err error) { log.Info().Msg("Success") return } + +type DebianGetDebCmd struct { + Path string `help:"path to download directory" type:"existingdir" default:"./"` + Regexp string `help:"match deb pkg names by regexp" default:".*"` +} + +func (cmd DebianGetDebCmd) Run() (err error) { + kernels, err := debian.GetKernels() + if err != nil { + log.Error().Err(err).Msg("") + return + } + + re := regexp.MustCompile(cmd.Regexp) + + var packages []snapshot.Package + for _, kernel := range kernels { + for _, pkg := range kernel.Packages() { + if !re.MatchString(pkg.Deb.Name) { + continue + } + packages = append(packages, pkg) + } + } + + tmp := filepath.Join(cmd.Path, "tmp") + err = os.MkdirAll(tmp, os.ModePerm) + if err != nil { + return + } + defer os.RemoveAll(tmp) + + for _, pkg := range packages { + log.Info().Msgf("downloading %s", pkg.Deb.URL) + + resp, err := grab.Get(tmp, pkg.Deb.URL) + if err != nil { + log.Warn().Err(err).Msg("download") + continue + } + + err = os.Rename(resp.Filename, filepath.Base(resp.Filename)) + if err != nil { + log.Fatal().Err(err).Msg("mv") + } + } + + return +}