github.com/xeptore/docker-cli@v20.10.14+incompatible/cli/command/checkpoint/create.go (about)

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