From de8001d088ea7131e480301e792f6292de294f2e Mon Sep 17 00:00:00 2001 From: Emily McMullan Date: Mon, 4 Nov 2024 10:22:55 -0500 Subject: [PATCH] Cherry-pick #358: Fallback for detecting java projects (#367) fallback for detecting java projects Signed-off-by: Emily McMullan --- cmd/analyze.go | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/cmd/analyze.go b/cmd/analyze.go index a3ce65b..fad6f51 100644 --- a/cmd/analyze.go +++ b/cmd/analyze.go @@ -244,10 +244,19 @@ func NewAnalyzeCmd(log logr.Logger) *cobra.Command { return err } // alizer does not detect certain files such as xml - // in this case we need to run only the analyzer to use builtin provider + // in this case, we can first check for a java project + // if not found, only start builtin provider if len(foundProviders) == 0 { - analyzeCmd.needsBuiltin = true - return analyzeCmd.RunAnalysis(ctx, xmlOutputDir, analyzeCmd.input) + foundJava, err := analyzeCmd.detectJavaProviderFallback() + if err != nil { + return err + } + if foundJava { + foundProviders = append(foundProviders, javaProvider) + } else { + analyzeCmd.needsBuiltin = true + return analyzeCmd.RunAnalysis(ctx, xmlOutputDir, analyzeCmd.input) + } } err = analyzeCmd.setProviderInitInfo(foundProviders) @@ -2312,3 +2321,33 @@ func (a *analyzeCommand) analyzeDotnetFramework(ctx context.Context) error { return nil } + +func (a *analyzeCommand) detectJavaProviderFallback() (bool, error) { + a.log.V(7).Info("language files not found. Using fallback") + pomPath := filepath.Join(a.input, "pom.xml") + _, err := os.Stat(pomPath) + // some other error + if err != nil && !errors.Is(err, os.ErrNotExist) { + return false, err + } + if err == nil { + return true, nil + } + // try gradle next + gradlePath := filepath.Join(a.input, "build.gradle") + _, err = os.Stat(gradlePath) + // some other error + if err != nil && !errors.Is(err, os.ErrNotExist) { + return false, err + + // java project not found + } else if err != nil && errors.Is(err, os.ErrNotExist) { + a.log.V(7).Info("language files not found. Only starting builtin provider") + return false, nil + + } else if err == nil { + return true, nil + } + + return false, nil +}