This repository has been archived by the owner on Jun 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 177
Support format option in docker app ls #743
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
40a670c
Support format option in docker app ls
ndeloof 9b4f291
Introduce --format option for docker app image ls
ndeloof 92bf8cc
simplification: remove intermediate pkg struct
ndeloof 81921ff
Use a formatter for app image ls
ndeloof 04a69f8
Get a happy-linter
ndeloof d5e1ff5
table minwidth is 20
ndeloof File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,126 @@ | ||||||
package image | ||||||
|
||||||
import ( | ||||||
"time" | ||||||
|
||||||
"github.com/docker/cli/cli/command/formatter" | ||||||
"github.com/docker/docker/pkg/stringid" | ||||||
"github.com/docker/go-units" | ||||||
) | ||||||
|
||||||
const ( | ||||||
defaultImageTableFormat = "table {{.Repository}}\t{{.Tag}}\t{{.ID}}\t{{.Name}}\t{{if .CreatedSince }}{{.CreatedSince}}{{else}}N/A{{end}}\t" | ||||||
defaultImageTableFormatWithDigest = "table {{.Repository}}\t{{.Tag}}\t{{.Digest}}\t{{.ID}}\t{{.Name}}\t\t{{if .CreatedSince }}{{.CreatedSince}}{{else}}N/A{{end}}\t" | ||||||
|
||||||
imageIDHeader = "APP IMAGE ID" | ||||||
repositoryHeader = "REPOSITORY" | ||||||
tagHeader = "TAG" | ||||||
digestHeader = "DIGEST" | ||||||
imageNameHeader = "APP NAME" | ||||||
) | ||||||
|
||||||
// NewImageFormat returns a format for rendering an ImageContext | ||||||
func NewImageFormat(source string, quiet bool, digest bool) formatter.Format { | ||||||
switch source { | ||||||
case formatter.TableFormatKey: | ||||||
switch { | ||||||
case quiet: | ||||||
return formatter.DefaultQuietFormat | ||||||
case digest: | ||||||
return defaultImageTableFormatWithDigest | ||||||
default: | ||||||
return defaultImageTableFormat | ||||||
} | ||||||
} | ||||||
|
||||||
format := formatter.Format(source) | ||||||
if format.IsTable() && digest && !format.Contains("{{.Digest}}") { | ||||||
format += "\t{{.Digest}}" | ||||||
} | ||||||
return format | ||||||
} | ||||||
|
||||||
// Write writes the formatter images using the ImageContext | ||||||
func Write(ctx formatter.Context, images []imageDesc) error { | ||||||
render := func(format func(subContext formatter.SubContext) error) error { | ||||||
return imageFormat(ctx, images, format) | ||||||
} | ||||||
return ctx.Write(newImageContext(), render) | ||||||
} | ||||||
|
||||||
func imageFormat(ctx formatter.Context, images []imageDesc, format func(subContext formatter.SubContext) error) error { | ||||||
for _, image := range images { | ||||||
img := &imageContext{ | ||||||
trunc: ctx.Trunc, | ||||||
i: image} | ||||||
if err := format(img); err != nil { | ||||||
return err | ||||||
} | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
type imageContext struct { | ||||||
formatter.HeaderContext | ||||||
trunc bool | ||||||
i imageDesc | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not ?
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same reason |
||||||
} | ||||||
|
||||||
func newImageContext() *imageContext { | ||||||
imageCtx := imageContext{} | ||||||
imageCtx.Header = formatter.SubHeaderContext{ | ||||||
"ID": imageIDHeader, | ||||||
"Name": imageNameHeader, | ||||||
"Repository": repositoryHeader, | ||||||
"Tag": tagHeader, | ||||||
"Digest": digestHeader, | ||||||
"CreatedSince": formatter.CreatedSinceHeader, | ||||||
} | ||||||
return &imageCtx | ||||||
} | ||||||
|
||||||
func (c *imageContext) MarshalJSON() ([]byte, error) { | ||||||
return formatter.MarshalJSON(c) | ||||||
} | ||||||
|
||||||
func (c *imageContext) ID() string { | ||||||
if c.trunc { | ||||||
return stringid.TruncateID(c.i.ID) | ||||||
} | ||||||
return c.i.ID | ||||||
} | ||||||
|
||||||
func (c *imageContext) Name() string { | ||||||
if c.i.Name == "" { | ||||||
return "<none>" | ||||||
} | ||||||
return c.i.Name | ||||||
} | ||||||
|
||||||
func (c *imageContext) Repository() string { | ||||||
if c.i.Repository == "" { | ||||||
return "<none>" | ||||||
} | ||||||
return c.i.Repository | ||||||
} | ||||||
|
||||||
func (c *imageContext) Tag() string { | ||||||
if c.i.Tag == "" { | ||||||
return "<none>" | ||||||
} | ||||||
return c.i.Tag | ||||||
} | ||||||
|
||||||
func (c *imageContext) Digest() string { | ||||||
if c.i.Digest == "" { | ||||||
return "<none>" | ||||||
} | ||||||
return c.i.Digest | ||||||
} | ||||||
|
||||||
func (c *imageContext) CreatedSince() string { | ||||||
if c.i.Created.IsZero() { | ||||||
return "" | ||||||
} | ||||||
return units.HumanDuration(time.Now().UTC().Sub(c.i.Created)) + " ago" | ||||||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have written it like:
I don't see the usage of
switch
with only one case andswitch
on different variables.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because this code was a (simplified) version of the original formatter from docker/cli. Wanted to keep implementations in sync
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I understand.