Skip to content

Commit e1d8565

Browse files
not fail when analysing tool/runtime that is not installed
1 parent b14e5a3 commit e1d8565

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
@@ -317,71 +317,268 @@ func getToolName(toolName string, version string) string {
317317
}
318318

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

415637
var analyzeCmd = &cobra.Command{

0 commit comments

Comments
 (0)