|  | 
|  | 1 | +/* | 
|  | 2 | +   Copyright The containerd Authors. | 
|  | 3 | +
 | 
|  | 4 | +   Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | +   you may not use this file except in compliance with the License. | 
|  | 6 | +   You may obtain a copy of the License at | 
|  | 7 | +
 | 
|  | 8 | +       http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | +
 | 
|  | 10 | +   Unless required by applicable law or agreed to in writing, software | 
|  | 11 | +   distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | +   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | +   See the License for the specific language governing permissions and | 
|  | 14 | +   limitations under the License. | 
|  | 15 | +*/ | 
|  | 16 | + | 
|  | 17 | +package checkpoint | 
|  | 18 | + | 
|  | 19 | +import ( | 
|  | 20 | +	"fmt" | 
|  | 21 | +	"text/tabwriter" | 
|  | 22 | + | 
|  | 23 | +	"github.com/containerd/nerdctl/v2/cmd/nerdctl/completion" | 
|  | 24 | +	"github.com/containerd/nerdctl/v2/cmd/nerdctl/helpers" | 
|  | 25 | +	"github.com/containerd/nerdctl/v2/pkg/api/types" | 
|  | 26 | +	"github.com/containerd/nerdctl/v2/pkg/clientutil" | 
|  | 27 | +	"github.com/containerd/nerdctl/v2/pkg/cmd/checkpoint" | 
|  | 28 | +	"github.com/spf13/cobra" | 
|  | 29 | +) | 
|  | 30 | + | 
|  | 31 | +func ListCommand() *cobra.Command { | 
|  | 32 | +	var cmd = &cobra.Command{ | 
|  | 33 | +		Use:               "list [OPTIONS] CONTAINER", | 
|  | 34 | +		Short:             "List checkpoints for a container", | 
|  | 35 | +		Args:              cobra.ExactArgs(1), | 
|  | 36 | +		RunE:              listAction, | 
|  | 37 | +		ValidArgsFunction: listShellComplete, | 
|  | 38 | +		SilenceUsage:      true, | 
|  | 39 | +		SilenceErrors:     true, | 
|  | 40 | +	} | 
|  | 41 | +	cmd.Flags().String("checkpoint-dir", "", "Checkpoint directory") | 
|  | 42 | +	return cmd | 
|  | 43 | +} | 
|  | 44 | + | 
|  | 45 | +func processListFlags(cmd *cobra.Command) (types.CheckpointListOptions, error) { | 
|  | 46 | +	globalOptions, err := helpers.ProcessRootCmdFlags(cmd) | 
|  | 47 | +	if err != nil { | 
|  | 48 | +		return types.CheckpointListOptions{}, err | 
|  | 49 | +	} | 
|  | 50 | + | 
|  | 51 | +	checkpointDir, err := cmd.Flags().GetString("checkpoint-dir") | 
|  | 52 | +	if err != nil { | 
|  | 53 | +		return types.CheckpointListOptions{}, err | 
|  | 54 | +	} | 
|  | 55 | +	if checkpointDir == "" { | 
|  | 56 | +		checkpointDir = globalOptions.DataRoot + "/checkpoints" | 
|  | 57 | +	} | 
|  | 58 | + | 
|  | 59 | +	return types.CheckpointListOptions{ | 
|  | 60 | +		Stdout:        cmd.OutOrStdout(), | 
|  | 61 | +		GOptions:      globalOptions, | 
|  | 62 | +		CheckpointDir: checkpointDir, | 
|  | 63 | +	}, nil | 
|  | 64 | +} | 
|  | 65 | + | 
|  | 66 | +func listAction(cmd *cobra.Command, args []string) error { | 
|  | 67 | +	listOptions, err := processListFlags(cmd) | 
|  | 68 | +	if err != nil { | 
|  | 69 | +		return err | 
|  | 70 | +	} | 
|  | 71 | +	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), listOptions.GOptions.Namespace, listOptions.GOptions.Address) | 
|  | 72 | +	if err != nil { | 
|  | 73 | +		return err | 
|  | 74 | +	} | 
|  | 75 | +	defer cancel() | 
|  | 76 | + | 
|  | 77 | +	checkpoints, err := checkpoint.List(ctx, client, args[0], listOptions) | 
|  | 78 | +	if err != nil { | 
|  | 79 | +		return err | 
|  | 80 | +	} | 
|  | 81 | + | 
|  | 82 | +	w := tabwriter.NewWriter(listOptions.Stdout, 4, 8, 4, ' ', 0) | 
|  | 83 | +	fmt.Fprintln(w, "CHECKPOINT NAME") | 
|  | 84 | + | 
|  | 85 | +	for _, cp := range checkpoints { | 
|  | 86 | +		fmt.Fprintln(w, cp.Name) | 
|  | 87 | +	} | 
|  | 88 | + | 
|  | 89 | +	return w.Flush() | 
|  | 90 | +} | 
|  | 91 | + | 
|  | 92 | +func listShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | 
|  | 93 | +	return completion.ImageNames(cmd) | 
|  | 94 | +} | 
0 commit comments