mirror of https://github.com/docker/cli.git
73 lines
1.7 KiB
Go
73 lines
1.7 KiB
Go
package image
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/docker/cli/cli"
|
|
"github.com/docker/cli/cli/command"
|
|
"github.com/pkg/errors"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type saveOptions struct {
|
|
images []string
|
|
output string
|
|
}
|
|
|
|
// NewSaveCommand creates a new `docker save` command
|
|
func NewSaveCommand(dockerCli command.Cli) *cobra.Command {
|
|
var opts saveOptions
|
|
|
|
cmd := &cobra.Command{
|
|
Use: "save [OPTIONS] IMAGE [IMAGE...]",
|
|
Short: "Save one or more images to a tar archive (streamed to STDOUT by default)",
|
|
Args: cli.RequiresMinArgs(1),
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
opts.images = args
|
|
return runSave(dockerCli, opts)
|
|
},
|
|
}
|
|
|
|
flags := cmd.Flags()
|
|
|
|
flags.StringVarP(&opts.output, "output", "o", "", "Write to a file, instead of STDOUT")
|
|
|
|
return cmd
|
|
}
|
|
|
|
func runSave(dockerCli command.Cli, opts saveOptions) error {
|
|
if opts.output == "" && dockerCli.Out().IsTerminal() {
|
|
return errors.New("cowardly refusing to save to a terminal. Use the -o flag or redirect")
|
|
}
|
|
|
|
if err := validateOutputPath(opts.output); err != nil {
|
|
return errors.Wrap(err, "failed to save image")
|
|
}
|
|
|
|
responseBody, err := dockerCli.Client().ImageSave(context.Background(), opts.images)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer responseBody.Close()
|
|
|
|
if opts.output == "" {
|
|
_, err := io.Copy(dockerCli.Out(), responseBody)
|
|
return err
|
|
}
|
|
|
|
return command.CopyToFile(opts.output, responseBody)
|
|
}
|
|
|
|
func validateOutputPath(path string) error {
|
|
dir := filepath.Dir(path)
|
|
if dir != "" && dir != "." {
|
|
if _, err := os.Stat(dir); os.IsNotExist(err) {
|
|
return errors.Errorf("unable to validate output path: directory %q does not exist", dir)
|
|
}
|
|
}
|
|
return nil
|
|
}
|