Skip to content

Commit 937fc4e

Browse files
refactoring runTools to be more generic
1 parent b78ef4d commit 937fc4e

File tree

1 file changed

+55
-292
lines changed

1 file changed

+55
-292
lines changed

cmd/analyze.go

Lines changed: 55 additions & 292 deletions
Original file line numberDiff line numberDiff line change
@@ -317,291 +317,71 @@ func getToolName(toolName string, version string) string {
317317
return toolName
318318
}
319319

320-
func runEslintAnalysis(workDirectory string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) error {
321-
// Ensure ESLint tool is configured and installed
322-
eslint := config.Config.Tools()["eslint"]
323-
isToolInstalled := config.Config.IsToolInstalled("eslint", eslint)
324-
325-
// Also check if the runtime is installed
326-
var isRuntimeInstalled bool
327-
if eslint != nil {
328-
nodeRuntime := config.Config.Runtimes()["node"]
329-
isRuntimeInstalled = nodeRuntime != nil && config.Config.IsRuntimeInstalled("node", nodeRuntime)
330-
}
331-
332-
if eslint == nil || !isToolInstalled || !isRuntimeInstalled {
333-
if eslint == nil {
334-
fmt.Println("Eslint tool configuration not found, adding and installing...")
335-
} else if !isToolInstalled {
336-
fmt.Println("Eslint tool is not installed, installing...")
337-
} else if !isRuntimeInstalled {
338-
fmt.Println("Node.js runtime is not installed, installing eslint (which will install the runtime)...")
339-
}
340-
341-
err := config.InstallTool("eslint", eslint, "")
342-
if err != nil {
343-
return fmt.Errorf("failed to install eslint: %w", err)
344-
}
345-
346-
// Get the updated tool info after installation
347-
eslint = config.Config.Tools()["eslint"]
348-
if eslint == nil {
349-
return fmt.Errorf("eslint tool configuration still not found after installation")
350-
}
351-
fmt.Println("Eslint tool installed successfully")
352-
}
353-
354-
// Ensure node runtime is available
355-
nodeRuntime := config.Config.Runtimes()["node"]
356-
if nodeRuntime == nil {
357-
return fmt.Errorf("node runtime not found - this should not happen after eslint installation")
358-
}
359-
360-
nodeBinary := nodeRuntime.Binaries["node"]
361-
if nodeBinary == "" {
362-
return fmt.Errorf("node binary not found in runtime configuration")
363-
}
364-
365-
// Run ESLint
366-
eslintInstallationDirectory := eslint.InstallDir
367-
return tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, pathsToCheck, autoFix, outputFile, outputFormat)
368-
}
369-
370-
func runTrivyAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
371-
// Ensure Trivy tool is configured and installed
372-
trivy := config.Config.Tools()["trivy"]
373-
isToolInstalled := config.Config.IsToolInstalled("trivy", trivy)
374-
375-
// Trivy is a download-based tool (no runtime dependency), so runtime is always "installed"
376-
isRuntimeInstalled := true
377-
378-
if trivy == nil || !isToolInstalled || !isRuntimeInstalled {
379-
if trivy == nil {
380-
fmt.Println("Trivy tool configuration not found, adding and installing...")
381-
} else if !isToolInstalled {
382-
fmt.Println("Trivy tool is not installed, installing...")
383-
}
384-
385-
err := config.InstallTool("trivy", trivy, "")
386-
if err != nil {
387-
return fmt.Errorf("failed to install trivy: %w", err)
388-
}
389-
390-
// Get the updated tool info after installation
391-
trivy = config.Config.Tools()["trivy"]
392-
if trivy == nil {
393-
return fmt.Errorf("trivy tool configuration still not found after installation")
394-
}
395-
fmt.Println("Trivy tool installed successfully")
396-
}
397-
398-
// Ensure trivy binary is available
399-
trivyBinary := trivy.Binaries["trivy"]
400-
if trivyBinary == "" {
401-
return fmt.Errorf("trivy binary not found in tool configuration")
402-
}
403-
404-
// Run Trivy
405-
return tools.RunTrivy(workDirectory, trivyBinary, pathsToCheck, outputFile, outputFormat)
406-
}
407-
408-
func runPmdAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
409-
// Ensure PMD tool is configured and installed
410-
pmd := config.Config.Tools()["pmd"]
411-
isToolInstalled := config.Config.IsToolInstalled("pmd", pmd)
412-
413-
// Also check if the runtime is installed
414-
var isRuntimeInstalled bool
415-
if pmd != nil {
416-
javaRuntime := config.Config.Runtimes()["java"]
417-
isRuntimeInstalled = javaRuntime != nil && config.Config.IsRuntimeInstalled("java", javaRuntime)
418-
}
419-
420-
if pmd == nil || !isToolInstalled || !isRuntimeInstalled {
421-
if pmd == nil {
422-
fmt.Println("PMD tool configuration not found, adding and installing...")
423-
} else if !isToolInstalled {
424-
fmt.Println("PMD tool is not installed, installing...")
425-
} else if !isRuntimeInstalled {
426-
fmt.Println("Java runtime is not installed, installing PMD (which will install the runtime)...")
427-
}
428-
429-
err := config.InstallTool("pmd", pmd, "")
430-
if err != nil {
431-
return fmt.Errorf("failed to install pmd: %w", err)
432-
}
433-
434-
// Get the updated tool info after installation
435-
pmd = config.Config.Tools()["pmd"]
436-
if pmd == nil {
437-
return fmt.Errorf("pmd tool configuration still not found after installation")
438-
}
439-
fmt.Println("PMD tool installed successfully")
440-
}
441-
442-
// Ensure Java runtime is available
443-
javaRuntime := config.Config.Runtimes()["java"]
444-
if javaRuntime == nil {
445-
return fmt.Errorf("java runtime not found - this should not happen after pmd installation")
446-
}
447-
448-
// Ensure pmd binary is available
449-
pmdBinary := pmd.Binaries["pmd"]
450-
if pmdBinary == "" {
451-
return fmt.Errorf("pmd binary not found in tool configuration")
452-
}
453-
454-
// Run PMD
455-
return tools.RunPmd(workDirectory, pmdBinary, pathsToCheck, outputFile, outputFormat, config.Config)
456-
}
457-
458-
func runPylintAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
459-
// Ensure Pylint tool is configured and installed
460-
pylint := config.Config.Tools()["pylint"]
461-
isToolInstalled := config.Config.IsToolInstalled("pylint", pylint)
462-
463-
// Also check if the runtime is installed
464-
var isRuntimeInstalled bool
465-
if pylint != nil {
466-
pythonRuntime := config.Config.Runtimes()["python"]
467-
isRuntimeInstalled = pythonRuntime != nil && config.Config.IsRuntimeInstalled("python", pythonRuntime)
468-
}
469-
470-
if pylint == nil || !isToolInstalled || !isRuntimeInstalled {
471-
if pylint == nil {
472-
fmt.Println("Pylint tool configuration not found, adding and installing...")
473-
} else if !isToolInstalled {
474-
fmt.Println("Pylint tool is not installed, installing...")
475-
} else if !isRuntimeInstalled {
476-
fmt.Println("Python runtime is not installed, installing Pylint (which will install the runtime)...")
477-
}
478-
479-
err := config.InstallTool("pylint", pylint, "")
480-
if err != nil {
481-
return fmt.Errorf("failed to install pylint: %w", err)
482-
}
483-
484-
// Get the updated tool info after installation
485-
pylint = config.Config.Tools()["pylint"]
486-
if pylint == nil {
487-
return fmt.Errorf("pylint tool configuration still not found after installation")
488-
}
489-
fmt.Println("Pylint tool installed successfully")
490-
}
491-
492-
// Ensure Python runtime is available
493-
pythonRuntime := config.Config.Runtimes()["python"]
494-
if pythonRuntime == nil {
495-
return fmt.Errorf("python runtime not found - this should not happen after pylint installation")
496-
}
497-
498-
// Ensure python binary is available
499-
pylintBinary := pylint.Binaries["python"]
500-
if pylintBinary == "" {
501-
return fmt.Errorf("python binary not found in pylint tool configuration")
320+
func runToolByTooName(toolName string, workDirectory string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string, tool *plugins.ToolInfo, runtime *plugins.RuntimeInfo) error {
321+
switch toolName {
322+
case "eslint":
323+
return tools.RunEslint(workDirectory, tool.InstallDir, runtime.Binaries[tool.Runtime], pathsToCheck, autoFix, outputFile, outputFormat)
324+
case "trivy":
325+
return tools.RunTrivy(workDirectory, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
326+
case "pmd":
327+
return tools.RunPmd(workDirectory, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat, config.Config)
328+
case "pylint":
329+
return tools.RunPylint(workDirectory, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
330+
case "dartanalyzer":
331+
return tools.RunDartAnalyzer(workDirectory, tool.InstallDir, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
332+
case "semgrep":
333+
return tools.RunSemgrep(workDirectory, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
334+
case "lizard":
335+
return runLizardAnalysis(workDirectory, pathsToCheck, outputFile, outputFormat)
336+
case "enigma":
337+
return tools.RunEnigma(workDirectory, tool.InstallDir, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
502338
}
503-
504-
// Run Pylint
505-
return tools.RunPylint(workDirectory, pylintBinary, pathsToCheck, outputFile, outputFormat)
339+
return fmt.Errorf("unsupported tool: %s", toolName)
506340
}
507341

508-
func runDartAnalyzer(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
509-
// Ensure Dart Analyzer tool is configured and installed
510-
dartanalyzer := config.Config.Tools()["dartanalyzer"]
511-
isToolInstalled := config.Config.IsToolInstalled("dartanalyzer", dartanalyzer)
512-
513-
// Also check if the runtime is installed
342+
func genericRunTool(toolName string, workDirectory string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) error {
343+
tool := config.Config.Tools()[toolName]
344+
isToolInstalled := config.Config.IsToolInstalled(toolName, tool)
514345
var isRuntimeInstalled bool
515-
if dartanalyzer != nil {
516-
dartRuntime := config.Config.Runtimes()["dart"]
517-
isRuntimeInstalled = dartRuntime != nil && config.Config.IsRuntimeInstalled("dart", dartRuntime)
518-
}
519-
520-
if dartanalyzer == nil || !isToolInstalled || !isRuntimeInstalled {
521-
if dartanalyzer == nil {
522-
fmt.Println("Dart analyzer tool configuration not found, adding and installing...")
523-
} else if !isToolInstalled {
524-
fmt.Println("Dart analyzer tool is not installed, installing...")
525-
} else if !isRuntimeInstalled {
526-
fmt.Println("Dart runtime is not installed, installing Dart Analyzer (which will install the runtime)...")
527-
}
528346

529-
err := config.InstallTool("dartanalyzer", dartanalyzer, "")
347+
var runtime *plugins.RuntimeInfo
348+
if tool == nil || !isToolInstalled {
349+
fmt.Println("Tool configuration not found, adding and installing...")
350+
err := config.InstallTool(toolName, tool, "")
530351
if err != nil {
531-
return fmt.Errorf("failed to install dartanalyzer: %w", err)
532-
}
533-
534-
// Get the updated tool info after installation
535-
dartanalyzer = config.Config.Tools()["dartanalyzer"]
536-
if dartanalyzer == nil {
537-
return fmt.Errorf("dartanalyzer tool configuration still not found after installation")
538-
}
539-
fmt.Println("Dart analyzer tool installed successfully")
540-
}
541-
542-
// Ensure Dart runtime is available (dart or flutter)
543-
if config.Config.Runtimes()["flutter"] == nil && config.Config.Runtimes()["dart"] == nil {
544-
return fmt.Errorf("dart or flutter runtime not found - this should not happen after dartanalyzer installation")
545-
}
546-
547-
// Ensure dart binary is available
548-
dartBinary := dartanalyzer.Binaries["dart"]
549-
if dartBinary == "" {
550-
return fmt.Errorf("dart binary not found in dartanalyzer tool configuration")
551-
}
552-
553-
// Run Dart Analyzer
554-
return tools.RunDartAnalyzer(workDirectory, dartanalyzer.InstallDir, dartBinary, pathsToCheck, outputFile, outputFormat)
555-
}
556-
557-
func runSemgrepAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
558-
// Ensure Semgrep tool is configured and installed
559-
semgrep := config.Config.Tools()["semgrep"]
560-
isToolInstalled := config.Config.IsToolInstalled("semgrep", semgrep)
561-
562-
// Also check if the runtime is installed
563-
var isRuntimeInstalled bool
564-
if semgrep != nil {
565-
pythonRuntime := config.Config.Runtimes()["python"]
566-
isRuntimeInstalled = pythonRuntime != nil && config.Config.IsRuntimeInstalled("python", pythonRuntime)
567-
}
568-
569-
if semgrep == nil || !isToolInstalled || !isRuntimeInstalled {
570-
if semgrep == nil {
571-
fmt.Println("Semgrep tool configuration not found, adding and installing...")
572-
} else if !isToolInstalled {
573-
fmt.Println("Semgrep tool is not installed, installing...")
574-
} else if !isRuntimeInstalled {
575-
fmt.Println("Python runtime is not installed, installing Semgrep (which will install the runtime)...")
352+
return fmt.Errorf("failed to install %s: %w", toolName, err)
576353
}
354+
tool = config.Config.Tools()[toolName]
355+
isToolInstalled = config.Config.IsToolInstalled(toolName, tool)
577356

578-
err := config.InstallTool("semgrep", semgrep, "")
579-
if err != nil {
580-
return fmt.Errorf("failed to install semgrep: %w", err)
357+
runtime = config.Config.Runtimes()[tool.Runtime]
358+
isRuntimeInstalled = runtime == nil || config.Config.IsRuntimeInstalled(tool.Runtime, runtime)
359+
if !isRuntimeInstalled {
360+
fmt.Println("Runtime is not installed, installing...")
361+
err := config.InstallRuntime(tool.Runtime, runtime)
362+
if err != nil {
363+
return fmt.Errorf("failed to install %s runtime: %w", tool.Runtime, err)
364+
}
365+
runtime = config.Config.Runtimes()[tool.Runtime]
366+
isRuntimeInstalled = config.Config.IsRuntimeInstalled(tool.Runtime, runtime)
581367
}
582368

583-
// Get the updated tool info after installation
584-
semgrep = config.Config.Tools()["semgrep"]
585-
if semgrep == nil {
586-
return fmt.Errorf("semgrep tool configuration still not found after installation")
369+
} else {
370+
runtime = config.Config.Runtimes()[tool.Runtime]
371+
isRuntimeInstalled = runtime == nil || config.Config.IsRuntimeInstalled(tool.Runtime, runtime)
372+
if !isRuntimeInstalled {
373+
fmt.Println("Runtime is not installed, installing...")
374+
err := config.InstallRuntime(tool.Runtime, runtime)
375+
if err != nil {
376+
return fmt.Errorf("failed to install %s runtime: %w", tool.Runtime, err)
377+
}
378+
runtime = config.Config.Runtimes()[tool.Runtime]
379+
isRuntimeInstalled = config.Config.IsRuntimeInstalled(tool.Runtime, runtime)
587380
}
588-
fmt.Println("Semgrep tool installed successfully")
589-
}
590-
591-
// Ensure Python runtime is available
592-
pythonRuntime := config.Config.Runtimes()["python"]
593-
if pythonRuntime == nil {
594-
return fmt.Errorf("python runtime not found - this should not happen after semgrep installation")
595-
}
596381

597-
// Ensure semgrep binary is available
598-
semgrepBinary := semgrep.Binaries["semgrep"]
599-
if semgrepBinary == "" {
600-
return fmt.Errorf("semgrep binary not found in tool configuration")
601382
}
602383

603-
// Run Semgrep
604-
return tools.RunSemgrep(workDirectory, semgrepBinary, pathsToCheck, outputFile, outputFormat)
384+
return runToolByTooName(toolName, workDirectory, pathsToCheck, autoFix, outputFile, outputFormat, tool, runtime)
605385
}
606386

607387
func runLizardAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
@@ -814,26 +594,9 @@ var analyzeCmd = &cobra.Command{
814594
}
815595

816596
func runTool(workDirectory string, toolName string, args []string, outputFile string) error {
817-
switch toolName {
818-
case "eslint":
819-
return runEslintAnalysis(workDirectory, args, autoFix, outputFile, outputFormat)
820-
case "trivy":
821-
return runTrivyAnalysis(workDirectory, args, outputFile, outputFormat)
822-
case "pmd":
823-
return runPmdAnalysis(workDirectory, args, outputFile, outputFormat)
824-
case "pylint":
825-
return runPylintAnalysis(workDirectory, args, outputFile, outputFormat)
826-
case "semgrep":
827-
return runSemgrepAnalysis(workDirectory, args, outputFile, outputFormat)
828-
case "dartanalyzer":
829-
return runDartAnalyzer(workDirectory, args, outputFile, outputFormat)
830-
case "lizard":
831-
return runLizardAnalysis(workDirectory, args, outputFile, outputFormat)
832-
case "codacy-enigma-cli":
833-
return runEnigmaAnalysis(workDirectory, args, outputFile, outputFormat)
834-
case "revive":
835-
return runReviveAnalysis(workDirectory, args, outputFile, outputFormat)
836-
default:
837-
return fmt.Errorf("unsupported tool: %s", toolName)
597+
err := genericRunTool(toolName, workDirectory, args, autoFix, outputFile, outputFormat)
598+
if err != nil {
599+
return err
838600
}
601+
return nil
839602
}

0 commit comments

Comments
 (0)