Skip to content

Commit fa8156f

Browse files
lizard refactor to have logic of running in runner
1 parent 937fc4e commit fa8156f

File tree

2 files changed

+23
-126
lines changed

2 files changed

+23
-126
lines changed

cmd/analyze.go

Lines changed: 3 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package cmd
22

33
import (
44
"codacy/cli-v2/config"
5-
"codacy/cli-v2/domain"
65
"codacy/cli-v2/plugins"
76
"codacy/cli-v2/tools"
87
"codacy/cli-v2/tools/lizard"
@@ -332,9 +331,11 @@ func runToolByTooName(toolName string, workDirectory string, pathsToCheck []stri
332331
case "semgrep":
333332
return tools.RunSemgrep(workDirectory, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
334333
case "lizard":
335-
return runLizardAnalysis(workDirectory, pathsToCheck, outputFile, outputFormat)
334+
return lizard.RunLizard(workDirectory, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
336335
case "enigma":
337336
return tools.RunEnigma(workDirectory, tool.InstallDir, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
337+
case "revive":
338+
return reviveTool.RunRevive(workDirectory, tool.Binaries["revive"], pathsToCheck, outputFile, outputFormat)
338339
}
339340
return fmt.Errorf("unsupported tool: %s", toolName)
340341
}
@@ -352,8 +353,6 @@ func genericRunTool(toolName string, workDirectory string, pathsToCheck []string
352353
return fmt.Errorf("failed to install %s: %w", toolName, err)
353354
}
354355
tool = config.Config.Tools()[toolName]
355-
isToolInstalled = config.Config.IsToolInstalled(toolName, tool)
356-
357356
runtime = config.Config.Runtimes()[tool.Runtime]
358357
isRuntimeInstalled = runtime == nil || config.Config.IsRuntimeInstalled(tool.Runtime, runtime)
359358
if !isRuntimeInstalled {
@@ -363,7 +362,6 @@ func genericRunTool(toolName string, workDirectory string, pathsToCheck []string
363362
return fmt.Errorf("failed to install %s runtime: %w", tool.Runtime, err)
364363
}
365364
runtime = config.Config.Runtimes()[tool.Runtime]
366-
isRuntimeInstalled = config.Config.IsRuntimeInstalled(tool.Runtime, runtime)
367365
}
368366

369367
} else {
@@ -376,131 +374,11 @@ func genericRunTool(toolName string, workDirectory string, pathsToCheck []string
376374
return fmt.Errorf("failed to install %s runtime: %w", tool.Runtime, err)
377375
}
378376
runtime = config.Config.Runtimes()[tool.Runtime]
379-
isRuntimeInstalled = config.Config.IsRuntimeInstalled(tool.Runtime, runtime)
380377
}
381-
382378
}
383-
384379
return runToolByTooName(toolName, workDirectory, pathsToCheck, autoFix, outputFile, outputFormat, tool, runtime)
385380
}
386381

387-
func runLizardAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
388-
// Ensure Lizard tool is configured and installed
389-
lizardTool := config.Config.Tools()["lizard"]
390-
isToolInstalled := config.Config.IsToolInstalled("lizard", lizardTool)
391-
392-
// Also check if the runtime is installed
393-
var isRuntimeInstalled bool
394-
if lizardTool != nil {
395-
pythonRuntime := config.Config.Runtimes()["python"]
396-
isRuntimeInstalled = pythonRuntime != nil && config.Config.IsRuntimeInstalled("python", pythonRuntime)
397-
}
398-
399-
if lizardTool == nil || !isToolInstalled || !isRuntimeInstalled {
400-
if lizardTool == nil {
401-
fmt.Println("Lizard tool configuration not found, adding and installing...")
402-
} else if !isToolInstalled {
403-
fmt.Println("Lizard tool is not installed, installing...")
404-
} else if !isRuntimeInstalled {
405-
fmt.Println("Python runtime is not installed, installing Lizard (which will install the runtime)...")
406-
}
407-
408-
err := config.InstallTool("lizard", lizardTool, "")
409-
if err != nil {
410-
return fmt.Errorf("failed to install lizard: %w", err)
411-
}
412-
413-
// Get the updated tool info after installation
414-
lizardTool = config.Config.Tools()["lizard"]
415-
if lizardTool == nil {
416-
return fmt.Errorf("lizard tool configuration still not found after installation")
417-
}
418-
fmt.Println("Lizard tool installed successfully")
419-
}
420-
421-
// Ensure Python runtime is available
422-
pythonRuntime := config.Config.Runtimes()["python"]
423-
if pythonRuntime == nil {
424-
return fmt.Errorf("python runtime not found - this should not happen after lizard installation")
425-
}
426-
427-
// Ensure python binary is available
428-
lizardBinary := lizardTool.Binaries["python"]
429-
if lizardBinary == "" {
430-
return fmt.Errorf("python binary not found in lizard tool configuration")
431-
}
432-
433-
// Get configuration patterns
434-
configFile, exists := tools.ConfigFileExists(config.Config, "lizard.yaml")
435-
var patterns []domain.PatternDefinition
436-
var err error
437-
438-
if exists {
439-
// Configuration exists, read from file
440-
patterns, err = lizard.ReadConfig(configFile)
441-
if err != nil {
442-
return fmt.Errorf("error reading config file: %v", err)
443-
}
444-
} else {
445-
fmt.Println("No configuration file found for Lizard, using default patterns, run init with repository token to get a custom configuration")
446-
patterns, err = tools.FetchDefaultEnabledPatterns(domain.Lizard)
447-
if err != nil {
448-
return fmt.Errorf("failed to fetch default patterns: %v", err)
449-
}
450-
}
451-
452-
// Run Lizard
453-
return lizard.RunLizard(workDirectory, lizardBinary, pathsToCheck, outputFile, outputFormat, patterns)
454-
}
455-
456-
func runEnigmaAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
457-
// Ensure Enigma tool is configured and installed
458-
enigma := config.Config.Tools()["codacy-enigma-cli"]
459-
isToolInstalled := config.Config.IsToolInstalled("codacy-enigma-cli", enigma)
460-
461-
// Enigma is a download-based tool (no runtime dependency), so runtime is always "installed"
462-
isRuntimeInstalled := true
463-
464-
if enigma == nil || !isToolInstalled || !isRuntimeInstalled {
465-
if enigma == nil {
466-
fmt.Println("Enigma tool configuration not found, adding and installing...")
467-
} else if !isToolInstalled {
468-
fmt.Println("Enigma tool is not installed, installing...")
469-
}
470-
471-
err := config.InstallTool("codacy-enigma-cli", enigma, "")
472-
if err != nil {
473-
return fmt.Errorf("failed to install codacy-enigma-cli: %w", err)
474-
}
475-
476-
// Get the updated tool info after installation
477-
enigma = config.Config.Tools()["codacy-enigma-cli"]
478-
if enigma == nil {
479-
return fmt.Errorf("codacy-enigma-cli tool configuration still not found after installation")
480-
}
481-
fmt.Println("Enigma tool installed successfully")
482-
}
483-
484-
// Ensure enigma binary is available
485-
enigmaBinary := enigma.Binaries["codacy-enigma-cli"]
486-
if enigmaBinary == "" {
487-
return fmt.Errorf("codacy-enigma-cli binary not found in tool configuration")
488-
}
489-
490-
// Run Enigma
491-
return tools.RunEnigma(workDirectory, enigma.InstallDir, enigmaBinary, pathsToCheck, outputFile, outputFormat)
492-
}
493-
494-
func runReviveAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
495-
revive := config.Config.Tools()["revive"]
496-
if revive == nil {
497-
log.Fatal("Revive tool configuration not found")
498-
}
499-
reviveBinary := revive.Binaries["revive"]
500-
501-
return reviveTool.RunRevive(workDirectory, reviveBinary, pathsToCheck, outputFile, outputFormat)
502-
}
503-
504382
var analyzeCmd = &cobra.Command{
505383
Use: "analyze",
506384
Short: "Runs all configured linters.",

tools/lizard/lizardRunner.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,35 @@ package lizard
22

33
import (
44
"bytes"
5+
"codacy/cli-v2/config"
56
"codacy/cli-v2/domain"
7+
"codacy/cli-v2/tools"
68
"encoding/json"
79
"fmt"
810
"os"
911
"os/exec"
1012
)
1113

1214
// RunLizard runs the Lizard tool and returns any issues found
13-
func RunLizard(workDirectory string, binary string, files []string, outputFile string, outputFormat string, patterns []domain.PatternDefinition) error {
15+
func RunLizard(workDirectory string, binary string, files []string, outputFile string, outputFormat string) error {
16+
// Get configuration patterns
17+
configFile, exists := tools.ConfigFileExists(config.Config, "lizard.yaml")
18+
var patterns []domain.PatternDefinition
19+
var errConfigs error
1420

21+
if exists {
22+
// Configuration exists, read from file
23+
patterns, errConfigs = ReadConfig(configFile)
24+
if errConfigs != nil {
25+
return fmt.Errorf("error reading config file: %v", errConfigs)
26+
}
27+
} else {
28+
fmt.Println("No configuration file found for Lizard, using default patterns, run init with repository token to get a custom configuration")
29+
patterns, errConfigs = tools.FetchDefaultEnabledPatterns(domain.Lizard)
30+
if errConfigs != nil {
31+
return fmt.Errorf("failed to fetch default patterns: %v", errConfigs)
32+
}
33+
}
1534
if len(patterns) == 0 {
1635
return fmt.Errorf("no valid patterns found in configuration")
1736
}

0 commit comments

Comments
 (0)