1
0

feat: command to download debian packages

This commit is contained in:
dump_stack() 2023-05-15 11:35:15 +00:00
parent eda23b45b9
commit 689bf1098a
Signed by: dump_stack
GPG Key ID: BE44DA8C062D87DC

View File

@ -1,9 +1,15 @@
package main package main
import ( import (
"os"
"path/filepath"
"regexp"
"github.com/cavaliergopher/grab/v3"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/distro/debian" "code.dumpstack.io/tools/out-of-tree/distro/debian"
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
) )
type DistroCmd struct { type DistroCmd struct {
@ -12,6 +18,7 @@ type DistroCmd struct {
type DebianCmd 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 { type DebianCacheCmd struct {
@ -36,3 +43,52 @@ func (cmd *DebianCacheCmd) Run() (err error) {
log.Info().Msg("Success") log.Info().Msg("Success")
return 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
}