diff --git a/cmd/trivy/main.go b/cmd/trivy/main.go index da420e03be..f42c356081 100644 --- a/cmd/trivy/main.go +++ b/cmd/trivy/main.go @@ -73,6 +73,10 @@ OPTIONS: Name: "clean, c", Usage: "clean all cache", }, + cli.BoolFlag{ + Name: "quiet, q", + Usage: "suppress progress bar", + }, cli.BoolFlag{ Name: "debug, d", Usage: "debug mode", diff --git a/pkg/git/git.go b/pkg/git/git.go index b696e3e068..d576175359 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -4,9 +4,7 @@ import ( "os" "path/filepath" "strings" - "time" - "github.com/briandowns/spinner" "github.com/knqyf263/trivy/pkg/log" "github.com/knqyf263/trivy/pkg/utils" "golang.org/x/xerrors" @@ -38,8 +36,8 @@ func CloneOrPull(url, repoPath string) (map[string]struct{}, error) { } log.Logger.Debug("remove an existed directory") - s := spinner.New(spinner.CharSets[36], 100*time.Millisecond) - s.Suffix = " The first time will take a while..." + suffix := " The first time will take a while..." + s := utils.NewSpinner(suffix) s.Start() defer s.Stop() diff --git a/pkg/run.go b/pkg/run.go index 30086132a8..4a37b4b6d3 100644 --- a/pkg/run.go +++ b/pkg/run.go @@ -36,6 +36,8 @@ func Run(c *cli.Context) (err error) { cli.ShowAppHelpAndExit(c, 1) } + utils.Quiet = c.Bool("quiet") + clean := c.Bool("clean") if clean { log.Logger.Info("Cleaning caches...") diff --git a/pkg/utils/progress.go b/pkg/utils/progress.go new file mode 100644 index 0000000000..b01bd9dc19 --- /dev/null +++ b/pkg/utils/progress.go @@ -0,0 +1,63 @@ +package utils + +import ( + "time" + + "github.com/briandowns/spinner" + pb "gopkg.in/cheggaaa/pb.v1" +) + +var ( + Quiet = false +) + +type Spinner struct { + client *spinner.Spinner +} + +func NewSpinner(suffix string) *Spinner { + if Quiet { + return &Spinner{} + } + s := spinner.New(spinner.CharSets[36], 100*time.Millisecond) + s.Suffix = suffix + return &Spinner{client: s} +} + +func (s *Spinner) Start() { + if s.client == nil { + return + } + s.client.Start() +} +func (s *Spinner) Stop() { + if s.client == nil { + return + } + s.client.Stop() +} + +type ProgressBar struct { + client *pb.ProgressBar +} + +func PbStartNew(total int) *ProgressBar { + if Quiet { + return &ProgressBar{} + } + bar := pb.StartNew(total) + return &ProgressBar{client: bar} +} + +func (p *ProgressBar) Increment() { + if p.client == nil { + return + } + p.client.Increment() +} +func (p *ProgressBar) Finish() { + if p.client == nil { + return + } + p.client.Finish() +} diff --git a/pkg/vulnsrc/alpine/alpine.go b/pkg/vulnsrc/alpine/alpine.go index 00ae22d59b..f69db0aa49 100644 --- a/pkg/vulnsrc/alpine/alpine.go +++ b/pkg/vulnsrc/alpine/alpine.go @@ -3,7 +3,6 @@ package alpine import ( "encoding/json" "fmt" - "gopkg.in/cheggaaa/pb.v1" "io" "path/filepath" @@ -37,7 +36,7 @@ func Update(dir string, updatedFiles map[string]struct{}) error { } log.Logger.Debugf("Alpine updated files: %d", len(targets)) - bar := pb.StartNew(len(targets)) + bar := utils.PbStartNew(len(targets)) defer bar.Finish() var cves []AlpineCVE diff --git a/pkg/vulnsrc/debian-oval/debian-oval.go b/pkg/vulnsrc/debian-oval/debian-oval.go index 02c4f99672..27b9a8b01c 100644 --- a/pkg/vulnsrc/debian-oval/debian-oval.go +++ b/pkg/vulnsrc/debian-oval/debian-oval.go @@ -3,7 +3,6 @@ package debianoval import ( "encoding/json" "fmt" - "gopkg.in/cheggaaa/pb.v1" "io" "os" "path/filepath" @@ -39,7 +38,7 @@ func Update(dir string, updatedFiles map[string]struct{}) error { } log.Logger.Debugf("Debian OVAL updated files: %d", len(targets)) - bar := pb.StartNew(len(targets)) + bar := utils.PbStartNew(len(targets)) defer bar.Finish() var cves []DebianOVAL diff --git a/pkg/vulnsrc/debian/debian.go b/pkg/vulnsrc/debian/debian.go index ad39cf793f..92abdf70e9 100644 --- a/pkg/vulnsrc/debian/debian.go +++ b/pkg/vulnsrc/debian/debian.go @@ -3,7 +3,6 @@ package debian import ( "encoding/json" "fmt" - "gopkg.in/cheggaaa/pb.v1" "io" "path/filepath" "strings" @@ -45,7 +44,7 @@ func Update(dir string, updatedFiles map[string]struct{}) error { } log.Logger.Debugf("Debian updated files: %d", len(targets)) - bar := pb.StartNew(len(targets)) + bar := utils.PbStartNew(len(targets)) defer bar.Finish() var cves []DebianCVE diff --git a/pkg/vulnsrc/nvd/nvd.go b/pkg/vulnsrc/nvd/nvd.go index 3daeb114e2..6c344be055 100644 --- a/pkg/vulnsrc/nvd/nvd.go +++ b/pkg/vulnsrc/nvd/nvd.go @@ -2,7 +2,6 @@ package nvd import ( "encoding/json" - "gopkg.in/cheggaaa/pb.v1" "io" "path/filepath" @@ -35,7 +34,7 @@ func Update(dir string, updatedFiles map[string]struct{}) error { } log.Logger.Debugf("NVD updated files: %d", len(targets)) - bar := pb.StartNew(len(targets)) + bar := utils.PbStartNew(len(targets)) defer bar.Finish() var items []vulnerability.Item err = utils.FileWalk(rootDir, targets, func(r io.Reader, _ string) error { diff --git a/pkg/vulnsrc/redhat/redhat.go b/pkg/vulnsrc/redhat/redhat.go index 89cbaa0fd2..b9b04ab3ed 100644 --- a/pkg/vulnsrc/redhat/redhat.go +++ b/pkg/vulnsrc/redhat/redhat.go @@ -3,7 +3,6 @@ package redhat import ( "encoding/json" "fmt" - "gopkg.in/cheggaaa/pb.v1" "io" "io/ioutil" "path/filepath" @@ -41,7 +40,7 @@ func Update(dir string, updatedFiles map[string]struct{}) error { } log.Logger.Debugf("Red Hat updated files: %d", len(targets)) - bar := pb.StartNew(len(targets)) + bar := utils.PbStartNew(len(targets)) defer bar.Finish() var cves []RedhatCVE diff --git a/pkg/vulnsrc/ubuntu/ubuntu.go b/pkg/vulnsrc/ubuntu/ubuntu.go index c6a344bbec..f5aba032ae 100644 --- a/pkg/vulnsrc/ubuntu/ubuntu.go +++ b/pkg/vulnsrc/ubuntu/ubuntu.go @@ -3,7 +3,6 @@ package ubuntu import ( "encoding/json" "fmt" - "gopkg.in/cheggaaa/pb.v1" "io" "path/filepath" @@ -56,7 +55,7 @@ func Update(dir string, updatedFiles map[string]struct{}) error { } log.Logger.Debugf("Ubuntu OVAL updated files: %d", len(targets)) - bar := pb.StartNew(len(targets)) + bar := utils.PbStartNew(len(targets)) defer bar.Finish() var cves []UbuntuCVE