Files
syft/cmd/syft/internal/ui/log_writer.go
T
Christopher Angelo PhillipsandAlex Goodman ba80e490c2 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>
2023-11-17 14:14:13 -05:00

38 lines
587 B
Go

package ui
import (
"bufio"
"bytes"
"io"
"strings"
"github.com/anchore/syft/internal/log"
)
func newLogWriter() io.Writer {
l := logWriter{}
l.r = bufio.NewReader(&l.buf)
return &l
}
type logWriter struct {
buf bytes.Buffer
r *bufio.Reader
}
func (l *logWriter) Write(data []byte) (n int, err error) {
l.buf.Write(data)
s, err := l.r.ReadString('\n')
s = strings.TrimRight(s, "\n")
for s != "" {
log.Trace("[unexpected stdout] " + s)
n += len(s)
if err != nil {
break
}
s, err = l.r.ReadString('\n')
s = strings.TrimRight(s, "\n")
}
return n, err
}