github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_restart.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  	"time"
    21  
    22  	"github.com/containerd/nerdctl/pkg/api/types"
    23  	"github.com/containerd/nerdctl/pkg/clientutil"
    24  	"github.com/containerd/nerdctl/pkg/cmd/container"
    25  	"github.com/spf13/cobra"
    26  )
    27  
    28  func newRestartCommand() *cobra.Command {
    29  	var restartCommand = &cobra.Command{
    30  		Use:               "restart [flags] CONTAINER [CONTAINER, ...]",
    31  		Args:              cobra.MinimumNArgs(1),
    32  		Short:             "Restart one or more running containers",
    33  		RunE:              restartAction,
    34  		ValidArgsFunction: startShellComplete,
    35  		SilenceUsage:      true,
    36  		SilenceErrors:     true,
    37  	}
    38  	restartCommand.Flags().UintP("time", "t", 10, "Seconds to wait for stop before killing it")
    39  	return restartCommand
    40  }
    41  
    42  func processContainerRestartOptions(cmd *cobra.Command) (types.ContainerRestartOptions, error) {
    43  	globalOptions, err := processRootCmdFlags(cmd)
    44  	if err != nil {
    45  		return types.ContainerRestartOptions{}, err
    46  	}
    47  
    48  	var timeout *time.Duration
    49  	if cmd.Flags().Changed("time") {
    50  		// Seconds to wait for stop before killing it
    51  		timeValue, err := cmd.Flags().GetUint("time")
    52  		if err != nil {
    53  			return types.ContainerRestartOptions{}, err
    54  		}
    55  		t := time.Duration(timeValue) * time.Second
    56  		timeout = &t
    57  	}
    58  
    59  	return types.ContainerRestartOptions{
    60  		Stdout:  cmd.OutOrStdout(),
    61  		GOption: globalOptions,
    62  		Timeout: timeout,
    63  	}, err
    64  }
    65  
    66  func restartAction(cmd *cobra.Command, args []string) error {
    67  	options, err := processContainerRestartOptions(cmd)
    68  	if err != nil {
    69  		return err
    70  	}
    71  
    72  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOption.Namespace, options.GOption.Address)
    73  	if err != nil {
    74  		return err
    75  	}
    76  	defer cancel()
    77  
    78  	return container.Restart(ctx, client, args, options)
    79  }