1
0
out-of-tree/distro.go

102 lines
2.0 KiB
Go
Raw Normal View History

2023-05-11 20:08:08 +00:00
package main
import (
"os"
"path/filepath"
"regexp"
"github.com/cavaliergopher/grab/v3"
2023-05-11 20:08:08 +00:00
"github.com/rs/zerolog/log"
"code.dumpstack.io/tools/out-of-tree/distro/debian"
"code.dumpstack.io/tools/out-of-tree/distro/debian/snapshot"
"code.dumpstack.io/tools/out-of-tree/fs"
2023-05-11 20:08:08 +00:00
)
type DistroCmd struct {
Debian DebianCmd `cmd:""`
}
2023-05-11 20:08:08 +00:00
type DebianCmd struct {
Cache DebianCacheCmd `cmd:"" help:"populate cache"`
GetDeb DebianGetDebCmd `cmd:"" help:"download deb packages"`
2023-05-11 20:08:08 +00:00
}
type DebianCacheCmd struct {
Path string `help:"path to cache"`
2023-05-12 00:07:51 +00:00
Refetch int `help:"days before refetch versions without deb package" default:"7"`
2023-05-11 20:08:08 +00:00
}
func (cmd *DebianCacheCmd) Run() (err error) {
if cmd.Path != "" {
debian.CachePath = cmd.Path
}
2023-05-14 12:37:45 +00:00
debian.RefetchDays = cmd.Refetch
2023-05-11 20:08:08 +00:00
2023-05-12 15:00:50 +00:00
log.Info().Msg("Fetching kernels...")
2023-05-14 12:37:45 +00:00
_, err = debian.GetKernels()
2023-05-11 20:08:08 +00:00
if err != nil {
2023-05-12 15:00:50 +00:00
log.Error().Err(err).Msg("")
2023-05-11 20:08:08 +00:00
return
}
2023-05-12 15:00:50 +00:00
log.Info().Msg("Success")
2023-05-11 20:08:08 +00:00
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, err := os.MkdirTemp(cmd.Path, "tmp-")
if err != nil {
return
}
defer os.RemoveAll(tmp)
for _, pkg := range packages {
target := filepath.Join(cmd.Path, filepath.Base(pkg.Deb.URL))
if fs.PathExists(target) {
log.Info().Msgf("%s already exists", pkg.Deb.URL)
continue
}
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, target)
if err != nil {
log.Fatal().Err(err).Msg("mv")
}
}
return
}