Skip to content

Commit

Permalink
Suppress log output when --quiet flag is on (#125)
Browse files Browse the repository at this point in the history
* Add --no-progress flag

* Disable log output when --quiet flag is enabled
  • Loading branch information
mas9612 authored and knqyf263 committed Aug 22, 2019
1 parent 31a1f59 commit a77984a
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 11 deletions.
4 changes: 4 additions & 0 deletions cmd/trivy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ OPTIONS:
},
cli.BoolFlag{
Name: "quiet, q",
Usage: "suppress progress bar and log output",
},
cli.BoolFlag{
Name: "no-progress",
Usage: "suppress progress bar",
},
cli.BoolFlag{
Expand Down
12 changes: 9 additions & 3 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package log

import (
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/xerrors"
Expand All @@ -11,17 +13,17 @@ var (
debugOption bool
)

func InitLogger(debug bool) (err error) {
func InitLogger(debug, disable bool) (err error) {
debugOption = debug
Logger, err = newLogger(debug)
Logger, err = newLogger(debug, disable)
if err != nil {
return xerrors.Errorf("error in new logger: %w", err)
}
return nil

}

func newLogger(debug bool) (*zap.SugaredLogger, error) {
func newLogger(debug, disable bool) (*zap.SugaredLogger, error) {
level := zap.NewAtomicLevel()
if debug {
level.SetLevel(zapcore.DebugLevel)
Expand Down Expand Up @@ -50,6 +52,10 @@ func newLogger(debug bool) (*zap.SugaredLogger, error) {
OutputPaths: []string{"stdout"},
ErrorOutputPaths: []string{"stderr"},
}
if disable {
myConfig.OutputPaths = []string{os.DevNull}
myConfig.ErrorOutputPaths = []string{os.DevNull}
}
logger, err := myConfig.Build()
if err != nil {
return nil, xerrors.Errorf("failed to build zap config: %w", err)
Expand Down
8 changes: 5 additions & 3 deletions pkg/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"os"
"strings"

"github.com/genuinetools/reg/registry"
"github.com/aquasecurity/fanal/cache"
"github.com/aquasecurity/trivy/pkg/db"
"github.com/aquasecurity/trivy/pkg/log"
Expand All @@ -15,16 +14,19 @@ import (
"github.com/aquasecurity/trivy/pkg/utils"
"github.com/aquasecurity/trivy/pkg/vulnsrc"
"github.com/aquasecurity/trivy/pkg/vulnsrc/vulnerability"
"github.com/genuinetools/reg/registry"
"github.com/urfave/cli"
"golang.org/x/xerrors"
)

func Run(c *cli.Context) (err error) {
cliVersion := c.App.Version

utils.Quiet = c.Bool("quiet")
if c.Bool("quiet") || c.Bool("no-progress") {
utils.Quiet = true
}
debug := c.Bool("debug")
if err = log.InitLogger(debug); err != nil {
if err = log.InitLogger(debug, c.Bool("quiet")); err != nil {
l.Fatal(err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/scanner/ospkg/alpine/alpine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMain(m *testing.M) {
log.InitLogger(false)
log.InitLogger(false, false)
os.Exit(m.Run())
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/scanner/ospkg/debian/debian_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMain(m *testing.M) {
log.InitLogger(false)
log.InitLogger(false, false)
os.Exit(m.Run())
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/scanner/ospkg/redhat/redhat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMain(m *testing.M) {
log.InitLogger(false)
log.InitLogger(false, false)
os.Exit(m.Run())
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/scanner/ospkg/ubuntu/ubnutu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestMain(m *testing.M) {
log.InitLogger(false)
log.InitLogger(false, false)
os.Exit(m.Run())
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func write(t *testing.T, name string, content string) {
}

func TestFileWalk(t *testing.T) {
if err := log.InitLogger(false); err != nil {
if err := log.InitLogger(false, false); err != nil {
t.Fatal(err)
}
td, err := ioutil.TempDir("", "walktest")
Expand Down

0 comments on commit a77984a

Please sign in to comment.