Skip to content

Commit 9ae6b48

Browse files
committed
image/tree: No legend if stdout redirected
Signed-off-by: Paweł Gronowski <[email protected]>
1 parent c44e8a0 commit 9ae6b48

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

cli/command/image/list_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,14 @@ func TestNewImagesCommandSuccess(t *testing.T) {
8080
},
8181
},
8282
}
83+
84+
prevForceStreamRedirected := testForceStreamRedirected
85+
t.Cleanup(func() {
86+
testForceStreamRedirected = prevForceStreamRedirected
87+
})
88+
testForceStreamRedirected = new(bool)
89+
*testForceStreamRedirected = false
90+
8391
for _, tc := range testCases {
8492
t.Run(tc.name, func(t *testing.T) {
8593
cli := test.NewFakeCli(&fakeClient{imageListFunc: tc.imageListFunc})

cli/command/image/tree.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ func getPossibleChips(view treeView) (chips []imageChip) {
225225
}
226226

227227
func printImageTree(outs command.Streams, view treeView) {
228-
if streamRedirected(outs.Out()) {
228+
stdoutRedirected := streamRedirected(outs.Out())
229+
if stdoutRedirected {
229230
_, _ = fmt.Fprintln(outs.Err(), "WARNING: This output is designed for human readability. For machine-readable output, please use --format.")
230231
}
231232

@@ -245,7 +246,10 @@ func printImageTree(outs command.Streams, view treeView) {
245246

246247
isTerm := out.IsTerminal()
247248

248-
out.Println(generateLegend(out, width))
249+
// Don't print legend when output is redirected
250+
if !stdoutRedirected {
251+
out.Println(generateLegend(out, width))
252+
}
249253

250254
possibleChips := getPossibleChips(view)
251255
columns := []imgColumn{
@@ -517,7 +521,12 @@ func widestFirstColumnValue(headers []imgColumn, images []topImage) int {
517521
return width
518522
}
519523

524+
var testForceStreamRedirected *bool
525+
520526
func streamRedirected(s *streams.Out) bool {
527+
if testForceStreamRedirected != nil {
528+
return *testForceStreamRedirected
529+
}
521530
fd := s.FD()
522531
if os.Stdout.Fd() != fd {
523532
return true

cli/command/image/tree_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ func TestPrintImageTreeAnsiTty(t *testing.T) {
109109
imageSpacing: false,
110110
}
111111

112+
prevForceStreamRedirected := testForceStreamRedirected
113+
t.Cleanup(func() {
114+
testForceStreamRedirected = prevForceStreamRedirected
115+
})
116+
testForceStreamRedirected = new(bool)
117+
*testForceStreamRedirected = false
118+
112119
for _, tc := range testCases {
113120
t.Run(tc.name, func(t *testing.T) {
114121
cli := test.NewFakeCli(nil)

0 commit comments

Comments
 (0)