github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/compose_stop.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/nerdctl/pkg/clientutil"
    21  	"github.com/containerd/nerdctl/pkg/cmd/compose"
    22  	"github.com/containerd/nerdctl/pkg/composer"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func newComposeStopCommand() *cobra.Command {
    27  	var composeStopCommand = &cobra.Command{
    28  		Use:           "stop [flags] [SERVICE...]",
    29  		Short:         "Stop running containers without removing them.",
    30  		RunE:          composeStopAction,
    31  		SilenceUsage:  true,
    32  		SilenceErrors: true,
    33  	}
    34  	composeStopCommand.Flags().UintP("timeout", "t", 10, "Seconds to wait for stop before killing them")
    35  	return composeStopCommand
    36  }
    37  
    38  func composeStopAction(cmd *cobra.Command, args []string) error {
    39  	globalOptions, err := processRootCmdFlags(cmd)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	var opt composer.StopOptions
    44  
    45  	if cmd.Flags().Changed("timeout") {
    46  		timeValue, err := cmd.Flags().GetUint("timeout")
    47  		if err != nil {
    48  			return err
    49  		}
    50  		opt.Timeout = &timeValue
    51  	}
    52  
    53  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), globalOptions.Namespace, globalOptions.Address)
    54  	if err != nil {
    55  		return err
    56  	}
    57  	defer cancel()
    58  	options, err := getComposeOptions(cmd, globalOptions.DebugFull, globalOptions.Experimental)
    59  	if err != nil {
    60  		return err
    61  	}
    62  	c, err := compose.New(client, globalOptions, options, cmd.OutOrStdout(), cmd.ErrOrStderr())
    63  	if err != nil {
    64  		return err
    65  	}
    66  	return c.Stop(ctx, opt, args)
    67  }