github.com/docker/app@v0.9.1-beta3.0.20210611140623-a48f773ab002/internal/commands/image/formatter.go (about)

     1  package image
     2  
     3  import (
     4  	"github.com/docker/cli/cli/command/formatter"
     5  	"github.com/docker/docker/pkg/stringid"
     6  )
     7  
     8  const (
     9  	defaultImageTableFormat           = "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.Name}}\t"
    10  	defaultImageTableFormatWithDigest = "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.ID}}\t{{.Name}}\t\t"
    11  
    12  	imageIDHeader    = "APP IMAGE ID"
    13  	repositoryHeader = "REPOSITORY"
    14  	tagHeader        = "TAG"
    15  	digestHeader     = "DIGEST"
    16  	imageNameHeader  = "APP NAME"
    17  )
    18  
    19  // NewImageFormat returns a format for rendering an ImageContext
    20  func NewImageFormat(source string, quiet bool, digest bool) formatter.Format {
    21  	switch source {
    22  	case formatter.TableFormatKey:
    23  		switch {
    24  		case quiet:
    25  			return formatter.DefaultQuietFormat
    26  		case digest:
    27  			return defaultImageTableFormatWithDigest
    28  		default:
    29  			return defaultImageTableFormat
    30  		}
    31  	}
    32  
    33  	format := formatter.Format(source)
    34  	if format.IsTable() && digest && !format.Contains("{{.Digest}}") {
    35  		format += "\t{{.Digest}}"
    36  	}
    37  	return format
    38  }
    39  
    40  // Write writes the formatter images using the ImageContext
    41  func Write(ctx formatter.Context, images []imageDesc) error {
    42  	render := func(format func(subContext formatter.SubContext) error) error {
    43  		return imageFormat(ctx, images, format)
    44  	}
    45  	return ctx.Write(newImageContext(), render)
    46  }
    47  
    48  func imageFormat(ctx formatter.Context, images []imageDesc, format func(subContext formatter.SubContext) error) error {
    49  	for _, image := range images {
    50  		img := &imageContext{
    51  			trunc: ctx.Trunc,
    52  			i:     image}
    53  		if err := format(img); err != nil {
    54  			return err
    55  		}
    56  	}
    57  	return nil
    58  }
    59  
    60  type imageContext struct {
    61  	formatter.HeaderContext
    62  	trunc bool
    63  	i     imageDesc
    64  }
    65  
    66  func newImageContext() *imageContext {
    67  	imageCtx := imageContext{}
    68  	imageCtx.Header = formatter.SubHeaderContext{
    69  		"ID":         imageIDHeader,
    70  		"Name":       imageNameHeader,
    71  		"Repository": repositoryHeader,
    72  		"Tag":        tagHeader,
    73  		"Digest":     digestHeader,
    74  	}
    75  	return &imageCtx
    76  }
    77  
    78  func (c *imageContext) MarshalJSON() ([]byte, error) {
    79  	return formatter.MarshalJSON(c)
    80  }
    81  
    82  func (c *imageContext) ID() string {
    83  	if c.trunc {
    84  		return stringid.TruncateID(c.i.ID)
    85  	}
    86  	return c.i.ID
    87  }
    88  
    89  func (c *imageContext) Name() string {
    90  	if c.i.Name == "" {
    91  		return "<none>"
    92  	}
    93  	return c.i.Name
    94  }
    95  
    96  func (c *imageContext) Repository() string {
    97  	if c.i.Repository == "" {
    98  		return "<none>"
    99  	}
   100  	return c.i.Repository
   101  }
   102  
   103  func (c *imageContext) Tag() string {
   104  	if c.i.Tag == "" {
   105  		return "<none>"
   106  	}
   107  	return c.i.Tag
   108  }
   109  
   110  func (c *imageContext) Digest() string {
   111  	if c.i.Digest == "" {
   112  		return "<none>"
   113  	}
   114  	return c.i.Digest
   115  }