2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
|
|
|
"github.com/docker/docker/reference"
|
2016-09-12 16:59:18 -04:00
|
|
|
units "github.com/docker/go-units"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
defaultImageTableFormat = "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.CreatedSince}} ago\t{{.Size}}"
|
|
|
|
defaultImageTableFormatWithDigest = "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.ID}}\t{{.CreatedSince}} ago\t{{.Size}}"
|
|
|
|
|
|
|
|
imageIDHeader = "IMAGE ID"
|
|
|
|
repositoryHeader = "REPOSITORY"
|
|
|
|
tagHeader = "TAG"
|
|
|
|
digestHeader = "DIGEST"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ImageContext contains image specific information required by the formater, encapsulate a Context struct.
|
|
|
|
type ImageContext struct {
|
|
|
|
Context
|
|
|
|
Digest bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func isDangling(image types.Image) bool {
|
|
|
|
return len(image.RepoTags) == 1 && image.RepoTags[0] == "<none>:<none>" && len(image.RepoDigests) == 1 && image.RepoDigests[0] == "<none>@<none>"
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// NewImageFormat returns a format for rendering an ImageContext
|
|
|
|
func NewImageFormat(source string, quiet bool, digest bool) Format {
|
|
|
|
switch source {
|
|
|
|
case TableFormatKey:
|
|
|
|
switch {
|
|
|
|
case quiet:
|
|
|
|
return defaultQuietFormat
|
|
|
|
case digest:
|
|
|
|
return defaultImageTableFormatWithDigest
|
|
|
|
default:
|
|
|
|
return defaultImageTableFormat
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
case RawFormatKey:
|
|
|
|
switch {
|
|
|
|
case quiet:
|
|
|
|
return `image_id: {{.ID}}`
|
|
|
|
case digest:
|
|
|
|
return `repository: {{ .Repository }}
|
2016-09-08 13:11:39 -04:00
|
|
|
tag: {{.Tag}}
|
|
|
|
digest: {{.Digest}}
|
|
|
|
image_id: {{.ID}}
|
|
|
|
created_at: {{.CreatedAt}}
|
|
|
|
virtual_size: {{.Size}}
|
|
|
|
`
|
2016-09-12 16:59:18 -04:00
|
|
|
default:
|
|
|
|
return `repository: {{ .Repository }}
|
2016-09-08 13:11:39 -04:00
|
|
|
tag: {{.Tag}}
|
|
|
|
image_id: {{.ID}}
|
|
|
|
created_at: {{.CreatedAt}}
|
|
|
|
virtual_size: {{.Size}}
|
|
|
|
`
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
format := Format(source)
|
|
|
|
if format.IsTable() && digest && !format.Contains("{{.Digest}}") {
|
|
|
|
format += "\t{{.Digest}}"
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return format
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
// ImageWrite writes the formatter images using the ImageContext
|
|
|
|
func ImageWrite(ctx ImageContext, images []types.Image) error {
|
|
|
|
render := func(format func(subContext subContext) error) error {
|
|
|
|
return imageFormat(ctx, images, format)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return ctx.Write(&imageContext{}, render)
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2016-09-12 16:59:18 -04:00
|
|
|
func imageFormat(ctx ImageContext, images []types.Image, format func(subContext subContext) error) error {
|
|
|
|
for _, image := range images {
|
2016-09-08 13:11:39 -04:00
|
|
|
images := []*imageContext{}
|
|
|
|
if isDangling(image) {
|
|
|
|
images = append(images, &imageContext{
|
|
|
|
trunc: ctx.Trunc,
|
|
|
|
i: image,
|
|
|
|
repo: "<none>",
|
|
|
|
tag: "<none>",
|
|
|
|
digest: "<none>",
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
repoTags := map[string][]string{}
|
|
|
|
repoDigests := map[string][]string{}
|
|
|
|
|
|
|
|
for _, refString := range append(image.RepoTags) {
|
|
|
|
ref, err := reference.ParseNamed(refString)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if nt, ok := ref.(reference.NamedTagged); ok {
|
|
|
|
repoTags[ref.Name()] = append(repoTags[ref.Name()], nt.Tag())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, refString := range append(image.RepoDigests) {
|
|
|
|
ref, err := reference.ParseNamed(refString)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c, ok := ref.(reference.Canonical); ok {
|
|
|
|
repoDigests[ref.Name()] = append(repoDigests[ref.Name()], c.Digest().String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for repo, tags := range repoTags {
|
|
|
|
digests := repoDigests[repo]
|
|
|
|
|
|
|
|
// Do not display digests as their own row
|
|
|
|
delete(repoDigests, repo)
|
|
|
|
|
|
|
|
if !ctx.Digest {
|
|
|
|
// Ignore digest references, just show tag once
|
|
|
|
digests = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
if len(digests) == 0 {
|
|
|
|
images = append(images, &imageContext{
|
|
|
|
trunc: ctx.Trunc,
|
|
|
|
i: image,
|
|
|
|
repo: repo,
|
|
|
|
tag: tag,
|
|
|
|
digest: "<none>",
|
|
|
|
})
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Display the digests for each tag
|
|
|
|
for _, dgst := range digests {
|
|
|
|
images = append(images, &imageContext{
|
|
|
|
trunc: ctx.Trunc,
|
|
|
|
i: image,
|
|
|
|
repo: repo,
|
|
|
|
tag: tag,
|
|
|
|
digest: dgst,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Show rows for remaining digest only references
|
|
|
|
for repo, digests := range repoDigests {
|
|
|
|
// If digests are displayed, show row per digest
|
|
|
|
if ctx.Digest {
|
|
|
|
for _, dgst := range digests {
|
|
|
|
images = append(images, &imageContext{
|
|
|
|
trunc: ctx.Trunc,
|
|
|
|
i: image,
|
|
|
|
repo: repo,
|
|
|
|
tag: "<none>",
|
|
|
|
digest: dgst,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
images = append(images, &imageContext{
|
|
|
|
trunc: ctx.Trunc,
|
|
|
|
i: image,
|
|
|
|
repo: repo,
|
|
|
|
tag: "<none>",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, imageCtx := range images {
|
2016-09-12 16:59:18 -04:00
|
|
|
if err := format(imageCtx); err != nil {
|
|
|
|
return err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-12 16:59:18 -04:00
|
|
|
return nil
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type imageContext struct {
|
2016-09-12 16:59:18 -04:00
|
|
|
HeaderContext
|
2016-09-08 13:11:39 -04:00
|
|
|
trunc bool
|
|
|
|
i types.Image
|
|
|
|
repo string
|
|
|
|
tag string
|
|
|
|
digest string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) ID() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(imageIDHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
if c.trunc {
|
|
|
|
return stringid.TruncateID(c.i.ID)
|
|
|
|
}
|
|
|
|
return c.i.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) Repository() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(repositoryHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.repo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) Tag() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(tagHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) Digest() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(digestHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return c.digest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) CreatedSince() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(createdSinceHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
createdAt := time.Unix(int64(c.i.Created), 0)
|
|
|
|
return units.HumanDuration(time.Now().UTC().Sub(createdAt))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) CreatedAt() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(createdAtHeader)
|
2016-09-08 13:11:39 -04:00
|
|
|
return time.Unix(int64(c.i.Created), 0).String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) Size() string {
|
2016-09-12 16:59:18 -04:00
|
|
|
c.AddHeader(sizeHeader)
|
2016-09-04 17:44:34 -04:00
|
|
|
return units.HumanSizeWithPrecision(float64(c.i.Size), 3)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|