-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
image/list: Add --tree
flag
#4982
Draft
vvoland
wants to merge
3
commits into
docker:master
Choose a base branch
from
vvoland:c8d-multiplatform-list
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+470
−9
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,252 @@ | ||
package image | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"unicode/utf8" | ||
|
||
"github.com/docker/cli/cli/command" | ||
"github.com/morikuni/aec" | ||
|
||
"github.com/containerd/platforms" | ||
"github.com/docker/docker/api/types/filters" | ||
imagetypes "github.com/docker/docker/api/types/image" | ||
"github.com/docker/docker/pkg/stringid" | ||
"github.com/docker/go-units" | ||
) | ||
|
||
type treeOptions struct { | ||
all bool | ||
filters filters.Args | ||
} | ||
|
||
func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error { | ||
images, err := dockerCLI.Client().ImageList(ctx, imagetypes.ListOptions{ | ||
All: opts.all, | ||
ContainerCount: true, | ||
Filters: opts.filters, | ||
Manifests: true, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
view := make([]topImage, 0, len(images)) | ||
for _, img := range images { | ||
details := imageDetails{ | ||
ID: img.ID, | ||
DiskUsage: units.HumanSizeWithPrecision(float64(img.Size), 3), | ||
Used: img.Containers > 0, | ||
} | ||
|
||
children := make([]subImage, 0, len(img.Manifests)) | ||
for _, im := range img.Manifests { | ||
if im.Kind != imagetypes.ImageManifestKindImage { | ||
continue | ||
} | ||
|
||
imgData := im.ImageData | ||
platform := imgData.Platform | ||
|
||
sub := subImage{ | ||
Platform: platforms.Format(platform), | ||
Available: im.Available, | ||
Details: imageDetails{ | ||
ID: im.ID, | ||
DiskUsage: units.HumanSizeWithPrecision(float64(im.ContentSize+imgData.UnpackedSize), 3), | ||
Used: len(imgData.Containers) > 0, | ||
}, | ||
} | ||
|
||
children = append(children, sub) | ||
} | ||
|
||
view = append(view, topImage{ | ||
Names: img.RepoTags, | ||
Details: details, | ||
Children: children, | ||
}) | ||
} | ||
|
||
return printImageTree(dockerCLI, view) | ||
} | ||
|
||
type imageDetails struct { | ||
ID string | ||
DiskUsage string | ||
Used bool | ||
} | ||
|
||
type topImage struct { | ||
Names []string | ||
Details imageDetails | ||
Children []subImage | ||
} | ||
|
||
type subImage struct { | ||
Platform string | ||
Available bool | ||
Details imageDetails | ||
} | ||
|
||
func printImageTree(dockerCLI command.Cli, images []topImage) error { | ||
out := dockerCLI.Out() | ||
_, width := out.GetTtySize() | ||
|
||
headers := []header{ | ||
{Title: "Image", Width: 0, Left: true}, | ||
{Title: "ID", Width: 12}, | ||
{Title: "Disk use", Width: 8}, | ||
{Title: "Used", Width: 4}, | ||
} | ||
|
||
const spacing = 3 | ||
nameWidth := int(width) | ||
for _, h := range headers { | ||
if h.Width == 0 { | ||
continue | ||
} | ||
nameWidth -= h.Width | ||
nameWidth -= spacing | ||
} | ||
|
||
maxImageName := len(headers[0].Title) | ||
for _, img := range images { | ||
for _, name := range img.Names { | ||
if len(name) > maxImageName { | ||
maxImageName = len(name) | ||
} | ||
} | ||
for _, sub := range img.Children { | ||
if len(sub.Platform) > maxImageName { | ||
maxImageName = len(sub.Platform) | ||
} | ||
} | ||
} | ||
|
||
if nameWidth > maxImageName+spacing { | ||
nameWidth = maxImageName + spacing | ||
} | ||
|
||
if nameWidth < 0 { | ||
headers = headers[:1] | ||
nameWidth = int(width) | ||
} | ||
headers[0].Width = nameWidth | ||
|
||
headerColor := aec.NewBuilder(aec.DefaultF, aec.Bold).ANSI | ||
|
||
// Print headers | ||
for i, h := range headers { | ||
if i > 0 { | ||
_, _ = fmt.Fprint(out, strings.Repeat(" ", spacing)) | ||
} | ||
|
||
_, _ = fmt.Fprint(out, h.PrintC(headerColor, h.Title)) | ||
} | ||
|
||
_, _ = fmt.Fprintln(out) | ||
|
||
topNameColor := aec.NewBuilder(aec.BlueF, aec.Underline, aec.Bold).ANSI | ||
normalColor := aec.NewBuilder(aec.DefaultF).ANSI | ||
normalFaintedColor := aec.NewBuilder(aec.DefaultF).Faint().ANSI | ||
greenColor := aec.NewBuilder(aec.GreenF).ANSI | ||
|
||
printDetails := func(clr aec.ANSI, details imageDetails) { | ||
truncID := stringid.TruncateID(details.ID) | ||
fmt.Fprint(out, headers[1].Print(clr, truncID)) | ||
fmt.Fprint(out, strings.Repeat(" ", spacing)) | ||
|
||
fmt.Fprint(out, headers[2].Print(clr, details.DiskUsage)) | ||
fmt.Fprint(out, strings.Repeat(" ", spacing)) | ||
|
||
if details.Used { | ||
fmt.Fprint(out, headers[3].Print(greenColor, " ✔ ️")) | ||
} else { | ||
fmt.Fprint(out, headers[3].Print(clr, " ")) | ||
} | ||
} | ||
|
||
// Print images | ||
for _, img := range images { | ||
for idx, name := range img.Names { | ||
fmt.Fprint(out, headers[0].Print(topNameColor, name)) | ||
if idx != len(img.Names)-1 { | ||
fmt.Fprintln(out, "") | ||
} | ||
} | ||
fmt.Fprint(out, strings.Repeat(" ", spacing)) | ||
|
||
printDetails(normalColor, img.Details) | ||
|
||
_, _ = fmt.Fprintln(out, "") | ||
for idx, sub := range img.Children { | ||
clr := normalColor | ||
if !sub.Available { | ||
clr = normalFaintedColor | ||
} | ||
|
||
if idx != len(img.Children)-1 { | ||
fmt.Fprint(out, headers[0].Print(clr, "├─ "+sub.Platform)) | ||
} else { | ||
fmt.Fprint(out, headers[0].Print(clr, "└─ "+sub.Platform)) | ||
} | ||
|
||
fmt.Fprint(out, strings.Repeat(" ", spacing)) | ||
printDetails(clr, sub.Details) | ||
|
||
fmt.Fprintln(out, "") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
type header struct { | ||
Title string | ||
Width int | ||
Left bool | ||
} | ||
|
||
func truncateRunes(s string, length int) string { | ||
runes := []rune(s) | ||
if len(runes) > length { | ||
return string(runes[:length]) | ||
} | ||
return s | ||
} | ||
|
||
func (h header) Print(clr aec.ANSI, s string) (out string) { | ||
if h.Left { | ||
return h.PrintL(clr, s) | ||
} | ||
return h.PrintC(clr, s) | ||
} | ||
|
||
func (h header) PrintC(clr aec.ANSI, s string) (out string) { | ||
ln := utf8.RuneCountInString(s) | ||
if h.Left { | ||
return h.PrintL(clr, s) | ||
} | ||
|
||
if ln > h.Width { | ||
return clr.Apply(truncateRunes(s, h.Width)) | ||
} | ||
|
||
fill := h.Width - ln | ||
|
||
l := fill / 2 | ||
r := fill - l | ||
|
||
return strings.Repeat(" ", l) + clr.Apply(s) + strings.Repeat(" ", r) | ||
} | ||
|
||
func (h header) PrintL(clr aec.ANSI, s string) string { | ||
ln := utf8.RuneCountInString(s) | ||
if ln > h.Width { | ||
return clr.Apply(truncateRunes(s, h.Width)) | ||
} | ||
|
||
return clr.Apply(s) + strings.Repeat(" ", h.Width-ln) | ||
} |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
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.
Needs: moby/moby#47501