Skip to content

Commit 38d0e65

Browse files
not fail when analysing tool/runtime that is not installed
1 parent 73c0a88 commit 38d0e65

File tree

4 files changed

+351
-42
lines changed

4 files changed

+351
-42
lines changed

cmd/analyze.go

Lines changed: 239 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -318,71 +318,268 @@ func getToolName(toolName string, version string) string {
318318
}
319319

320320
func runEslintAnalysis(workDirectory string, pathsToCheck []string, autoFix bool, outputFile string, outputFormat string) error {
321+
// Ensure ESLint tool is configured and installed
321322
eslint := config.Config.Tools()["eslint"]
322-
eslintInstallationDirectory := eslint.InstallDir
323+
if eslint == nil || !config.Config.IsToolInstalled("eslint", eslint) {
324+
if eslint == nil {
325+
fmt.Println("Eslint tool configuration not found, adding and installing...")
326+
} else {
327+
fmt.Println("Eslint tool is not installed, installing...")
328+
}
329+
330+
err := config.InstallTool("eslint", eslint, "")
331+
if err != nil {
332+
return fmt.Errorf("failed to install eslint: %w", err)
333+
}
334+
335+
// Get the updated tool info after installation
336+
eslint = config.Config.Tools()["eslint"]
337+
if eslint == nil {
338+
return fmt.Errorf("eslint tool configuration still not found after installation")
339+
}
340+
fmt.Println("Eslint tool installed successfully")
341+
}
342+
343+
// Ensure node runtime is available
323344
nodeRuntime := config.Config.Runtimes()["node"]
345+
if nodeRuntime == nil {
346+
return fmt.Errorf("node runtime not found - this should not happen after eslint installation")
347+
}
348+
324349
nodeBinary := nodeRuntime.Binaries["node"]
350+
if nodeBinary == "" {
351+
return fmt.Errorf("node binary not found in runtime configuration")
352+
}
325353

354+
// Run ESLint
355+
eslintInstallationDirectory := eslint.InstallDir
326356
return tools.RunEslint(workDirectory, eslintInstallationDirectory, nodeBinary, pathsToCheck, autoFix, outputFile, outputFormat)
327357
}
328358

329359
func runTrivyAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
360+
// Ensure Trivy tool is configured and installed
330361
trivy := config.Config.Tools()["trivy"]
331-
if trivy == nil {
332-
log.Fatal("Trivy tool configuration not found")
362+
if trivy == nil || !config.Config.IsToolInstalled("trivy", trivy) {
363+
if trivy == nil {
364+
fmt.Println("Trivy tool configuration not found, adding and installing...")
365+
} else {
366+
fmt.Println("Trivy tool is not installed, installing...")
367+
}
368+
369+
err := config.InstallTool("trivy", trivy, "")
370+
if err != nil {
371+
return fmt.Errorf("failed to install trivy: %w", err)
372+
}
373+
374+
// Get the updated tool info after installation
375+
trivy = config.Config.Tools()["trivy"]
376+
if trivy == nil {
377+
return fmt.Errorf("trivy tool configuration still not found after installation")
378+
}
379+
fmt.Println("Trivy tool installed successfully")
333380
}
381+
382+
// Ensure trivy binary is available
334383
trivyBinary := trivy.Binaries["trivy"]
384+
if trivyBinary == "" {
385+
return fmt.Errorf("trivy binary not found in tool configuration")
386+
}
335387

388+
// Run Trivy
336389
return tools.RunTrivy(workDirectory, trivyBinary, pathsToCheck, outputFile, outputFormat)
337390
}
338391

339392
func runPmdAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
393+
// Ensure PMD tool is configured and installed
340394
pmd := config.Config.Tools()["pmd"]
341-
if pmd == nil {
342-
log.Fatal("Pmd tool configuration not found")
395+
if pmd == nil || !config.Config.IsToolInstalled("pmd", pmd) {
396+
if pmd == nil {
397+
fmt.Println("PMD tool configuration not found, adding and installing...")
398+
} else {
399+
fmt.Println("PMD tool is not installed, installing...")
400+
}
401+
402+
err := config.InstallTool("pmd", pmd, "")
403+
if err != nil {
404+
return fmt.Errorf("failed to install pmd: %w", err)
405+
}
406+
407+
// Get the updated tool info after installation
408+
pmd = config.Config.Tools()["pmd"]
409+
if pmd == nil {
410+
return fmt.Errorf("pmd tool configuration still not found after installation")
411+
}
412+
fmt.Println("PMD tool installed successfully")
343413
}
414+
415+
// Ensure Java runtime is available
416+
javaRuntime := config.Config.Runtimes()["java"]
417+
if javaRuntime == nil {
418+
return fmt.Errorf("java runtime not found - this should not happen after pmd installation")
419+
}
420+
421+
// Ensure pmd binary is available
344422
pmdBinary := pmd.Binaries["pmd"]
423+
if pmdBinary == "" {
424+
return fmt.Errorf("pmd binary not found in tool configuration")
425+
}
345426

427+
// Run PMD
346428
return tools.RunPmd(workDirectory, pmdBinary, pathsToCheck, outputFile, outputFormat, config.Config)
347429
}
348430

349431
func runPylintAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
432+
// Ensure Pylint tool is configured and installed
350433
pylint := config.Config.Tools()["pylint"]
351-
if pylint == nil {
352-
log.Fatal("Pylint tool configuration not found")
434+
if pylint == nil || !config.Config.IsToolInstalled("pylint", pylint) {
435+
if pylint == nil {
436+
fmt.Println("Pylint tool configuration not found, adding and installing...")
437+
} else {
438+
fmt.Println("Pylint tool is not installed, installing...")
439+
}
440+
441+
err := config.InstallTool("pylint", pylint, "")
442+
if err != nil {
443+
return fmt.Errorf("failed to install pylint: %w", err)
444+
}
445+
446+
// Get the updated tool info after installation
447+
pylint = config.Config.Tools()["pylint"]
448+
if pylint == nil {
449+
return fmt.Errorf("pylint tool configuration still not found after installation")
450+
}
451+
fmt.Println("Pylint tool installed successfully")
452+
}
453+
454+
// Ensure Python runtime is available
455+
pythonRuntime := config.Config.Runtimes()["python"]
456+
if pythonRuntime == nil {
457+
return fmt.Errorf("python runtime not found - this should not happen after pylint installation")
353458
}
459+
460+
// Ensure python binary is available
354461
pylintBinary := pylint.Binaries["python"]
462+
if pylintBinary == "" {
463+
return fmt.Errorf("python binary not found in pylint tool configuration")
464+
}
355465

466+
// Run Pylint
356467
return tools.RunPylint(workDirectory, pylintBinary, pathsToCheck, outputFile, outputFormat)
357468
}
358469

359470
func runDartAnalyzer(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
471+
// Ensure Dart Analyzer tool is configured and installed
360472
dartanalyzer := config.Config.Tools()["dartanalyzer"]
361-
if dartanalyzer == nil {
362-
log.Fatal("Dart analyzer tool configuration not found")
473+
if dartanalyzer == nil || !config.Config.IsToolInstalled("dartanalyzer", dartanalyzer) {
474+
if dartanalyzer == nil {
475+
fmt.Println("Dart analyzer tool configuration not found, adding and installing...")
476+
} else {
477+
fmt.Println("Dart analyzer tool is not installed, installing...")
478+
}
479+
480+
err := config.InstallTool("dartanalyzer", dartanalyzer, "")
481+
if err != nil {
482+
return fmt.Errorf("failed to install dartanalyzer: %w", err)
483+
}
484+
485+
// Get the updated tool info after installation
486+
dartanalyzer = config.Config.Tools()["dartanalyzer"]
487+
if dartanalyzer == nil {
488+
return fmt.Errorf("dartanalyzer tool configuration still not found after installation")
489+
}
490+
fmt.Println("Dart analyzer tool installed successfully")
491+
}
492+
493+
// Ensure Dart runtime is available (dart or flutter)
494+
if config.Config.Runtimes()["flutter"] == nil && config.Config.Runtimes()["dart"] == nil {
495+
return fmt.Errorf("dart or flutter runtime not found - this should not happen after dartanalyzer installation")
363496
}
364-
return tools.RunDartAnalyzer(workDirectory, dartanalyzer.InstallDir, dartanalyzer.Binaries["dart"], pathsToCheck, outputFile, outputFormat)
497+
498+
// Ensure dart binary is available
499+
dartBinary := dartanalyzer.Binaries["dart"]
500+
if dartBinary == "" {
501+
return fmt.Errorf("dart binary not found in dartanalyzer tool configuration")
502+
}
503+
504+
// Run Dart Analyzer
505+
return tools.RunDartAnalyzer(workDirectory, dartanalyzer.InstallDir, dartBinary, pathsToCheck, outputFile, outputFormat)
365506
}
366507

367508
func runSemgrepAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
509+
// Ensure Semgrep tool is configured and installed
368510
semgrep := config.Config.Tools()["semgrep"]
369-
if semgrep == nil {
370-
log.Fatal("Semgrep tool configuration not found")
511+
if semgrep == nil || !config.Config.IsToolInstalled("semgrep", semgrep) {
512+
if semgrep == nil {
513+
fmt.Println("Semgrep tool configuration not found, adding and installing...")
514+
} else {
515+
fmt.Println("Semgrep tool is not installed, installing...")
516+
}
517+
518+
err := config.InstallTool("semgrep", semgrep, "")
519+
if err != nil {
520+
return fmt.Errorf("failed to install semgrep: %w", err)
521+
}
522+
523+
// Get the updated tool info after installation
524+
semgrep = config.Config.Tools()["semgrep"]
525+
if semgrep == nil {
526+
return fmt.Errorf("semgrep tool configuration still not found after installation")
527+
}
528+
fmt.Println("Semgrep tool installed successfully")
371529
}
530+
531+
// Ensure Python runtime is available
532+
pythonRuntime := config.Config.Runtimes()["python"]
533+
if pythonRuntime == nil {
534+
return fmt.Errorf("python runtime not found - this should not happen after semgrep installation")
535+
}
536+
537+
// Ensure semgrep binary is available
372538
semgrepBinary := semgrep.Binaries["semgrep"]
539+
if semgrepBinary == "" {
540+
return fmt.Errorf("semgrep binary not found in tool configuration")
541+
}
373542

543+
// Run Semgrep
374544
return tools.RunSemgrep(workDirectory, semgrepBinary, pathsToCheck, outputFile, outputFormat)
375545
}
376546

377547
func runLizardAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
548+
// Ensure Lizard tool is configured and installed
378549
lizardTool := config.Config.Tools()["lizard"]
550+
if lizardTool == nil || !config.Config.IsToolInstalled("lizard", lizardTool) {
551+
if lizardTool == nil {
552+
fmt.Println("Lizard tool configuration not found, adding and installing...")
553+
} else {
554+
fmt.Println("Lizard tool is not installed, installing...")
555+
}
556+
557+
err := config.InstallTool("lizard", lizardTool, "")
558+
if err != nil {
559+
return fmt.Errorf("failed to install lizard: %w", err)
560+
}
561+
562+
// Get the updated tool info after installation
563+
lizardTool = config.Config.Tools()["lizard"]
564+
if lizardTool == nil {
565+
return fmt.Errorf("lizard tool configuration still not found after installation")
566+
}
567+
fmt.Println("Lizard tool installed successfully")
568+
}
379569

380-
if lizardTool == nil {
381-
log.Fatal("Lizard plugin configuration not found")
570+
// Ensure Python runtime is available
571+
pythonRuntime := config.Config.Runtimes()["python"]
572+
if pythonRuntime == nil {
573+
return fmt.Errorf("python runtime not found - this should not happen after lizard installation")
382574
}
383575

576+
// Ensure python binary is available
384577
lizardBinary := lizardTool.Binaries["python"]
578+
if lizardBinary == "" {
579+
return fmt.Errorf("python binary not found in lizard tool configuration")
580+
}
385581

582+
// Get configuration patterns
386583
configFile, exists := tools.ConfigFileExists(config.Config, "lizard.yaml")
387584
var patterns []domain.PatternDefinition
388585
var err error
@@ -401,16 +598,41 @@ func runLizardAnalysis(workDirectory string, pathsToCheck []string, outputFile s
401598
}
402599
}
403600

601+
// Run Lizard
404602
return lizard.RunLizard(workDirectory, lizardBinary, pathsToCheck, outputFile, outputFormat, patterns)
405603
}
406604

407605
func runEnigmaAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
606+
// Ensure Enigma tool is configured and installed
408607
enigma := config.Config.Tools()["codacy-enigma-cli"]
409-
if enigma == nil {
410-
log.Fatal("Enigma tool configuration not found")
608+
if enigma == nil || !config.Config.IsToolInstalled("codacy-enigma-cli", enigma) {
609+
if enigma == nil {
610+
fmt.Println("Enigma tool configuration not found, adding and installing...")
611+
} else {
612+
fmt.Println("Enigma tool is not installed, installing...")
613+
}
614+
615+
err := config.InstallTool("codacy-enigma-cli", enigma, "")
616+
if err != nil {
617+
return fmt.Errorf("failed to install codacy-enigma-cli: %w", err)
618+
}
619+
620+
// Get the updated tool info after installation
621+
enigma = config.Config.Tools()["codacy-enigma-cli"]
622+
if enigma == nil {
623+
return fmt.Errorf("codacy-enigma-cli tool configuration still not found after installation")
624+
}
625+
fmt.Println("Enigma tool installed successfully")
626+
}
627+
628+
// Ensure enigma binary is available
629+
enigmaBinary := enigma.Binaries["codacy-enigma-cli"]
630+
if enigmaBinary == "" {
631+
return fmt.Errorf("codacy-enigma-cli binary not found in tool configuration")
411632
}
412633

413-
return tools.RunEnigma(workDirectory, enigma.InstallDir, enigma.Binaries["codacy-enigma-cli"], pathsToCheck, outputFile, outputFormat)
634+
// Run Enigma
635+
return tools.RunEnigma(workDirectory, enigma.InstallDir, enigmaBinary, pathsToCheck, outputFile, outputFormat)
414636
}
415637

416638
func runReviveAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {

0 commit comments

Comments
 (0)