github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/client/save.go (about)

     1  package client
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  
     7  	"golang.org/x/net/context"
     8  
     9  	Cli "github.com/docker/docker/cli"
    10  	flag "github.com/docker/docker/pkg/mflag"
    11  )
    12  
    13  // CmdSave saves one or more images to a tar archive.
    14  //
    15  // The tar archive is written to STDOUT by default, or written to a file.
    16  //
    17  // Usage: docker save [OPTIONS] IMAGE [IMAGE...]
    18  func (cli *DockerCli) CmdSave(args ...string) error {
    19  	cmd := Cli.Subcmd("save", []string{"IMAGE [IMAGE...]"}, Cli.DockerCommands["save"].Description+" (streamed to STDOUT by default)", true)
    20  	outfile := cmd.String([]string{"o", "-output"}, "", "Write to a file, instead of STDOUT")
    21  	cmd.Require(flag.Min, 1)
    22  
    23  	cmd.ParseFlags(args, true)
    24  
    25  	if *outfile == "" && cli.isTerminalOut {
    26  		return errors.New("Cowardly refusing to save to a terminal. Use the -o flag or redirect.")
    27  	}
    28  
    29  	responseBody, err := cli.client.ImageSave(context.Background(), cmd.Args())
    30  	if err != nil {
    31  		return err
    32  	}
    33  	defer responseBody.Close()
    34  
    35  	if *outfile == "" {
    36  		_, err := io.Copy(cli.out, responseBody)
    37  		return err
    38  	}
    39  
    40  	return copyToFile(*outfile, responseBody)
    41  
    42  }