-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
Copy pathnuget.go
60 lines (46 loc) · 1.4 KB
/
nuget.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package nuget
import (
"context"
"os"
"path/filepath"
"golang.org/x/exp/slices"
"golang.org/x/xerrors"
"github.com/aquasecurity/go-dep-parser/pkg/nuget/config"
"github.com/aquasecurity/go-dep-parser/pkg/nuget/lock"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer"
"github.com/aquasecurity/trivy/pkg/fanal/analyzer/language"
"github.com/aquasecurity/trivy/pkg/fanal/types"
)
func init() {
analyzer.RegisterAnalyzer(&nugetLibraryAnalyzer{})
}
const (
version = 2
lockFile = types.NuGetPkgsLock
configFile = types.NuGetPkgsConfig
)
var requiredFiles = []string{lockFile, configFile}
type nugetLibraryAnalyzer struct{}
func (a nugetLibraryAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisInput) (*analyzer.AnalysisResult, error) {
// Set the default parser
parser := lock.NewParser()
targetFile := filepath.Base(input.FilePath)
if targetFile == configFile {
parser = config.NewParser()
}
res, err := language.Analyze(types.NuGet, input.FilePath, input.Content, parser)
if err != nil {
return nil, xerrors.Errorf("NuGet analysis error: %w", err)
}
return res, nil
}
func (a nugetLibraryAnalyzer) Required(filePath string, _ os.FileInfo) bool {
fileName := filepath.Base(filePath)
return slices.Contains(requiredFiles, fileName)
}
func (a nugetLibraryAnalyzer) Type() analyzer.Type {
return analyzer.TypeNuget
}
func (a nugetLibraryAnalyzer) Version() int {
return version
}