diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6a971e6 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +.PHONY: build + +BIN_NAME := "docker-save" + +build: + @go build -v -o $(BIN_NAME) ./cmd/main.go diff --git a/cmd/main.go b/cmd/main.go index 56668c6..e39974a 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -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") diff --git a/internal/docker/docker.go b/internal/docker/docker.go index f78de2d..a427159 100644 --- a/internal/docker/docker.go +++ b/internal/docker/docker.go @@ -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 {