From 55436a27692cdb40c888ffe029b7dd4552072f0a Mon Sep 17 00:00:00 2001 From: Michael Stringer Date: Tue, 18 Jun 2024 09:31:37 +0100 Subject: [PATCH] Switch from PostAnalyzer to Analyzer --- .../analyzer/language/java/sbt/lockfile.go | 45 ++++--------------- .../language/java/sbt/lockfile_test.go | 28 +++++++----- 2 files changed, 24 insertions(+), 49 deletions(-) diff --git a/pkg/fanal/analyzer/language/java/sbt/lockfile.go b/pkg/fanal/analyzer/language/java/sbt/lockfile.go index 75592c3738de..26efcb1034a0 100644 --- a/pkg/fanal/analyzer/language/java/sbt/lockfile.go +++ b/pkg/fanal/analyzer/language/java/sbt/lockfile.go @@ -2,8 +2,6 @@ package sbt import ( "context" - "io" - "io/fs" "os" "path/filepath" @@ -13,11 +11,10 @@ import ( "github.com/aquasecurity/trivy/pkg/fanal/analyzer" "github.com/aquasecurity/trivy/pkg/fanal/analyzer/language" "github.com/aquasecurity/trivy/pkg/fanal/types" - "github.com/aquasecurity/trivy/pkg/utils/fsutils" ) func init() { - analyzer.RegisterPostAnalyzer(analyzer.TypeSbtLock, newSbtDependencyLockAnalyzer) + analyzer.RegisterAnalyzer(&sbtDependencyLockAnalyzer{}) } const ( @@ -26,45 +23,19 @@ const ( ) // sbtDependencyLockAnalyzer analyzes '*.sbt.lock' -type sbtDependencyLockAnalyzer struct { - parser language.Parser -} - -func newSbtDependencyLockAnalyzer(_ analyzer.AnalyzerOptions) (analyzer.PostAnalyzer, error) { - return &sbtDependencyLockAnalyzer{ - parser: lockfile.NewParser(), - }, nil -} - -func (a sbtDependencyLockAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnalysisInput) (*analyzer.AnalysisResult, error) { - required := func(path string, d fs.DirEntry) bool { - return a.Required(path, nil) - } - - var apps []types.Application - var err error - err = fsutils.WalkDir(input.FS, ".", required, func(filePath string, _ fs.DirEntry, r io.Reader) error { - var app *types.Application - app, err = language.Parse(types.Sbt, filePath, r, a.parser) - if err != nil { - return xerrors.Errorf("%s parse error: %w", filePath, err) - } - - if app != nil { +type sbtDependencyLockAnalyzer struct{} - apps = append(apps, *app) - } +func (a sbtDependencyLockAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) { + filePath := filepath.Join(input.Dir, input.FilePath) + parser := lockfile.NewParser() - return nil - }) + res, err := language.Analyze(types.Sbt, filePath, input.Content, parser) if err != nil { - return nil, xerrors.Errorf("sbt walk error: %w", err) + return nil, xerrors.Errorf("%s parse error: %w", filePath, err) } - return &analyzer.AnalysisResult{ - Applications: apps, - }, nil + return res, nil } func (a sbtDependencyLockAnalyzer) Required(filePath string, _ os.FileInfo) bool { diff --git a/pkg/fanal/analyzer/language/java/sbt/lockfile_test.go b/pkg/fanal/analyzer/language/java/sbt/lockfile_test.go index 66a370775118..0469d9156a19 100644 --- a/pkg/fanal/analyzer/language/java/sbt/lockfile_test.go +++ b/pkg/fanal/analyzer/language/java/sbt/lockfile_test.go @@ -14,18 +14,18 @@ import ( func Test_sbtDependencyLockAnalyzer(t *testing.T) { tests := []struct { - name string - dir string - want *analyzer.AnalysisResult + name string + inputFile string + want *analyzer.AnalysisResult }{ { - name: "v1 lockfile", - dir: "testdata/v1", + name: "v1 lockfile", + inputFile: "testdata/v1/build.sbt.lock", want: &analyzer.AnalysisResult{ Applications: []types.Application{ { Type: types.Sbt, - FilePath: "build.sbt.lock", + FilePath: "testdata/v1/build.sbt.lock", Packages: types.Packages{ { ID: "org.apache.commons:commons-lang3:3.9", @@ -66,19 +66,23 @@ func Test_sbtDependencyLockAnalyzer(t *testing.T) { }, }, { - name: "empty lockfile", - dir: "testdata/empty", - want: &analyzer.AnalysisResult{}, + name: "empty lockfile", + inputFile: "testdata/empty/build.sbt.lock", + want: nil, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - a, err := newSbtDependencyLockAnalyzer(analyzer.AnalyzerOptions{}) + f, err := os.Open(tt.inputFile) require.NoError(t, err) - got, err := a.PostAnalyze(context.Background(), analyzer.PostAnalysisInput{ - FS: os.DirFS(tt.dir), + a := sbtDependencyLockAnalyzer{} + ctx := context.Background() + + got, err := a.Analyze(ctx, analyzer.AnalysisInput{ + FilePath: tt.inputFile, + Content: f, }) require.NoError(t, err)