github.com/justincormack/cli@v0.0.0-20201215022714-831ebeae9675/cli/command/image/list.go (about)

     1  package image
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/docker/cli/cli"
     7  	"github.com/docker/cli/cli/command"
     8  	"github.com/docker/cli/cli/command/formatter"
     9  	"github.com/docker/cli/opts"
    10  	"github.com/docker/docker/api/types"
    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      opts.FilterOpt
    23  }
    24  
    25  // NewImagesCommand creates a new `docker images` command
    26  func NewImagesCommand(dockerCli command.Cli) *cobra.Command {
    27  	options := imagesOptions{filter: opts.NewFilterOpt()}
    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  				options.matchName = args[0]
    36  			}
    37  			return runImages(dockerCli, options)
    38  		},
    39  	}
    40  
    41  	flags := cmd.Flags()
    42  
    43  	flags.BoolVarP(&options.quiet, "quiet", "q", false, "Only show image IDs")
    44  	flags.BoolVarP(&options.all, "all", "a", false, "Show all images (default hides intermediate images)")
    45  	flags.BoolVar(&options.noTrunc, "no-trunc", false, "Don't truncate output")
    46  	flags.BoolVar(&options.showDigests, "digests", false, "Show digests")
    47  	flags.StringVar(&options.format, "format", "", "Pretty-print images using a Go template")
    48  	flags.VarP(&options.filter, "filter", "f", "Filter output based on conditions provided")
    49  
    50  	return cmd
    51  }
    52  
    53  func newListCommand(dockerCli command.Cli) *cobra.Command {
    54  	cmd := *NewImagesCommand(dockerCli)
    55  	cmd.Aliases = []string{"list"}
    56  	cmd.Use = "ls [OPTIONS] [REPOSITORY[:TAG]]"
    57  	return &cmd
    58  }
    59  
    60  func runImages(dockerCli command.Cli, options imagesOptions) error {
    61  	ctx := context.Background()
    62  
    63  	filters := options.filter.Value()
    64  	if options.matchName != "" {
    65  		filters.Add("reference", options.matchName)
    66  	}
    67  
    68  	listOptions := types.ImageListOptions{
    69  		All:     options.all,
    70  		Filters: filters,
    71  	}
    72  
    73  	images, err := dockerCli.Client().ImageList(ctx, listOptions)
    74  	if err != nil {
    75  		return err
    76  	}
    77  
    78  	format := options.format
    79  	if len(format) == 0 {
    80  		if len(dockerCli.ConfigFile().ImagesFormat) > 0 && !options.quiet {
    81  			format = dockerCli.ConfigFile().ImagesFormat
    82  		} else {
    83  			format = formatter.TableFormatKey
    84  		}
    85  	}
    86  
    87  	imageCtx := formatter.ImageContext{
    88  		Context: formatter.Context{
    89  			Output: dockerCli.Out(),
    90  			Format: formatter.NewImageFormat(format, options.quiet, options.showDigests),
    91  			Trunc:  !options.noTrunc,
    92  		},
    93  		Digest: options.showDigests,
    94  	}
    95  	return formatter.ImageWrite(imageCtx, images)
    96  }