github.com/ncdc/docker@v0.10.1-0.20160129113957-6c6729ef5b74/api/client/images.go (about)

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