Skip to content

Commit

Permalink
refactor: use early return
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitriyLewen committed Jun 27, 2024
1 parent e3539b6 commit d75bce2
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 17 deletions.
37 changes: 20 additions & 17 deletions pkg/fanal/analyzer/language/conda/environment/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,28 @@ func (a environmentAnalyzer) Analyze(_ context.Context, input analyzer.AnalysisI
return nil, xerrors.Errorf("unable to parse environment.yaml: %w", err)
}

if res != nil && len(res.Applications) > 0 {
once := sync.Once{}
// For `environment.yaml` Applications always contains only 1 Application
for i, pkg := range res.Applications[0].Packages {
// Skip packages without a version, because in this case we will not be able to get the correct file name.
if pkg.Version != "" {
licenses, err := findLicenseFromEnvDir(pkg)
if err != nil {
// Show log once per file
once.Do(func() {
log.WithPrefix("conda").Debug("License not found. For more information, see https://aquasecurity.github.io/trivy/latest/docs/coverage/os/conda/#licenses",
log.String("file", input.FilePath), log.String("pkg", pkg.Name), log.Err(err))
})
}
pkg.Licenses = licenses
if res == nil {
return nil, nil
}

once := sync.Once{}
// res always contains only 1 Application
// cf. https://github.com/aquasecurity/trivy/blob/0ccdbfbb6598a52de7cda603ab22e794f710e86c/pkg/fanal/analyzer/language/analyze.go#L32
for i, pkg := range res.Applications[0].Packages {
// Skip packages without a version, because in this case we will not be able to get the correct file name.
if pkg.Version != "" {
licenses, err := findLicenseFromEnvDir(pkg)
if err != nil {
// Show log once per file
once.Do(func() {
log.WithPrefix("conda").Debug("License not found. For more information, see https://aquasecurity.github.io/trivy/latest/docs/coverage/os/conda/#licenses",
log.String("file", input.FilePath), log.String("pkg", pkg.Name), log.Err(err))
})
}
pkg.FilePath = "" // remove `prefix` from FilePath
res.Applications[0].Packages[i] = pkg
pkg.Licenses = licenses
}
pkg.FilePath = "" // remove `prefix` from FilePath
res.Applications[0].Packages[i] = pkg

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ func Test_environmentAnalyzer_Analyze(t *testing.T) {
},
},
},
{
name: "empty",
inputFile: "testdata/empty.yaml",
},
{
name: "invalid",
inputFile: "testdata/invalid.yaml",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: test-env
channels:
- defaults
dependencies:
prefix: /opt/conda/envs/test-env

0 comments on commit d75bce2

Please sign in to comment.