[feat] add rm option
This commit is contained in:
@@ -12,7 +12,7 @@ import (
|
||||
"github.com/docker/docker/client"
|
||||
)
|
||||
|
||||
func SaveImageTar(ctx context.Context, image, path, name string, doPull bool) error {
|
||||
func SaveImageTar(ctx context.Context, image, path, name string, doPull, rm bool) error {
|
||||
|
||||
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
||||
if err != nil {
|
||||
@@ -30,6 +30,12 @@ func SaveImageTar(ctx context.Context, image, path, name string, doPull bool) er
|
||||
return fmt.Errorf("save image %s failed: %v", image, err)
|
||||
}
|
||||
|
||||
if rm {
|
||||
if err := rmImage(ctx, cli, image); err != nil {
|
||||
return fmt.Errorf("remove image %s failed: %v", image, err)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -46,6 +52,15 @@ func pullImage(ctx context.Context, cli *client.Client, image string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func rmImage(ctx context.Context, cli *client.Client, image string) error {
|
||||
_, err := cli.ImageRemove(ctx, image, types.ImageRemoveOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func saveToTar(ctx context.Context, cli *client.Client, image, path, name string) error {
|
||||
out, err := cli.ImageSave(ctx, []string{image})
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user