github.com/portworx/docker@v1.12.1/api/client/image/images.go (about)

     1  package image
     2  
     3  import (
     4  	"golang.org/x/net/context"
     5  
     6  	"github.com/docker/docker/api/client"
     7  	"github.com/docker/docker/api/client/formatter"
     8  	"github.com/docker/docker/cli"
     9  	"github.com/docker/engine-api/types"
    10  	"github.com/docker/engine-api/types/filters"
    11  	"github.com/spf13/cobra"
    12  )
    13  
    14  type imagesOptions struct {
    15  	matchName string
    16  
    17  	quiet       bool
    18  	all         bool
    19  	noTrunc     bool
    20  	showDigests bool
    21  	format      string
    22  	filter      []string
    23  }
    24  
    25  // NewImagesCommand create a new `docker images` command
    26  func NewImagesCommand(dockerCli *client.DockerCli) *cobra.Command {
    27  	var opts imagesOptions
    28  
    29  	cmd := &cobra.Command{
    30  		Use:   "images [OPTIONS] [REPOSITORY[:TAG]]",
    31  		Short: "List images",
    32  		Args:  cli.RequiresMaxArgs(1),
    33  		RunE: func(cmd *cobra.Command, args []string) error {
    34  			if len(args) > 0 {
    35  				opts.matchName = args[0]
    36  			}
    37  			return runImages(dockerCli, opts)
    38  		},
    39  	}
    40  
    41  	flags := cmd.Flags()
    42  
    43  	flags.BoolVarP(&opts.quiet, "quiet", "q", false, "Only show numeric IDs")
    44  	flags.BoolVarP(&opts.all, "all", "a", false, "Show all images (default hides intermediate images)")
    45  	flags.BoolVar(&opts.noTrunc, "no-trunc", false, "Don't truncate output")
    46  	flags.BoolVar(&opts.showDigests, "digests", false, "Show digests")
    47  	flags.StringVar(&opts.format, "format", "", "Pretty-print images using a Go template")
    48  	flags.StringSliceVarP(&opts.filter, "filter", "f", []string{}, "Filter output based on conditions provided")
    49  
    50  	return cmd
    51  }
    52  
    53  func runImages(dockerCli *client.DockerCli, opts imagesOptions) error {
    54  	ctx := context.Background()
    55  
    56  	// Consolidate all filter flags, and sanity check them early.
    57  	// They'll get process in the daemon/server.
    58  	imageFilterArgs := filters.NewArgs()
    59  	for _, f := range opts.filter {
    60  		var err error
    61  		imageFilterArgs, err = filters.ParseFlag(f, imageFilterArgs)
    62  		if err != nil {
    63  			return err
    64  		}
    65  	}
    66  
    67  	matchName := opts.matchName
    68  
    69  	options := types.ImageListOptions{
    70  		MatchName: matchName,
    71  		All:       opts.all,
    72  		Filters:   imageFilterArgs,
    73  	}
    74  
    75  	images, err := dockerCli.Client().ImageList(ctx, options)
    76  	if err != nil {
    77  		return err
    78  	}
    79  
    80  	f := opts.format
    81  	if len(f) == 0 {
    82  		if len(dockerCli.ImagesFormat()) > 0 && !opts.quiet {
    83  			f = dockerCli.ImagesFormat()
    84  		} else {
    85  			f = "table"
    86  		}
    87  	}
    88  
    89  	imagesCtx := formatter.ImageContext{
    90  		Context: formatter.Context{
    91  			Output: dockerCli.Out(),
    92  			Format: f,
    93  			Quiet:  opts.quiet,
    94  			Trunc:  !opts.noTrunc,
    95  		},
    96  		Digest: opts.showDigests,
    97  		Images: images,
    98  	}
    99  
   100  	imagesCtx.Write()
   101  
   102  	return nil
   103  }