Skip to content

Commit

Permalink
Switch from PostAnalyzer to Analyzer
Browse files Browse the repository at this point in the history
  • Loading branch information
stringbean committed Jun 18, 2024
1 parent fdff10e commit 55436a2
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 49 deletions.
45 changes: 8 additions & 37 deletions pkg/fanal/analyzer/language/java/sbt/lockfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package sbt

import (
"context"
"io"
"io/fs"
"os"
"path/filepath"

Expand All @@ -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 (
Expand All @@ -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 {
Expand Down
28 changes: 16 additions & 12 deletions pkg/fanal/analyzer/language/java/sbt/lockfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 55436a2

Please sign in to comment.