github.com/olljanat/moby@v1.13.1/cli/command/checkpoint/create.go (about)

     1  package checkpoint
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"golang.org/x/net/context"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/cli"
    10  	"github.com/docker/docker/cli/command"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  type createOptions struct {
    15  	container     string
    16  	checkpoint    string
    17  	checkpointDir string
    18  	leaveRunning  bool
    19  }
    20  
    21  func newCreateCommand(dockerCli *command.DockerCli) *cobra.Command {
    22  	var opts createOptions
    23  
    24  	cmd := &cobra.Command{
    25  		Use:   "create [OPTIONS] CONTAINER CHECKPOINT",
    26  		Short: "Create a checkpoint from a running container",
    27  		Args:  cli.ExactArgs(2),
    28  		RunE: func(cmd *cobra.Command, args []string) error {
    29  			opts.container = args[0]
    30  			opts.checkpoint = args[1]
    31  			return runCreate(dockerCli, opts)
    32  		},
    33  	}
    34  
    35  	flags := cmd.Flags()
    36  	flags.BoolVar(&opts.leaveRunning, "leave-running", false, "Leave the container running after checkpoint")
    37  	flags.StringVarP(&opts.checkpointDir, "checkpoint-dir", "", "", "Use a custom checkpoint storage directory")
    38  
    39  	return cmd
    40  }
    41  
    42  func runCreate(dockerCli *command.DockerCli, opts createOptions) error {
    43  	client := dockerCli.Client()
    44  
    45  	checkpointOpts := types.CheckpointCreateOptions{
    46  		CheckpointID:  opts.checkpoint,
    47  		CheckpointDir: opts.checkpointDir,
    48  		Exit:          !opts.leaveRunning,
    49  	}
    50  
    51  	err := client.CheckpointCreate(context.Background(), opts.container, checkpointOpts)
    52  	if err != nil {
    53  		return err
    54  	}
    55  
    56  	fmt.Fprintf(dockerCli.Out(), "%s\n", opts.checkpoint)
    57  	return nil
    58  }