Skip to content

Commit cbbc86a

Browse files
committed
image/list: Show collapsed tree by default
Use the new tree view by default and only fallback if format or old view-related options are used. The expanded view is shown when `--tree` is passed. Signed-off-by: Paweł Gronowski <[email protected]>
1 parent 171a9b7 commit cbbc86a

File tree

2 files changed

+53
-26
lines changed

2 files changed

+53
-26
lines changed

cli/command/image/list.go

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,23 +85,16 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
8585
filters.Add("reference", options.matchName)
8686
}
8787

88-
if options.tree {
89-
if options.quiet {
90-
return errors.New("--quiet is not yet supported with --tree")
91-
}
92-
if options.noTrunc {
93-
return errors.New("--no-trunc is not yet supported with --tree")
94-
}
95-
if options.showDigests {
96-
return errors.New("--show-digest is not yet supported with --tree")
97-
}
98-
if options.format != "" {
99-
return errors.New("--format is not yet supported with --tree")
100-
}
88+
useTree, err := shouldUseTree(options)
89+
if err != nil {
90+
return err
91+
}
10192

93+
if useTree {
10294
return runTree(ctx, dockerCLI, treeOptions{
103-
all: options.all,
104-
filters: filters,
95+
all: options.all,
96+
filters: filters,
97+
expanded: options.tree,
10598
})
10699
}
107100

@@ -139,6 +132,34 @@ func runImages(ctx context.Context, dockerCLI command.Cli, options imagesOptions
139132
return nil
140133
}
141134

135+
func shouldUseTree(options imagesOptions) (bool, error) {
136+
if options.quiet {
137+
if options.tree {
138+
return false, errors.New("--quiet is not yet supported with --tree")
139+
}
140+
return false, nil
141+
}
142+
if options.noTrunc {
143+
if options.tree {
144+
return false, errors.New("--no-trunc is not yet supported with --tree")
145+
}
146+
return false, nil
147+
}
148+
if options.showDigests {
149+
if options.tree {
150+
return false, errors.New("--show-digest is not yet supported with --tree")
151+
}
152+
return false, nil
153+
}
154+
if options.format != "" {
155+
if options.tree {
156+
return false, errors.New("--format is not yet supported with --tree")
157+
}
158+
return false, nil
159+
}
160+
return true, nil
161+
}
162+
142163
// isDangling is a copy of [formatter.isDangling].
143164
func isDangling(img image.Summary) bool {
144165
if len(img.RepoTags) == 0 && len(img.RepoDigests) == 0 {

cli/command/image/tree.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,9 @@ import (
2424
)
2525

2626
type treeOptions struct {
27-
all bool
28-
filters client.Filters
27+
all bool
28+
filters client.Filters
29+
expanded bool
2930
}
3031

3132
type treeView struct {
@@ -54,7 +55,7 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
5455
attested := make(map[digest.Digest]bool)
5556

5657
for _, img := range images {
57-
details := imageDetails{
58+
topDetails := imageDetails{
5859
ID: img.ID,
5960
DiskUsage: units.HumanSizeWithPrecision(float64(img.Size), 3),
6061
InUse: img.Containers > 0,
@@ -73,33 +74,38 @@ func runTree(ctx context.Context, dockerCLI command.Cli, opts treeOptions) error
7374
continue
7475
}
7576

77+
inUse := len(im.ImageData.Containers) > 0
78+
if inUse {
79+
// Mark top-level parent image as used if any of its subimages are used.
80+
topDetails.InUse = true
81+
}
82+
83+
if !opts.expanded {
84+
continue
85+
}
86+
7687
sub := subImage{
7788
Platform: platforms.Format(im.ImageData.Platform),
7889
Available: im.Available,
7990
Details: imageDetails{
8091
ID: im.ID,
8192
DiskUsage: units.HumanSizeWithPrecision(float64(im.Size.Total), 3),
82-
InUse: len(im.ImageData.Containers) > 0,
93+
InUse: inUse,
8394
ContentSize: units.HumanSizeWithPrecision(float64(im.Size.Content), 3),
8495
},
8596
}
8697

87-
if sub.Details.InUse {
88-
// Mark top-level parent image as used if any of its subimages are used.
89-
details.InUse = true
90-
}
91-
9298
children = append(children, sub)
9399

94100
// Add extra spacing between images if there's at least one entry with children.
95101
view.imageSpacing = true
96102
}
97103

98-
details.ContentSize = units.HumanSizeWithPrecision(float64(totalContent), 3)
104+
topDetails.ContentSize = units.HumanSizeWithPrecision(float64(totalContent), 3)
99105

100106
view.images = append(view.images, topImage{
101107
Names: img.RepoTags,
102-
Details: details,
108+
Details: topDetails,
103109
Children: children,
104110
created: img.Created,
105111
})

0 commit comments

Comments
 (0)