github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_start.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/containerd/nerdctl/pkg/consoleutil" 25 "github.com/spf13/cobra" 26 ) 27 28 func newStartCommand() *cobra.Command { 29 var startCommand = &cobra.Command{ 30 Use: "start [flags] CONTAINER [CONTAINER, ...]", 31 Args: cobra.MinimumNArgs(1), 32 Short: "Start one or more running containers", 33 RunE: startAction, 34 ValidArgsFunction: startShellComplete, 35 SilenceUsage: true, 36 SilenceErrors: true, 37 } 38 39 startCommand.Flags().SetInterspersed(false) 40 startCommand.Flags().BoolP("attach", "a", false, "Attach STDOUT/STDERR and forward signals") 41 startCommand.Flags().String("detach-keys", consoleutil.DefaultDetachKeys, "Override the default detach keys") 42 43 return startCommand 44 } 45 46 func processContainerStartOptions(cmd *cobra.Command) (types.ContainerStartOptions, error) { 47 globalOptions, err := processRootCmdFlags(cmd) 48 if err != nil { 49 return types.ContainerStartOptions{}, err 50 } 51 attach, err := cmd.Flags().GetBool("attach") 52 if err != nil { 53 return types.ContainerStartOptions{}, err 54 } 55 detachKeys, err := cmd.Flags().GetString("detach-keys") 56 if err != nil { 57 return types.ContainerStartOptions{}, err 58 } 59 return types.ContainerStartOptions{ 60 Stdout: cmd.OutOrStdout(), 61 GOptions: globalOptions, 62 Attach: attach, 63 DetachKeys: detachKeys, 64 }, nil 65 } 66 67 func startAction(cmd *cobra.Command, args []string) error { 68 options, err := processContainerStartOptions(cmd) 69 if err != nil { 70 return err 71 } 72 73 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address) 74 if err != nil { 75 return err 76 } 77 defer cancel() 78 79 return container.Start(ctx, client, args, options) 80 } 81 82 func startShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 83 // show non-running container names 84 statusFilterFn := func(st containerd.ProcessStatus) bool { 85 return st != containerd.Running && st != containerd.Unknown 86 } 87 return shellCompleteContainerNames(cmd, statusFilterFn) 88 }