github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_pause.go (about)

     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 main
    18  
    19  import (
    20  	"github.com/containerd/containerd"
    21  	"github.com/containerd/nerdctl/pkg/api/types"
    22  	"github.com/containerd/nerdctl/pkg/clientutil"
    23  	"github.com/containerd/nerdctl/pkg/cmd/container"
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  func newPauseCommand() *cobra.Command {
    28  	var pauseCommand = &cobra.Command{
    29  		Use:               "pause [flags] CONTAINER [CONTAINER, ...]",
    30  		Args:              cobra.MinimumNArgs(1),
    31  		Short:             "Pause all processes within one or more containers",
    32  		RunE:              pauseAction,
    33  		ValidArgsFunction: pauseShellComplete,
    34  		SilenceUsage:      true,
    35  		SilenceErrors:     true,
    36  	}
    37  	return pauseCommand
    38  }
    39  
    40  func processContainerPauseOptions(cmd *cobra.Command) (types.ContainerPauseOptions, error) {
    41  	globalOptions, err := processRootCmdFlags(cmd)
    42  	if err != nil {
    43  		return types.ContainerPauseOptions{}, err
    44  	}
    45  	return types.ContainerPauseOptions{
    46  		GOptions: globalOptions,
    47  		Stdout:   cmd.OutOrStdout(),
    48  	}, nil
    49  }
    50  
    51  func pauseAction(cmd *cobra.Command, args []string) error {
    52  	options, err := processContainerPauseOptions(cmd)
    53  	if err != nil {
    54  		return err
    55  	}
    56  
    57  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
    58  	if err != nil {
    59  		return err
    60  	}
    61  	defer cancel()
    62  
    63  	return container.Pause(ctx, client, args, options)
    64  }
    65  
    66  func pauseShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    67  	// show running container names
    68  	statusFilterFn := func(st containerd.ProcessStatus) bool {
    69  		return st == containerd.Running
    70  	}
    71  	return shellCompleteContainerNames(cmd, statusFilterFn)
    72  }