[feat] add pull argument

This commit is contained in:
jay 2022-06-14 13:15:10 +08:00
parent b351de0f64
commit 1988ed6762
3 changed files with 14 additions and 4 deletions

6
Makefile Normal file
View File

@ -0,0 +1,6 @@
.PHONY: build
BIN_NAME := "docker-save"
build:
@go build -v -o $(BIN_NAME) ./cmd/main.go

View File

@ -15,6 +15,7 @@ func main() {
var outputPath string
var imageName string
var tarName string
var doPull bool
rootCmd = &cobra.Command{
Use: "docker-save",
@ -26,7 +27,7 @@ func main() {
}
ctx := context.Background()
if err := docker.SaveImageTar(ctx, imageName, outputPath, tarName); err != nil {
if err := docker.SaveImageTar(ctx, imageName, outputPath, tarName, doPull); err != nil {
log.Fatal(err)
}
},
@ -34,6 +35,7 @@ func main() {
rootCmd.Flags().StringVarP(&outputPath, "output", "o", "", "output path")
rootCmd.Flags().StringVarP(&imageName, "image", "i", "", "image name")
rootCmd.Flags().StringVarP(&tarName, "name", "n", "", "tar name")
rootCmd.Flags().BoolVarP(&doPull, "pull", "p", false, "pull image")
rootCmd.MarkFlagRequired("image")
rootCmd.MarkFlagRequired("output")

View File

@ -12,7 +12,7 @@ import (
"github.com/docker/docker/client"
)
func SaveImageTar(ctx context.Context, image, path, name string) error {
func SaveImageTar(ctx context.Context, image, path, name string, doPull bool) error {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
@ -20,8 +20,10 @@ func SaveImageTar(ctx context.Context, image, path, name string) error {
}
defer cli.Close()
if err := pullImage(ctx, cli, image); err != nil {
return fmt.Errorf("pull image %s failed: %v", image, err)
if doPull {
if err := pullImage(ctx, cli, image); err != nil {
return fmt.Errorf("pull image %s failed: %v", image, err)
}
}
if err := saveToTar(ctx, cli, image, path, name); err != nil {