github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/api/client/images.go (about)

     1  package client
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  
     6  	"github.com/docker/docker/api/client/formatter"
     7  	Cli "github.com/docker/docker/cli"
     8  	"github.com/docker/docker/opts"
     9  	flag "github.com/docker/docker/pkg/mflag"
    10  	"github.com/docker/engine-api/types"
    11  	"github.com/docker/engine-api/types/filters"
    12  )
    13  
    14  // CmdImages lists the images in a specified repository, or all top-level images if no repository is specified.
    15  //
    16  // Usage: docker images [OPTIONS] [REPOSITORY]
    17  func (cli *DockerCli) CmdImages(args ...string) error {
    18  	cmd := Cli.Subcmd("images", []string{"[REPOSITORY[:TAG]]"}, Cli.DockerCommands["images"].Description, true)
    19  	quiet := cmd.Bool([]string{"q", "-quiet"}, false, "Only show numeric IDs")
    20  	all := cmd.Bool([]string{"a", "-all"}, false, "Show all images (default hides intermediate images)")
    21  	noTrunc := cmd.Bool([]string{"-no-trunc"}, false, "Don't truncate output")
    22  	showDigests := cmd.Bool([]string{"-digests"}, false, "Show digests")
    23  	format := cmd.String([]string{"-format"}, "", "Pretty-print images using a Go template")
    24  
    25  	flFilter := opts.NewListOpts(nil)
    26  	cmd.Var(&flFilter, []string{"f", "-filter"}, "Filter output based on conditions provided")
    27  	cmd.Require(flag.Max, 1)
    28  
    29  	cmd.ParseFlags(args, true)
    30  
    31  	// Consolidate all filter flags, and sanity check them early.
    32  	// They'll get process in the daemon/server.
    33  	imageFilterArgs := filters.NewArgs()
    34  	for _, f := range flFilter.GetAll() {
    35  		var err error
    36  		imageFilterArgs, err = filters.ParseFlag(f, imageFilterArgs)
    37  		if err != nil {
    38  			return err
    39  		}
    40  	}
    41  
    42  	var matchName string
    43  	if cmd.NArg() == 1 {
    44  		matchName = cmd.Arg(0)
    45  	}
    46  
    47  	options := types.ImageListOptions{
    48  		MatchName: matchName,
    49  		All:       *all,
    50  		Filters:   imageFilterArgs,
    51  	}
    52  
    53  	images, err := cli.client.ImageList(context.Background(), options)
    54  	if err != nil {
    55  		return err
    56  	}
    57  
    58  	f := *format
    59  	if len(f) == 0 {
    60  		if len(cli.ImagesFormat()) > 0 && !*quiet {
    61  			f = cli.ImagesFormat()
    62  		} else {
    63  			f = "table"
    64  		}
    65  	}
    66  
    67  	imagesCtx := formatter.ImageContext{
    68  		Context: formatter.Context{
    69  			Output: cli.out,
    70  			Format: f,
    71  			Quiet:  *quiet,
    72  			Trunc:  !*noTrunc,
    73  		},
    74  		Digest: *showDigests,
    75  		Images: images,
    76  	}
    77  
    78  	imagesCtx.Write()
    79  
    80  	return nil
    81  }