github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/container_inspect.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 "fmt" 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 26 "github.com/spf13/cobra" 27 ) 28 29 func newContainerInspectCommand() *cobra.Command { 30 var containerInspectCommand = &cobra.Command{ 31 Use: "inspect [flags] CONTAINER [CONTAINER, ...]", 32 Short: "Display detailed information on one or more containers.", 33 Long: "Hint: set `--mode=native` for showing the full output", 34 Args: cobra.MinimumNArgs(1), 35 RunE: containerInspectAction, 36 ValidArgsFunction: containerInspectShellComplete, 37 SilenceUsage: true, 38 SilenceErrors: true, 39 } 40 containerInspectCommand.Flags().String("mode", "dockercompat", `Inspect mode, "dockercompat" for Docker-compatible output, "native" for containerd-native output`) 41 containerInspectCommand.RegisterFlagCompletionFunc("mode", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 42 return []string{"dockercompat", "native"}, cobra.ShellCompDirectiveNoFileComp 43 }) 44 containerInspectCommand.Flags().StringP("format", "f", "", "Format the output using the given Go template, e.g, '{{json .}}'") 45 containerInspectCommand.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 46 return []string{"json"}, cobra.ShellCompDirectiveNoFileComp 47 }) 48 return containerInspectCommand 49 } 50 51 var validModeType = map[string]bool{ 52 "native": true, 53 "dockercompat": true, 54 } 55 56 func processContainerInspectOptions(cmd *cobra.Command) (opt types.ContainerInspectOptions, err error) { 57 globalOptions, err := processRootCmdFlags(cmd) 58 if err != nil { 59 return 60 } 61 mode, err := cmd.Flags().GetString("mode") 62 if err != nil { 63 return 64 } 65 if len(mode) > 0 && !validModeType[mode] { 66 err = fmt.Errorf("%q is not a valid value for --mode", mode) 67 return 68 } 69 format, err := cmd.Flags().GetString("format") 70 if err != nil { 71 return 72 } 73 74 return types.ContainerInspectOptions{ 75 GOptions: globalOptions, 76 Format: format, 77 Mode: mode, 78 Stdout: cmd.OutOrStdout(), 79 }, nil 80 } 81 82 func containerInspectAction(cmd *cobra.Command, args []string) error { 83 opt, err := processContainerInspectOptions(cmd) 84 if err != nil { 85 return err 86 } 87 client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), opt.GOptions.Namespace, opt.GOptions.Address) 88 if err != nil { 89 return err 90 } 91 defer cancel() 92 93 return container.Inspect(ctx, client, args, opt) 94 } 95 96 func containerInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { 97 // show container names 98 return shellCompleteContainerNames(cmd, nil) 99 }