Skip to content

Commit 0c6d17b

Browse files
committed
cli/command/container: RunStats: early return for non-streaming
We should consider splitting this out to a separate function, but start with just an early return before we hit the timer-loop. Signed-off-by: Sebastiaan van Stijn <[email protected]>
1 parent 5a4d697 commit 0c6d17b

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

cli/command/container/stats.go

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,24 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
297297
Format: NewStatsFormat(format, daemonOSType),
298298
}
299299

300+
if options.NoStream {
301+
cStats.mu.RLock()
302+
ccStats := make([]StatsEntry, 0, len(cStats.cs))
303+
for _, c := range cStats.cs {
304+
ccStats = append(ccStats, c.GetStatistics())
305+
}
306+
cStats.mu.RUnlock()
307+
308+
if len(ccStats) == 0 {
309+
return nil
310+
}
311+
if err := statsFormatWrite(statsCtx, ccStats, daemonOSType, !options.NoTrunc); err != nil {
312+
return err
313+
}
314+
_, _ = fmt.Fprint(dockerCLI.Out(), statsTextBuffer.String())
315+
return nil
316+
}
317+
300318
ticker := time.NewTicker(500 * time.Millisecond)
301319
defer ticker.Stop()
302320
for {
@@ -309,34 +327,27 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
309327
}
310328
cStats.mu.RUnlock()
311329

312-
if !options.NoStream {
313-
// Start by moving the cursor to the top-left
314-
_, _ = fmt.Fprint(&statsTextBuffer, "\033[H")
315-
}
330+
// Start by moving the cursor to the top-left
331+
_, _ = fmt.Fprint(&statsTextBuffer, "\033[H")
316332

317333
if err := statsFormatWrite(statsCtx, ccStats, daemonOSType, !options.NoTrunc); err != nil {
318334
return err
319335
}
320336

321-
if !options.NoStream {
322-
for _, line := range strings.Split(statsTextBuffer.String(), "\n") {
323-
// In case the new text is shorter than the one we are writing over,
324-
// we'll append the "erase line" escape sequence to clear the remaining text.
325-
_, _ = fmt.Fprintln(&statsTextBuffer, line, "\033[K")
326-
}
327-
// We might have fewer containers than before, so let's clear the remaining text
328-
_, _ = fmt.Fprint(&statsTextBuffer, "\033[J")
337+
for _, line := range strings.Split(statsTextBuffer.String(), "\n") {
338+
// In case the new text is shorter than the one we are writing over,
339+
// we'll append the "erase line" escape sequence to clear the remaining text.
340+
_, _ = fmt.Fprintln(&statsTextBuffer, line, "\033[K")
329341
}
342+
// We might have fewer containers than before, so let's clear the remaining text
343+
_, _ = fmt.Fprint(&statsTextBuffer, "\033[J")
330344

331345
_, _ = fmt.Fprint(dockerCLI.Out(), statsTextBuffer.String())
332346
statsTextBuffer.Reset()
333347

334348
if len(ccStats) == 0 && !showAll {
335349
return nil
336350
}
337-
if options.NoStream {
338-
return nil
339-
}
340351
case err, ok := <-closeChan:
341352
if !ok || err == nil || errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) {
342353
// Suppress "unexpected EOF" errors in the CLI so that

0 commit comments

Comments
 (0)