feat: save container images
This commit is contained in:
@ -7,6 +7,7 @@ package cmd
|
||||
import (
|
||||
"fmt"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
@ -18,6 +19,7 @@ type ContainerCmd struct {
|
||||
Filter string `help:"filter by name"`
|
||||
|
||||
List ContainerListCmd `cmd:"" help:"list containers"`
|
||||
Save ContainerSaveCmd `cmd:"" help:"save containers"`
|
||||
Cleanup ContainerCleanupCmd `cmd:"" help:"cleanup containers"`
|
||||
}
|
||||
|
||||
@ -45,6 +47,37 @@ func (cmd ContainerListCmd) Run(containerCmd *ContainerCmd) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
type ContainerSaveCmd struct {
|
||||
OutDir string `help:"directory to save containers" default:"./" type:"existingdir"`
|
||||
}
|
||||
|
||||
func (cmd ContainerSaveCmd) Run(containerCmd *ContainerCmd) (err error) {
|
||||
for _, name := range containerCmd.Containers() {
|
||||
nlog := log.With().Str("name", name).Logger()
|
||||
|
||||
output := filepath.Join(cmd.OutDir, name+".tar")
|
||||
nlog.Info().Msgf("saving to %v")
|
||||
|
||||
err = container.Save(name, output)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
compressed := output + ".gz"
|
||||
nlog.Info().Msgf("compressing to %v", compressed)
|
||||
|
||||
var raw []byte
|
||||
raw, err = exec.Command("gzip", output).CombinedOutput()
|
||||
if err != nil {
|
||||
nlog.Error().Err(err).Msg(string(raw))
|
||||
return
|
||||
}
|
||||
|
||||
nlog.Info().Msg("done")
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type ContainerCleanupCmd struct{}
|
||||
|
||||
func (cmd ContainerCleanupCmd) Run(containerCmd *ContainerCmd) (err error) {
|
||||
|
Reference in New Issue
Block a user