mirror of
https://github.com/aquasecurity/trivy.git
synced 2025-12-23 07:29:00 -08:00
feat(report): considering App.Writer when printing results (#1722)
Co-authored-by: knqyf263 <knqyf263@gmail.com>
This commit is contained in:
@@ -60,7 +60,7 @@ func (c *Option) Init() error {
|
||||
}
|
||||
|
||||
func (c *Option) initPreScanOptions() error {
|
||||
if err := c.ReportOption.Init(c.Logger); err != nil {
|
||||
if err := c.ReportOption.Init(c.Context.App.Writer, c.Logger); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.DBOption.Init(); err != nil {
|
||||
|
||||
@@ -69,7 +69,7 @@ func (c *Option) Init() (err error) {
|
||||
c.CustomHeaders.Set(c.tokenHeader, c.token)
|
||||
}
|
||||
|
||||
if err = c.ReportOption.Init(c.Logger); err != nil {
|
||||
if err = c.ReportOption.Init(c.Context.App.Writer, c.Logger); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package option
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
@@ -32,7 +33,7 @@ type ReportOption struct {
|
||||
// these variables are populated by Init()
|
||||
VulnType []string
|
||||
SecurityChecks []string
|
||||
Output *os.File
|
||||
Output io.Writer
|
||||
Severities []dbTypes.Severity
|
||||
}
|
||||
|
||||
@@ -54,7 +55,7 @@ func NewReportOption(c *cli.Context) ReportOption {
|
||||
}
|
||||
|
||||
// Init initializes the ReportOption
|
||||
func (c *ReportOption) Init(logger *zap.SugaredLogger) error {
|
||||
func (c *ReportOption) Init(output io.Writer, logger *zap.SugaredLogger) error {
|
||||
var err error
|
||||
|
||||
if c.Template != "" {
|
||||
@@ -83,13 +84,15 @@ func (c *ReportOption) Init(logger *zap.SugaredLogger) error {
|
||||
c.vulnType = ""
|
||||
c.securityChecks = ""
|
||||
|
||||
c.Output = os.Stdout
|
||||
// The output is os.Stdout by default
|
||||
if c.output != "" {
|
||||
if c.Output, err = os.Create(c.output); err != nil {
|
||||
if output, err = os.Create(c.output); err != nil {
|
||||
return xerrors.Errorf("failed to create an output file: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
c.Output = output
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -154,7 +154,7 @@ func TestReportReportConfig_Init(t *testing.T) {
|
||||
Output: tt.fields.Output,
|
||||
}
|
||||
|
||||
err := c.Init(logger.Sugar())
|
||||
err := c.Init(os.Stdout, logger.Sugar())
|
||||
|
||||
// tests log messages
|
||||
var gotMessages []string
|
||||
|
||||
Reference in New Issue
Block a user