feat(report): considering App.Writer when printing results (#1722)

Co-authored-by: knqyf263 <knqyf263@gmail.com>
This commit is contained in:
Guy Ben-Aharon
2022-02-21 16:47:42 +02:00
committed by GitHub
parent 356ae30c7e
commit 4423396bcc
4 changed files with 10 additions and 7 deletions

View File

@@ -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 {

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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