feat: allow for stdout to be buffered on each command (#2335)

* feat: add preRun func to version to restore stdout

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>

* test: add test to capture version in output

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>

* change stdout buffering to log to be opt-in per command

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

* fix tests

Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>

---------

Signed-off-by: Christopher Phillips <christopher.phillips@anchore.com>
Signed-off-by: Alex Goodman <wagoodman@users.noreply.github.com>
Co-authored-by: Alex Goodman <wagoodman@users.noreply.github.com>
This commit is contained in:
Christopher Angelo Phillips
2023-11-17 14:14:13 -05:00
committed by GitHub
co-authored by Alex Goodman
parent 1c787f436f
commit ba80e490c2
11 changed files with 62 additions and 75 deletions
+11 -5
View File
@@ -8,13 +8,19 @@ import (
"github.com/anchore/syft/internal/log"
)
// capture replaces the provided *os.File and redirects output to the provided writer. The return value is a function,
// which is used to stop the current capturing of output and restore the original file.
const defaultStdoutLogBufferSize = 1024
// CaptureStdoutToTraceLog replaces stdout and redirects output to the log as trace lines. The return value is a
// function, which is used to stop the current capturing of output and restore the original file.
// Example:
//
// restore := capture(&os.Stderr, writer)
// // here, stderr will be captured and redirected to the provided writer
// restore() // block until the output has all been sent to the writer and restore the original stderr
// restore := CaptureStdoutToTraceLog()
// // here, stdout will be captured and redirected to the provided writer
// restore() // block until the output has all been sent to the writer and restore the original stdout
func CaptureStdoutToTraceLog() (close func()) {
return capture(&os.Stdout, newLogWriter(), defaultStdoutLogBufferSize)
}
func capture(target **os.File, writer io.Writer, bufSize int) (close func()) {
original := *target