Skip to content

Commit

Permalink
fix(dotnet): show nuget package dir not found log only when checkin…
Browse files Browse the repository at this point in the history
…g `nuget` packages (#7194)

Co-authored-by: knqyf263 <knqyf263@gmail.com>
  • Loading branch information
DmitriyLewen and knqyf263 authored Jul 25, 2024
1 parent 8d5ba3f commit d76feba
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 7 deletions.
6 changes: 6 additions & 0 deletions pkg/fanal/analyzer/language/dotnet/nuget/nuget.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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/log"
"github.com/aquasecurity/trivy/pkg/utils/fsutils"
)

Expand All @@ -39,19 +40,24 @@ type nugetLibraryAnalyzer struct {
lockParser language.Parser
configParser language.Parser
licenseParser nuspecParser
logger *log.Logger
}

func newNugetLibraryAnalyzer(_ analyzer.AnalyzerOptions) (analyzer.PostAnalyzer, error) {
return &nugetLibraryAnalyzer{
lockParser: lock.NewParser(),
configParser: config.NewParser(),
licenseParser: newNuspecParser(),
logger: log.WithPrefix("nuget"),
}, nil
}

func (a *nugetLibraryAnalyzer) PostAnalyze(_ context.Context, input analyzer.PostAnalysisInput) (*analyzer.AnalysisResult, error) {
var apps []types.Application
foundLicenses := make(map[string][]string)
if a.licenseParser.packagesDir == "" {
a.logger.Debug("The nuget packages directory couldn't be found. License search disabled")
}

// We saved only config and lock files in the FS,
// so we need to parse all saved files
Expand Down
2 changes: 1 addition & 1 deletion pkg/fanal/analyzer/language/dotnet/nuget/nuget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/aquasecurity/trivy/pkg/fanal/types"
)

func Test_nugetibraryAnalyzer_Analyze(t *testing.T) {
func Test_nugetLibraryAnalyzer_Analyze(t *testing.T) {
tests := []struct {
name string
dir string
Expand Down
6 changes: 0 additions & 6 deletions pkg/fanal/analyzer/language/dotnet/nuget/nuspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"golang.org/x/xerrors"

"github.com/aquasecurity/trivy/pkg/log"
"github.com/aquasecurity/trivy/pkg/utils/fsutils"
)

Expand All @@ -30,26 +29,21 @@ type License struct {
}

type nuspecParser struct {
logger *log.Logger
packagesDir string // global packages folder - https: //learn.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders
}

func newNuspecParser() nuspecParser {
logger := log.WithPrefix("nuget")

// cf. https: //learn.microsoft.com/en-us/nuget/consume-packages/managing-the-global-packages-and-cache-folders
packagesDir := os.Getenv("NUGET_PACKAGES")
if packagesDir == "" {
packagesDir = filepath.Join(os.Getenv("HOME"), ".nuget", "packages")
}

if !fsutils.DirExists(packagesDir) {
logger.Debug("The nuget packages directory couldn't be found. License search disabled")
return nuspecParser{}
}

return nuspecParser{
logger: logger,
packagesDir: packagesDir,
}
}
Expand Down

0 comments on commit d76feba

Please sign in to comment.