From dec5d3b1658a48aca44170cd3eed13558d17cf0c Mon Sep 17 00:00:00 2001 From: DjinnS Date: Sun, 25 Aug 2019 03:56:42 +0200 Subject: [PATCH] No warning(s) in the output file with -quiet option. Report command (#885) --- commands/report.go | 3 +++ config/config.go | 1 + report/util.go | 4 ++++ util/logutil.go | 13 ++++++++++++- 4 files changed, 20 insertions(+), 1 deletion(-) diff --git a/commands/report.go b/commands/report.go index 9f9847cb..42bdeabc 100644 --- a/commands/report.go +++ b/commands/report.go @@ -87,6 +87,7 @@ func (*ReportCmd) Usage() string { [-http-proxy=http://192.168.0.1:8080] [-debug] [-debug-sql] + [-quiet] [-pipe] [-cvedb-type=sqlite3|mysql|postgres|redis|http] [-cvedb-sqlite3-path=/path/to/cve.sqlite3] @@ -112,6 +113,8 @@ func (p *ReportCmd) SetFlags(f *flag.FlagSet) { f.BoolVar(&c.Conf.Debug, "debug", false, "debug mode") f.BoolVar(&c.Conf.DebugSQL, "debug-sql", false, "SQL debug mode") + f.BoolVar(&c.Conf.Quiet, "quiet", false, "Quiet mode. No output on stdout") + wd, _ := os.Getwd() defaultConfPath := filepath.Join(wd, "config.toml") f.StringVar(&p.configPath, "config", defaultConfPath, "/path/to/toml") diff --git a/config/config.go b/config/config.go index be3e1eed..ccd3bc8c 100644 --- a/config/config.go +++ b/config/config.go @@ -106,6 +106,7 @@ type Config struct { LogDir string `json:"logDir,omitempty"` ResultsDir string `json:"resultsDir,omitempty"` Pipe bool `json:"pipe,omitempty"` + Quiet bool `json:"quiet,omitempty"` Default ServerInfo `json:"default,omitempty"` Servers map[string]ServerInfo `json:"servers,omitempty"` diff --git a/report/util.go b/report/util.go index a8e200f2..3da25a3a 100644 --- a/report/util.go +++ b/report/util.go @@ -104,6 +104,10 @@ func formatOneLineSummary(rs ...models.ScanResult) string { r.FormatServerName(), r.Warnings)) } } + // We don't want warning message to the summary file + if config.Conf.Quiet { + return fmt.Sprintf("%s\n", table) + } return fmt.Sprintf("%s\n\n%s", table, strings.Join( warnMsgs, "\n\n")) } diff --git a/util/logutil.go b/util/logutil.go index e9b4d7b0..ec460388 100644 --- a/util/logutil.go +++ b/util/logutil.go @@ -44,7 +44,6 @@ func init() { func NewCustomLogger(c config.ServerInfo) *logrus.Entry { log := logrus.New() log.Formatter = &formatter.TextFormatter{MsgAnsiColor: c.LogMsgAnsiColor} - log.Out = os.Stderr log.Level = logrus.InfoLevel if config.Conf.Debug { log.Level = logrus.DebugLevel @@ -62,6 +61,18 @@ func NewCustomLogger(c config.ServerInfo) *logrus.Entry { } } + // Only log to a file if quiet mode enabled + if config.Conf.Quiet { + logFile := logDir + "/vuls.log" + if file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0644); err == nil { + log.Out = file + } else { + log.Errorf("Failed to create log file. path: %s, err: %s", logFile, err) + } + } else { + log.Out = os.Stderr + } + whereami := "localhost" if 0 < len(c.ServerName) { whereami = c.GetServerName()