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

     1  package checkpoint
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/cli/cli/command/formatter"
     9  	"github.com/docker/docker/api/types"
    10  	"github.com/spf13/cobra"
    11  )
    12  
    13  type listOptions struct {
    14  	checkpointDir string
    15  }
    16  
    17  func newListCommand(dockerCli command.Cli) *cobra.Command {
    18  	var opts listOptions
    19  
    20  	cmd := &cobra.Command{
    21  		Use:     "ls [OPTIONS] CONTAINER",
    22  		Aliases: []string{"list"},
    23  		Short:   "List checkpoints for a container",
    24  		Args:    cli.ExactArgs(1),
    25  		RunE: func(cmd *cobra.Command, args []string) error {
    26  			return runList(dockerCli, args[0], opts)
    27  		},
    28  	}
    29  
    30  	flags := cmd.Flags()
    31  	flags.StringVarP(&opts.checkpointDir, "checkpoint-dir", "", "", "Use a custom checkpoint storage directory")
    32  
    33  	return cmd
    34  
    35  }
    36  
    37  func runList(dockerCli command.Cli, container string, opts listOptions) error {
    38  	client := dockerCli.Client()
    39  
    40  	listOpts := types.CheckpointListOptions{
    41  		CheckpointDir: opts.checkpointDir,
    42  	}
    43  
    44  	checkpoints, err := client.CheckpointList(context.Background(), container, listOpts)
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	cpCtx := formatter.Context{
    50  		Output: dockerCli.Out(),
    51  		Format: NewFormat(formatter.TableFormatKey),
    52  	}
    53  	return FormatWrite(cpCtx, checkpoints)
    54  }