diff --git a/pkg/fanal/analyzer/language/dotnet/nuget/nuget.go b/pkg/fanal/analyzer/language/dotnet/nuget/nuget.go index fa0cc486def1..b7400048edac 100644 --- a/pkg/fanal/analyzer/language/dotnet/nuget/nuget.go +++ b/pkg/fanal/analyzer/language/dotnet/nuget/nuget.go @@ -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" ) @@ -39,6 +40,7 @@ type nugetLibraryAnalyzer struct { lockParser language.Parser configParser language.Parser licenseParser nuspecParser + logger *log.Logger } func newNugetLibraryAnalyzer(_ analyzer.AnalyzerOptions) (analyzer.PostAnalyzer, error) { @@ -46,12 +48,16 @@ func newNugetLibraryAnalyzer(_ analyzer.AnalyzerOptions) (analyzer.PostAnalyzer, 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 diff --git a/pkg/fanal/analyzer/language/dotnet/nuget/nuget_test.go b/pkg/fanal/analyzer/language/dotnet/nuget/nuget_test.go index 52e10ad5622d..ea105cc99e91 100644 --- a/pkg/fanal/analyzer/language/dotnet/nuget/nuget_test.go +++ b/pkg/fanal/analyzer/language/dotnet/nuget/nuget_test.go @@ -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 diff --git a/pkg/fanal/analyzer/language/dotnet/nuget/nuspec.go b/pkg/fanal/analyzer/language/dotnet/nuget/nuspec.go index 6f25c91e565a..98def24ab8d2 100644 --- a/pkg/fanal/analyzer/language/dotnet/nuget/nuspec.go +++ b/pkg/fanal/analyzer/language/dotnet/nuget/nuspec.go @@ -9,7 +9,6 @@ import ( "golang.org/x/xerrors" - "github.com/aquasecurity/trivy/pkg/log" "github.com/aquasecurity/trivy/pkg/utils/fsutils" ) @@ -30,13 +29,10 @@ 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 == "" { @@ -44,12 +40,10 @@ func newNuspecParser() nuspecParser { } 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, } }