From e793735ce8ddac823287e4fa3d8c464645052f81 Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Wed, 13 Nov 2024 08:10:59 +0100 Subject: [PATCH 1/2] Add --bulk to containerless analysis Adding --bulk option to analyze applications into the same static-report (similar as the default containerized versions). Fixes: https://issues.redhat.com/projects/MTA/issues/MTA-4205 Signed-off-by: Marek Aufart --- cmd/analyze-bin.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/cmd/analyze-bin.go b/cmd/analyze-bin.go index 0dc3297..bc8bb64 100644 --- a/cmd/analyze-bin.go +++ b/cmd/analyze-bin.go @@ -11,6 +11,7 @@ import ( "path/filepath" "runtime" "sort" + "strings" "sync" "github.com/bombsimon/logrusr/v3" @@ -533,16 +534,37 @@ func (a *analyzeCommand) DependencyOutputContainerless(ctx context.Context, prov func (a *analyzeCommand) buildStaticReportFile(ctx context.Context, staticReportPath string, depsErr bool) error { // Prepare report args list with single input analysis - applicationName := []string{filepath.Base(a.input)} - outputAnalysis := []string{filepath.Join(a.output, "output.yaml")} + applicationNames := []string{filepath.Base(a.input)} + outputAnalyses := []string{filepath.Join(a.output, "output.yaml")} outputDeps := []string{filepath.Join(a.output, "dependencies.yaml")} outputJSPath := filepath.Join(staticReportPath, "output.js") + if a.bulk { + // Scan all available analysis output files to be reported + applicationNames = nil + outputAnalyses = nil + outputDeps = nil + outputFiles, err := filepath.Glob(filepath.Join(a.output, "output.yaml.*")) + // optional + outputDeps, _ := filepath.Glob(filepath.Join(a.output, "dependencies.yaml.*")) + if err != nil { + return err + } + for i := range outputFiles { + outputName := filepath.Base(outputFiles[i]) + applicationName := strings.SplitN(outputName, "output.yaml.", 2)[1] + applicationNames = append(applicationNames, applicationName) + outputAnalyses = append(outputAnalyses, outputFiles[i]) + outputDeps = append(outputDeps, fmt.Sprintf("%s.%s", DepsOutputMountPath, applicationName)) + } + + } + if depsErr { outputDeps = []string{} } // create output.js file from analysis output.yaml - apps, err := validateFlags(outputAnalysis, applicationName, outputDeps, a.log) + apps, err := validateFlags(outputAnalyses, applicationNames, outputDeps, a.log) if err != nil { log.Fatalln("failed to validate flags", err) } @@ -595,8 +617,12 @@ func (a *analyzeCommand) GenerateStaticReportContainerless(ctx context.Context) return noDepFileErr } - staticReportAanlyzePath := filepath.Join(a.kantraDir, "static-report") - err = a.buildStaticReportFile(ctx, staticReportAanlyzePath, errors.Is(noDepFileErr, os.ErrNotExist)) + if a.bulk { + a.moveResults() + } + + staticReportAnalyzePath := filepath.Join(a.kantraDir, "static-report") + err = a.buildStaticReportFile(ctx, staticReportAnalyzePath, errors.Is(noDepFileErr, os.ErrNotExist)) if err != nil { return err } From 007651da4a27f843c6968356c0a3164c748822c1 Mon Sep 17 00:00:00 2001 From: Marek Aufart Date: Wed, 13 Nov 2024 15:23:52 +0100 Subject: [PATCH 2/2] Fix outputDeps and situation when missing for just some apps Signed-off-by: Marek Aufart --- cmd/analyze-bin.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/cmd/analyze-bin.go b/cmd/analyze-bin.go index bc8bb64..391196f 100644 --- a/cmd/analyze-bin.go +++ b/cmd/analyze-bin.go @@ -545,8 +545,6 @@ func (a *analyzeCommand) buildStaticReportFile(ctx context.Context, staticReport outputAnalyses = nil outputDeps = nil outputFiles, err := filepath.Glob(filepath.Join(a.output, "output.yaml.*")) - // optional - outputDeps, _ := filepath.Glob(filepath.Join(a.output, "dependencies.yaml.*")) if err != nil { return err } @@ -555,7 +553,12 @@ func (a *analyzeCommand) buildStaticReportFile(ctx context.Context, staticReport applicationName := strings.SplitN(outputName, "output.yaml.", 2)[1] applicationNames = append(applicationNames, applicationName) outputAnalyses = append(outputAnalyses, outputFiles[i]) - outputDeps = append(outputDeps, fmt.Sprintf("%s.%s", DepsOutputMountPath, applicationName)) + deps := fmt.Sprintf("%s.%s", filepath.Join(a.output, "dependencies.yaml"), applicationName) + // If deps for given application are missing, empty the deps path allowing skip it in static-report + if _, err := os.Stat(deps); errors.Is(err, os.ErrNotExist) { + deps = "" + } + outputDeps = append(outputDeps, deps) } }