Skip to content

Commit

Permalink
Support --quiet option
Browse files Browse the repository at this point in the history
  • Loading branch information
knqyf263 committed May 7, 2019
1 parent 7aa4070 commit d270ede
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 16 deletions.
4 changes: 4 additions & 0 deletions cmd/trivy/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 2 additions & 0 deletions pkg/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...")
Expand Down
63 changes: 63 additions & 0 deletions pkg/utils/progress.go
Original file line number Diff line number Diff line change
@@ -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()
}
3 changes: 1 addition & 2 deletions pkg/vulnsrc/alpine/alpine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package alpine
import (
"encoding/json"
"fmt"
"gopkg.in/cheggaaa/pb.v1"
"io"
"path/filepath"

Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/vulnsrc/debian-oval/debian-oval.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package debianoval
import (
"encoding/json"
"fmt"
"gopkg.in/cheggaaa/pb.v1"
"io"
"os"
"path/filepath"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/vulnsrc/debian/debian.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package debian
import (
"encoding/json"
"fmt"
"gopkg.in/cheggaaa/pb.v1"
"io"
"path/filepath"
"strings"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/vulnsrc/nvd/nvd.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package nvd

import (
"encoding/json"
"gopkg.in/cheggaaa/pb.v1"
"io"
"path/filepath"

Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions pkg/vulnsrc/redhat/redhat.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package redhat
import (
"encoding/json"
"fmt"
"gopkg.in/cheggaaa/pb.v1"
"io"
"io/ioutil"
"path/filepath"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions pkg/vulnsrc/ubuntu/ubuntu.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ubuntu
import (
"encoding/json"
"fmt"
"gopkg.in/cheggaaa/pb.v1"
"io"
"path/filepath"

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d270ede

Please sign in to comment.