2016-09-08 13:11:39 -04:00
|
|
|
package formatter
|
|
|
|
|
|
|
|
import (
|
2016-08-23 19:37:37 -04:00
|
|
|
"fmt"
|
2016-09-08 13:11:39 -04:00
|
|
|
"time"
|
|
|
|
|
2017-01-11 16:54:52 -05:00
|
|
|
"github.com/docker/distribution/reference"
|
2016-09-08 13:11:39 -04:00
|
|
|
"github.com/docker/docker/api/types"
|
|
|
|
"github.com/docker/docker/pkg/stringid"
|
2016-09-12 16:59:18 -04:00
|
|
|
units "github.com/docker/go-units"
|
2016-09-08 13:11:39 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-10-29 16:15:13 -04:00
|
|
|
defaultImageTableFormat = "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{if .CreatedSince }}{{.CreatedSince}}{{else}}N/A{{end}}\t{{.Size}}"
|
|
|
|
defaultImageTableFormatWithDigest = "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.ID}}\t{{if .CreatedSince }}{{.CreatedSince}}{{else}}N/A{{end}}\t{{.Size}}"
|
2016-09-08 13:11:39 -04:00
|
|
|
|
|
|
|
imageIDHeader = "IMAGE ID"
|
|
|
|
repositoryHeader = "REPOSITORY"
|
|
|
|
tagHeader = "TAG"
|
|
|
|
digestHeader = "DIGEST"
|
|
|
|
)
|
|
|
|
|
2017-01-17 11:26:37 -05:00
|
|
|
// ImageContext contains image specific information required by the formatter, encapsulate a Context struct.
|
2016-09-08 13:11:39 -04:00
|
|
|
type ImageContext struct {
|
|
|
|
Context
|
|
|
|
Digest bool
|
|
|
|
}
|
|
|
|
|
2016-10-03 15:17:39 -04:00
|
|
|
func isDangling(image types.ImageSummary) bool {
|
2016-09-08 13:11:39 -04:00
|
|
|
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:
|
2018-10-23 11:05:44 -04:00
|
|
|
return DefaultQuietFormat
|
2016-09-12 16:59:18 -04:00
|
|
|
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
|
2016-10-03 15:17:39 -04:00
|
|
|
func ImageWrite(ctx ImageContext, images []types.ImageSummary) error {
|
2018-10-23 11:05:44 -04:00
|
|
|
render := func(format func(subContext SubContext) error) error {
|
2016-09-12 16:59:18 -04:00
|
|
|
return imageFormat(ctx, images, format)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-02-03 19:48:46 -05:00
|
|
|
return ctx.Write(newImageContext(), render)
|
2016-09-12 16:59:18 -04:00
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-08-14 05:06:31 -04:00
|
|
|
// needDigest determines whether the image digest should be ignored or not when writing image context
|
|
|
|
func needDigest(ctx ImageContext) bool {
|
|
|
|
return ctx.Digest || ctx.Format.Contains("{{.Digest}}")
|
|
|
|
}
|
|
|
|
|
2018-10-23 11:05:44 -04:00
|
|
|
func imageFormat(ctx ImageContext, images []types.ImageSummary, format func(subContext SubContext) error) error {
|
2016-09-12 16:59:18 -04:00
|
|
|
for _, image := range images {
|
2017-05-09 17:24:40 -04:00
|
|
|
formatted := []*imageContext{}
|
2016-09-08 13:11:39 -04:00
|
|
|
if isDangling(image) {
|
2017-05-09 17:24:40 -04:00
|
|
|
formatted = append(formatted, &imageContext{
|
2016-09-08 13:11:39 -04:00
|
|
|
trunc: ctx.Trunc,
|
|
|
|
i: image,
|
|
|
|
repo: "<none>",
|
|
|
|
tag: "<none>",
|
|
|
|
digest: "<none>",
|
|
|
|
})
|
|
|
|
} else {
|
2017-05-09 17:24:40 -04:00
|
|
|
formatted = imageFormatTaggedAndDigest(ctx, image)
|
|
|
|
}
|
|
|
|
for _, imageCtx := range formatted {
|
|
|
|
if err := format(imageCtx); err != nil {
|
|
|
|
return err
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 17:24:40 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-05-09 17:24:40 -04:00
|
|
|
func imageFormatTaggedAndDigest(ctx ImageContext, image types.ImageSummary) []*imageContext {
|
|
|
|
repoTags := map[string][]string{}
|
|
|
|
repoDigests := map[string][]string{}
|
|
|
|
images := []*imageContext{}
|
|
|
|
|
|
|
|
for _, refString := range image.RepoTags {
|
|
|
|
ref, err := reference.ParseNormalizedNamed(refString)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if nt, ok := ref.(reference.NamedTagged); ok {
|
|
|
|
familiarRef := reference.FamiliarName(ref)
|
|
|
|
repoTags[familiarRef] = append(repoTags[familiarRef], nt.Tag())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, refString := range image.RepoDigests {
|
|
|
|
ref, err := reference.ParseNormalizedNamed(refString)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c, ok := ref.(reference.Canonical); ok {
|
|
|
|
familiarRef := reference.FamiliarName(ref)
|
|
|
|
repoDigests[familiarRef] = append(repoDigests[familiarRef], c.Digest().String())
|
|
|
|
}
|
|
|
|
}
|
2016-09-08 13:11:39 -04:00
|
|
|
|
2017-05-09 17:24:40 -04:00
|
|
|
addImage := func(repo, tag, digest string) {
|
|
|
|
image := &imageContext{
|
|
|
|
trunc: ctx.Trunc,
|
|
|
|
i: image,
|
|
|
|
repo: repo,
|
|
|
|
tag: tag,
|
|
|
|
digest: digest,
|
|
|
|
}
|
|
|
|
images = append(images, image)
|
|
|
|
}
|
|
|
|
|
|
|
|
for repo, tags := range repoTags {
|
|
|
|
digests := repoDigests[repo]
|
|
|
|
|
|
|
|
// Do not display digests as their own row
|
|
|
|
delete(repoDigests, repo)
|
|
|
|
|
|
|
|
if !needDigest(ctx) {
|
|
|
|
// Ignore digest references, just show tag once
|
|
|
|
digests = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range tags {
|
|
|
|
if len(digests) == 0 {
|
|
|
|
addImage(repo, tag, "<none>")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Display the digests for each tag
|
|
|
|
for _, dgst := range digests {
|
|
|
|
addImage(repo, tag, dgst)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 17:24:40 -04:00
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 17:24:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
addImage(repo, "<none>", dgst)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2017-05-09 17:24:40 -04:00
|
|
|
} else {
|
|
|
|
addImage(repo, "<none>", "")
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
}
|
2017-05-09 17:24:40 -04:00
|
|
|
return images
|
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
|
2016-10-03 15:17:39 -04:00
|
|
|
i types.ImageSummary
|
2016-09-08 13:11:39 -04:00
|
|
|
repo string
|
|
|
|
tag string
|
|
|
|
digest string
|
|
|
|
}
|
|
|
|
|
2017-02-03 19:48:46 -05:00
|
|
|
func newImageContext() *imageContext {
|
|
|
|
imageCtx := imageContext{}
|
2018-10-23 11:05:44 -04:00
|
|
|
imageCtx.Header = SubHeaderContext{
|
2017-02-03 19:48:46 -05:00
|
|
|
"ID": imageIDHeader,
|
|
|
|
"Repository": repositoryHeader,
|
|
|
|
"Tag": tagHeader,
|
|
|
|
"Digest": digestHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"CreatedSince": CreatedSinceHeader,
|
|
|
|
"CreatedAt": CreatedAtHeader,
|
|
|
|
"Size": SizeHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"Containers": containersHeader,
|
2018-10-23 11:05:44 -04:00
|
|
|
"VirtualSize": SizeHeader,
|
2017-02-03 19:48:46 -05:00
|
|
|
"SharedSize": sharedSizeHeader,
|
|
|
|
"UniqueSize": uniqueSizeHeader,
|
|
|
|
}
|
|
|
|
return &imageCtx
|
|
|
|
}
|
|
|
|
|
2017-01-19 04:50:28 -05:00
|
|
|
func (c *imageContext) MarshalJSON() ([]byte, error) {
|
2018-10-23 11:05:44 -04:00
|
|
|
return MarshalJSON(c)
|
2017-01-19 04:50:28 -05:00
|
|
|
}
|
|
|
|
|
2016-09-08 13:11:39 -04:00
|
|
|
func (c *imageContext) ID() string {
|
|
|
|
if c.trunc {
|
|
|
|
return stringid.TruncateID(c.i.ID)
|
|
|
|
}
|
|
|
|
return c.i.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) Repository() string {
|
|
|
|
return c.repo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) Tag() string {
|
|
|
|
return c.tag
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) Digest() string {
|
|
|
|
return c.digest
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) CreatedSince() string {
|
2017-06-09 16:41:53 -04:00
|
|
|
createdAt := time.Unix(c.i.Created, 0)
|
2019-10-29 16:15:13 -04:00
|
|
|
|
|
|
|
if createdAt.IsZero() {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2017-02-03 19:48:46 -05:00
|
|
|
return units.HumanDuration(time.Now().UTC().Sub(createdAt)) + " ago"
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) CreatedAt() string {
|
2017-06-09 16:41:53 -04:00
|
|
|
return time.Unix(c.i.Created, 0).String()
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) Size() string {
|
2017-01-11 16:42:49 -05:00
|
|
|
return units.HumanSizeWithPrecision(float64(c.i.Size), 3)
|
2016-09-08 13:11:39 -04:00
|
|
|
}
|
2016-08-23 19:37:37 -04:00
|
|
|
|
|
|
|
func (c *imageContext) Containers() string {
|
|
|
|
if c.i.Containers == -1 {
|
|
|
|
return "N/A"
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("%d", c.i.Containers)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) VirtualSize() string {
|
|
|
|
return units.HumanSize(float64(c.i.VirtualSize))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) SharedSize() string {
|
|
|
|
if c.i.SharedSize == -1 {
|
|
|
|
return "N/A"
|
|
|
|
}
|
|
|
|
return units.HumanSize(float64(c.i.SharedSize))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *imageContext) UniqueSize() string {
|
2017-01-11 16:42:49 -05:00
|
|
|
if c.i.VirtualSize == -1 || c.i.SharedSize == -1 {
|
2016-08-23 19:37:37 -04:00
|
|
|
return "N/A"
|
|
|
|
}
|
2017-01-11 16:42:49 -05:00
|
|
|
return units.HumanSize(float64(c.i.VirtualSize - c.i.SharedSize))
|
2016-08-23 19:37:37 -04:00
|
|
|
}
|