Skip to content

Commit 44db82d

Browse files
committed
cmd/image/tree: refactor
Signed-off-by: Laura Brehm <laurabrehm@hey.com>
1 parent 32ff200 commit 44db82d

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

cli/command/image/tree.go

+31-20
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/containerd/platforms"
1111
"github.com/docker/cli/cli/command"
12-
"github.com/docker/cli/cli/streams"
1312
"github.com/docker/docker/api/types/filters"
1413
imagetypes "github.com/docker/docker/api/types/image"
1514
"github.com/docker/docker/pkg/stringid"
@@ -230,64 +229,76 @@ func printImageTree(dockerCLI command.Cli, view treeView) error {
230229

231230
// Print images
232231
for _, img := range images {
233-
printNames(out, columns, img, topNameColor, untaggedColor)
234-
printDetails(out, columns, normalColor, img.Details)
235-
236-
if len(img.Children) > 0 || view.imageSpacing {
237-
_, _ = fmt.Fprintln(out)
238-
}
239-
printChildren(out, columns, img, normalColor)
232+
_, _ = fmt.Fprint(out, formatImageTree(img, columns, view.imageSpacing, normalColor, topNameColor, untaggedColor))
240233
_, _ = fmt.Fprintln(out)
241234
}
242235

243236
return nil
244237
}
245238

246-
func printDetails(out *streams.Out, headers []imgColumn, defaultColor aec.ANSI, details imageDetails) {
239+
func formatImageTree(img topImage, columns []imgColumn, imgSpacing bool, normalColor, topNameColor, untaggedColor aec.ANSI) (out string) {
240+
out += printNames(columns, img, topNameColor, untaggedColor)
241+
out += printDetails(columns, normalColor, img.Details)
242+
243+
if len(img.Children) > 0 || imgSpacing {
244+
out += "\n"
245+
}
246+
out += printChildren(columns, img, normalColor)
247+
248+
return out
249+
}
250+
251+
func printDetails(headers []imgColumn, defaultColor aec.ANSI, details imageDetails) (out string) {
247252
for _, h := range headers {
248253
if h.DetailsValue == nil {
249254
continue
250255
}
251256

252-
_, _ = fmt.Fprint(out, strings.Repeat(" ", columnSpacing))
257+
out += strings.Repeat(" ", columnSpacing)
253258
clr := defaultColor
254259
if h.Color != nil {
255260
clr = *h.Color
256261
}
257262
val := h.DetailsValue(&details)
258-
_, _ = fmt.Fprint(out, h.Print(clr, val))
263+
out += h.Print(clr, val)
259264
}
265+
266+
return out
260267
}
261268

262-
func printChildren(out *streams.Out, headers []imgColumn, img topImage, normalColor aec.ANSI) {
269+
func printChildren(headers []imgColumn, img topImage, normalColor aec.ANSI) (out string) {
263270
for idx, sub := range img.Children {
264271
clr := normalColor
265272
if !sub.Available {
266273
clr = normalColor.With(aec.Faint)
267274
}
268275

269276
if idx != len(img.Children)-1 {
270-
_, _ = fmt.Fprint(out, headers[0].Print(clr, "├─ "+sub.Platform))
277+
out += headers[0].Print(clr, "├─ "+sub.Platform)
271278
} else {
272-
_, _ = fmt.Fprint(out, headers[0].Print(clr, "└─ "+sub.Platform))
279+
out += headers[0].Print(clr, "└─ "+sub.Platform)
273280
}
274281

275-
printDetails(out, headers, clr, sub.Details)
276-
_, _ = fmt.Fprintln(out, "")
282+
out += printDetails(headers, clr, sub.Details)
283+
out += "\n"
277284
}
285+
286+
return out
278287
}
279288

280-
func printNames(out *streams.Out, headers []imgColumn, img topImage, color, untaggedColor aec.ANSI) {
289+
func printNames(headers []imgColumn, img topImage, color, untaggedColor aec.ANSI) (out string) {
281290
if len(img.Names) == 0 {
282-
_, _ = fmt.Fprint(out, headers[0].Print(untaggedColor, "<untagged>"))
291+
out += headers[0].Print(untaggedColor, "<untagged>")
283292
}
284293

285294
for nameIdx, name := range img.Names {
286295
if nameIdx != 0 {
287-
_, _ = fmt.Fprintln(out, "")
296+
out += "\n"
288297
}
289-
_, _ = fmt.Fprint(out, headers[0].Print(color, name))
298+
out += headers[0].Print(color, name)
290299
}
300+
301+
return out
291302
}
292303

293304
type alignment int

0 commit comments

Comments
 (0)