github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_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  	"github.com/containerd/nerdctl/pkg/api/types"
    21  	"github.com/containerd/nerdctl/pkg/clientutil"
    22  	"github.com/containerd/nerdctl/pkg/cmd/image"
    23  	"github.com/spf13/cobra"
    24  )
    25  
    26  func newImageInspectCommand() *cobra.Command {
    27  	var imageInspectCommand = &cobra.Command{
    28  		Use:               "inspect [flags] IMAGE [IMAGE...]",
    29  		Args:              cobra.MinimumNArgs(1),
    30  		Short:             "Display detailed information on one or more images.",
    31  		Long:              "Hint: set `--mode=native` for showing the full output",
    32  		RunE:              imageInspectAction,
    33  		ValidArgsFunction: imageInspectShellComplete,
    34  		SilenceUsage:      true,
    35  		SilenceErrors:     true,
    36  	}
    37  	imageInspectCommand.Flags().String("mode", "dockercompat", `Inspect mode, "dockercompat" for Docker-compatible output, "native" for containerd-native output`)
    38  	imageInspectCommand.RegisterFlagCompletionFunc("mode", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    39  		return []string{"dockercompat", "native"}, cobra.ShellCompDirectiveNoFileComp
    40  	})
    41  	imageInspectCommand.Flags().StringP("format", "f", "", "Format the output using the given Go template, e.g, '{{json .}}'")
    42  	imageInspectCommand.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    43  		return []string{"json"}, cobra.ShellCompDirectiveNoFileComp
    44  	})
    45  
    46  	// #region platform flags
    47  	imageInspectCommand.Flags().String("platform", "", "Inspect a specific platform") // not a slice, and there is no --all-platforms
    48  	imageInspectCommand.RegisterFlagCompletionFunc("platform", shellCompletePlatforms)
    49  	// #endregion
    50  
    51  	return imageInspectCommand
    52  }
    53  
    54  func processImageInspectOptions(cmd *cobra.Command, platform *string) (types.ImageInspectOptions, error) {
    55  	globalOptions, err := processRootCmdFlags(cmd)
    56  	if err != nil {
    57  		return types.ImageInspectOptions{}, err
    58  	}
    59  	mode, err := cmd.Flags().GetString("mode")
    60  	if err != nil {
    61  		return types.ImageInspectOptions{}, err
    62  	}
    63  	format, err := cmd.Flags().GetString("format")
    64  	if err != nil {
    65  		return types.ImageInspectOptions{}, err
    66  	}
    67  	if platform == nil {
    68  		tempPlatform, err := cmd.Flags().GetString("platform")
    69  		if err != nil {
    70  			return types.ImageInspectOptions{}, err
    71  		}
    72  		platform = &tempPlatform
    73  	}
    74  	return types.ImageInspectOptions{
    75  		GOptions: globalOptions,
    76  		Mode:     mode,
    77  		Format:   format,
    78  		Platform: *platform,
    79  		Stdout:   cmd.OutOrStdout(),
    80  	}, nil
    81  }
    82  
    83  func imageInspectAction(cmd *cobra.Command, args []string) error {
    84  	options, err := processImageInspectOptions(cmd, nil)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	client, ctx, cancel, err := clientutil.NewClientWithPlatform(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address, options.Platform)
    90  	if err != nil {
    91  		return err
    92  	}
    93  	defer cancel()
    94  
    95  	return image.Inspect(ctx, client, args, options)
    96  }
    97  
    98  func imageInspectShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    99  	// show image names
   100  	return shellCompleteImageNames(cmd)
   101  }