github.com/containerd/nerdctl@v1.7.7/cmd/nerdctl/image_list.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/image"
    25  	"github.com/containerd/nerdctl/pkg/referenceutil"
    26  	"github.com/spf13/cobra"
    27  )
    28  
    29  func newImagesCommand() *cobra.Command {
    30  	shortHelp := "List images"
    31  	longHelp := shortHelp + `
    32  
    33  Properties:
    34  - REPOSITORY: Repository
    35  - TAG:        Tag
    36  - NAME:       Name of the image, --names for skip parsing as repository and tag.
    37  - IMAGE ID:   OCI Digest. Usually different from Docker image ID. Shared for multi-platform images.
    38  - CREATED:    Created time
    39  - PLATFORM:   Platform
    40  - SIZE:       Size of the unpacked snapshots
    41  - BLOB SIZE:  Size of the blobs (such as layer tarballs) in the content store
    42  `
    43  	var imagesCommand = &cobra.Command{
    44  		Use:                   "images [flags] [REPOSITORY[:TAG]]",
    45  		Short:                 shortHelp,
    46  		Long:                  longHelp,
    47  		Args:                  cobra.MaximumNArgs(1),
    48  		RunE:                  imagesAction,
    49  		ValidArgsFunction:     imagesShellComplete,
    50  		SilenceUsage:          true,
    51  		SilenceErrors:         true,
    52  		DisableFlagsInUseLine: true,
    53  	}
    54  
    55  	imagesCommand.Flags().BoolP("quiet", "q", false, "Only show numeric IDs")
    56  	imagesCommand.Flags().Bool("no-trunc", false, "Don't truncate output")
    57  	// Alias "-f" is reserved for "--filter"
    58  	imagesCommand.Flags().String("format", "", "Format the output using the given Go template, e.g, '{{json .}}', 'wide'")
    59  	imagesCommand.Flags().StringSliceP("filter", "f", []string{}, "Filter output based on conditions provided")
    60  	imagesCommand.RegisterFlagCompletionFunc("format", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
    61  		return []string{"json", "table", "wide"}, cobra.ShellCompDirectiveNoFileComp
    62  	})
    63  	imagesCommand.Flags().Bool("digests", false, "Show digests (compatible with Docker, unlike ID)")
    64  	imagesCommand.Flags().Bool("names", false, "Show image names")
    65  	imagesCommand.Flags().BoolP("all", "a", true, "(unimplemented yet, always true)")
    66  
    67  	return imagesCommand
    68  }
    69  
    70  func processImageListOptions(cmd *cobra.Command, args []string) (types.ImageListOptions, error) {
    71  	globalOptions, err := processRootCmdFlags(cmd)
    72  	if err != nil {
    73  		return types.ImageListOptions{}, err
    74  	}
    75  	var filters []string
    76  
    77  	if len(args) > 0 {
    78  		canonicalRef, err := referenceutil.ParseAny(args[0])
    79  		if err != nil {
    80  			return types.ImageListOptions{}, err
    81  		}
    82  		filters = append(filters, fmt.Sprintf("name==%s", canonicalRef.String()))
    83  		filters = append(filters, fmt.Sprintf("name==%s", args[0]))
    84  	}
    85  	quiet, err := cmd.Flags().GetBool("quiet")
    86  	if err != nil {
    87  		return types.ImageListOptions{}, err
    88  	}
    89  	noTrunc, err := cmd.Flags().GetBool("no-trunc")
    90  	if err != nil {
    91  		return types.ImageListOptions{}, err
    92  	}
    93  	format, err := cmd.Flags().GetString("format")
    94  	if err != nil {
    95  		return types.ImageListOptions{}, err
    96  	}
    97  	var inputFilters []string
    98  	if cmd.Flags().Changed("filter") {
    99  		inputFilters, err = cmd.Flags().GetStringSlice("filter")
   100  		if err != nil {
   101  			return types.ImageListOptions{}, err
   102  		}
   103  	}
   104  	digests, err := cmd.Flags().GetBool("digests")
   105  	if err != nil {
   106  		return types.ImageListOptions{}, err
   107  	}
   108  	names, err := cmd.Flags().GetBool("names")
   109  	if err != nil {
   110  		return types.ImageListOptions{}, err
   111  	}
   112  	return types.ImageListOptions{
   113  		GOptions:         globalOptions,
   114  		Quiet:            quiet,
   115  		NoTrunc:          noTrunc,
   116  		Format:           format,
   117  		Filters:          inputFilters,
   118  		NameAndRefFilter: filters,
   119  		Digests:          digests,
   120  		Names:            names,
   121  		All:              true,
   122  		Stdout:           cmd.OutOrStdout(),
   123  	}, nil
   124  
   125  }
   126  
   127  func imagesAction(cmd *cobra.Command, args []string) error {
   128  	options, err := processImageListOptions(cmd, args)
   129  	if err != nil {
   130  		return err
   131  	}
   132  	if !options.All {
   133  		options.All = true
   134  	}
   135  
   136  	client, ctx, cancel, err := clientutil.NewClient(cmd.Context(), options.GOptions.Namespace, options.GOptions.Address)
   137  	if err != nil {
   138  		return err
   139  	}
   140  	defer cancel()
   141  
   142  	return image.ListCommandHandler(ctx, client, options)
   143  }
   144  
   145  func imagesShellComplete(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
   146  	if len(args) == 0 {
   147  		// show image names
   148  		return shellCompleteImageNames(cmd)
   149  	}
   150  	return nil, cobra.ShellCompDirectiveNoFileComp
   151  }