Support --quiet option

This commit is contained in:
knqyf263
2019-05-07 23:06:57 +09:00
parent 7aa407099c
commit d270edea75
10 changed files with 77 additions and 16 deletions

View File

@@ -73,6 +73,10 @@ OPTIONS:
Name: "clean, c", Name: "clean, c",
Usage: "clean all cache", Usage: "clean all cache",
}, },
cli.BoolFlag{
Name: "quiet, q",
Usage: "suppress progress bar",
},
cli.BoolFlag{ cli.BoolFlag{
Name: "debug, d", Name: "debug, d",
Usage: "debug mode", Usage: "debug mode",

View File

@@ -4,9 +4,7 @@ import (
"os" "os"
"path/filepath" "path/filepath"
"strings" "strings"
"time"
"github.com/briandowns/spinner"
"github.com/knqyf263/trivy/pkg/log" "github.com/knqyf263/trivy/pkg/log"
"github.com/knqyf263/trivy/pkg/utils" "github.com/knqyf263/trivy/pkg/utils"
"golang.org/x/xerrors" "golang.org/x/xerrors"
@@ -38,8 +36,8 @@ func CloneOrPull(url, repoPath string) (map[string]struct{}, error) {
} }
log.Logger.Debug("remove an existed directory") log.Logger.Debug("remove an existed directory")
s := spinner.New(spinner.CharSets[36], 100*time.Millisecond) suffix := " The first time will take a while..."
s.Suffix = " The first time will take a while..." s := utils.NewSpinner(suffix)
s.Start() s.Start()
defer s.Stop() defer s.Stop()

View File

@@ -36,6 +36,8 @@ func Run(c *cli.Context) (err error) {
cli.ShowAppHelpAndExit(c, 1) cli.ShowAppHelpAndExit(c, 1)
} }
utils.Quiet = c.Bool("quiet")
clean := c.Bool("clean") clean := c.Bool("clean")
if clean { if clean {
log.Logger.Info("Cleaning caches...") log.Logger.Info("Cleaning caches...")

63
pkg/utils/progress.go Normal file
View File

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

View File

@@ -3,7 +3,6 @@ package alpine
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/cheggaaa/pb.v1"
"io" "io"
"path/filepath" "path/filepath"
@@ -37,7 +36,7 @@ func Update(dir string, updatedFiles map[string]struct{}) error {
} }
log.Logger.Debugf("Alpine updated files: %d", len(targets)) log.Logger.Debugf("Alpine updated files: %d", len(targets))
bar := pb.StartNew(len(targets)) bar := utils.PbStartNew(len(targets))
defer bar.Finish() defer bar.Finish()
var cves []AlpineCVE var cves []AlpineCVE

View File

@@ -3,7 +3,6 @@ package debianoval
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/cheggaaa/pb.v1"
"io" "io"
"os" "os"
"path/filepath" "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)) log.Logger.Debugf("Debian OVAL updated files: %d", len(targets))
bar := pb.StartNew(len(targets)) bar := utils.PbStartNew(len(targets))
defer bar.Finish() defer bar.Finish()
var cves []DebianOVAL var cves []DebianOVAL

View File

@@ -3,7 +3,6 @@ package debian
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/cheggaaa/pb.v1"
"io" "io"
"path/filepath" "path/filepath"
"strings" "strings"
@@ -45,7 +44,7 @@ func Update(dir string, updatedFiles map[string]struct{}) error {
} }
log.Logger.Debugf("Debian updated files: %d", len(targets)) log.Logger.Debugf("Debian updated files: %d", len(targets))
bar := pb.StartNew(len(targets)) bar := utils.PbStartNew(len(targets))
defer bar.Finish() defer bar.Finish()
var cves []DebianCVE var cves []DebianCVE

View File

@@ -2,7 +2,6 @@ package nvd
import ( import (
"encoding/json" "encoding/json"
"gopkg.in/cheggaaa/pb.v1"
"io" "io"
"path/filepath" "path/filepath"
@@ -35,7 +34,7 @@ func Update(dir string, updatedFiles map[string]struct{}) error {
} }
log.Logger.Debugf("NVD updated files: %d", len(targets)) log.Logger.Debugf("NVD updated files: %d", len(targets))
bar := pb.StartNew(len(targets)) bar := utils.PbStartNew(len(targets))
defer bar.Finish() defer bar.Finish()
var items []vulnerability.Item var items []vulnerability.Item
err = utils.FileWalk(rootDir, targets, func(r io.Reader, _ string) error { err = utils.FileWalk(rootDir, targets, func(r io.Reader, _ string) error {

View File

@@ -3,7 +3,6 @@ package redhat
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/cheggaaa/pb.v1"
"io" "io"
"io/ioutil" "io/ioutil"
"path/filepath" "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)) log.Logger.Debugf("Red Hat updated files: %d", len(targets))
bar := pb.StartNew(len(targets)) bar := utils.PbStartNew(len(targets))
defer bar.Finish() defer bar.Finish()
var cves []RedhatCVE var cves []RedhatCVE

View File

@@ -3,7 +3,6 @@ package ubuntu
import ( import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"gopkg.in/cheggaaa/pb.v1"
"io" "io"
"path/filepath" "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)) log.Logger.Debugf("Ubuntu OVAL updated files: %d", len(targets))
bar := pb.StartNew(len(targets)) bar := utils.PbStartNew(len(targets))
defer bar.Finish() defer bar.Finish()
var cves []UbuntuCVE var cves []UbuntuCVE