[feat] add rm option
This commit is contained in:
parent
1988ed6762
commit
7a23303a1b
@ -16,6 +16,7 @@ func main() {
|
|||||||
var imageName string
|
var imageName string
|
||||||
var tarName string
|
var tarName string
|
||||||
var doPull bool
|
var doPull bool
|
||||||
|
var rm bool
|
||||||
|
|
||||||
rootCmd = &cobra.Command{
|
rootCmd = &cobra.Command{
|
||||||
Use: "docker-save",
|
Use: "docker-save",
|
||||||
@ -27,7 +28,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
if err := docker.SaveImageTar(ctx, imageName, outputPath, tarName, doPull); err != nil {
|
if err := docker.SaveImageTar(ctx, imageName, outputPath, tarName, doPull, rm); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -36,6 +37,7 @@ func main() {
|
|||||||
rootCmd.Flags().StringVarP(&imageName, "image", "i", "", "image name")
|
rootCmd.Flags().StringVarP(&imageName, "image", "i", "", "image name")
|
||||||
rootCmd.Flags().StringVarP(&tarName, "name", "n", "", "tar name")
|
rootCmd.Flags().StringVarP(&tarName, "name", "n", "", "tar name")
|
||||||
rootCmd.Flags().BoolVarP(&doPull, "pull", "p", false, "pull image")
|
rootCmd.Flags().BoolVarP(&doPull, "pull", "p", false, "pull image")
|
||||||
|
rootCmd.Flags().BoolVarP(&rm, "rm", "", false, "delete image after save")
|
||||||
rootCmd.MarkFlagRequired("image")
|
rootCmd.MarkFlagRequired("image")
|
||||||
rootCmd.MarkFlagRequired("output")
|
rootCmd.MarkFlagRequired("output")
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
"github.com/docker/docker/client"
|
"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())
|
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
|
||||||
if err != nil {
|
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)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -46,6 +52,15 @@ func pullImage(ctx context.Context, cli *client.Client, image string) error {
|
|||||||
return nil
|
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 {
|
func saveToTar(ctx context.Context, cli *client.Client, image, path, name string) error {
|
||||||
out, err := cli.ImageSave(ctx, []string{image})
|
out, err := cli.ImageSave(ctx, []string{image})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user