Skip to content

Commit 292001a

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 0b1c7bc commit 292001a

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
@@ -298,6 +298,24 @@ func RunStats(ctx context.Context, dockerCLI command.Cli, options *StatsOptions)
298298
Format: NewStatsFormat(format, daemonOSType),
299299
}
300300

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

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

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

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

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

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

0 commit comments

Comments
 (0)