Skip to content

Commit

Permalink
feat: configure delay seconds
Browse files Browse the repository at this point in the history
Signed-off-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
knqyf263 committed Nov 6, 2023
1 parent 362871a commit 4972f06
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 30 deletions.
8 changes: 1 addition & 7 deletions pkg/fanal/artifact/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ type Option struct {
LicenseScannerOption analyzer.LicenseScannerOption

// File walk
WalkOption WalkOption
}

// WalkOption is a struct that allows users to define a custom walking behavior.
// This option is only available when using Trivy as an imported library and not through CLI flags.
type WalkOption struct {
ErrorCallback walker.ErrorCallback
WalkOption walker.Option
}

func (o *Option) Sort() {
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/artifact/local/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewArtifact(rootPath string, c cache.ArtifactCache, opt artifact.Option) (a
rootPath: filepath.ToSlash(filepath.Clean(rootPath)),
cache: c,
walker: walker.NewFS(buildPathsToSkip(rootPath, opt.SkipFiles), buildPathsToSkip(rootPath, opt.SkipDirs),
opt.WalkOption.ErrorCallback),
opt.Slow, opt.WalkOption),
analyzer: a,
handlerManager: handlerManager,

Expand Down
27 changes: 16 additions & 11 deletions pkg/fanal/walker/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,23 @@ import (
dio "github.com/aquasecurity/go-dep-parser/pkg/io"
)

const walkDelay = 10 * time.Millisecond

type ErrorCallback func(pathname string, err error) error

// Option is a struct that allows users to define a custom walking behavior.
// This option is only available when using Trivy as an imported library and not through CLI flags.
type Option struct {
ErrorCallback ErrorCallback
Delay time.Duration
}

type FS struct {
walker
errCallback ErrorCallback
option Option
}

func NewFS(skipFiles, skipDirs []string, slow bool, errCallback ErrorCallback) FS {
if errCallback == nil {
errCallback = func(pathname string, err error) error {
func NewFS(skipFiles, skipDirs []string, slow bool, opt Option) FS {
if opt.ErrorCallback == nil {
opt.ErrorCallback = func(pathname string, err error) error {
switch {
// Unwrap fs.SkipDir error
case errors.Is(err, fs.SkipDir):
Expand All @@ -38,8 +43,8 @@ func NewFS(skipFiles, skipDirs []string, slow bool, errCallback ErrorCallback) F
}

return FS{
walker: newWalker(skipFiles, skipDirs, slow),
errCallback: errCallback,
walker: newWalker(skipFiles, skipDirs, slow),
option: opt,
}
}

Expand All @@ -49,7 +54,7 @@ func (w FS) Walk(root string, fn WalkFunc) error {
walkDir := w.walkDirFunc(root, fn)
err := filepath.WalkDir(root, func(filePath string, d fs.DirEntry, err error) error {
if walkErr := walkDir(filePath, d, err); walkErr != nil {
return w.errCallback(filePath, walkErr)
return w.option.ErrorCallback(filePath, walkErr)
}
return nil
})
Expand All @@ -62,11 +67,11 @@ func (w FS) Walk(root string, fn WalkFunc) error {
func (w FS) walkDirFunc(root string, fn WalkFunc) fs.WalkDirFunc {
return func(filePath string, d fs.DirEntry, err error) error {
if err != nil {
return w.errCallback(filePath, err)
return err
}

if w.walker.slow {
time.Sleep(walkDelay)
time.Sleep(w.option.Delay)
}

filePath = filepath.Clean(filePath)
Expand Down
26 changes: 15 additions & 11 deletions pkg/fanal/walker/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import (

func TestDir_Walk(t *testing.T) {
type fields struct {
skipFiles []string
skipDirs []string
errCallback walker.ErrorCallback
skipFiles []string
skipDirs []string
option walker.Option
}
tests := []struct {
name string
Expand Down Expand Up @@ -74,8 +74,10 @@ func TestDir_Walk(t *testing.T) {
name: "ignore all errors",
rootDir: "testdata/fs/nosuch",
fields: fields{
errCallback: func(pathname string, err error) error {
return nil
option: walker.Option{
ErrorCallback: func(pathname string, err error) error {
return nil
},
},
},
analyzeFn: func(string, os.FileInfo, analyzer.Opener) error {
Expand All @@ -86,11 +88,13 @@ func TestDir_Walk(t *testing.T) {
name: "ignore analysis errors",
rootDir: "testdata/fs",
fields: fields{
errCallback: func(pathname string, err error) error {
if errors.Is(err, fs.ErrClosed) {
return nil
}
return err
option: walker.Option{
ErrorCallback: func(pathname string, err error) error {
if errors.Is(err, fs.ErrClosed) {
return nil
}
return err
},
},
},
analyzeFn: func(string, os.FileInfo, analyzer.Opener) error {
Expand All @@ -108,7 +112,7 @@ func TestDir_Walk(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := walker.NewFS(tt.fields.skipFiles, tt.fields.skipDirs, tt.fields.errCallback)
w := walker.NewFS(tt.fields.skipFiles, tt.fields.skipDirs, false, tt.fields.option)

err := w.Walk(tt.rootDir, tt.analyzeFn)
if tt.wantErr != "" {
Expand Down

0 comments on commit 4972f06

Please sign in to comment.