Skip to content

Commit 8642b50

Browse files
lizard refactor to have logic of running in runner
1 parent e3a8275 commit 8642b50

File tree

2 files changed

+21
-78
lines changed

2 files changed

+21
-78
lines changed

cmd/analyze.go

Lines changed: 1 addition & 77 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"
@@ -331,7 +330,7 @@ func runToolByTooName(toolName string, workDirectory string, pathsToCheck []stri
331330
case "semgrep":
332331
return tools.RunSemgrep(workDirectory, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
333332
case "lizard":
334-
return runLizardAnalysis(workDirectory, pathsToCheck, outputFile, outputFormat)
333+
return lizard.RunLizard(workDirectory, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
335334
case "enigma":
336335
return tools.RunEnigma(workDirectory, tool.InstallDir, tool.Binaries[tool.Runtime], pathsToCheck, outputFile, outputFormat)
337336
}
@@ -351,8 +350,6 @@ func genericRunTool(toolName string, workDirectory string, pathsToCheck []string
351350
return fmt.Errorf("failed to install %s: %w", toolName, err)
352351
}
353352
tool = config.Config.Tools()[toolName]
354-
isToolInstalled = config.Config.IsToolInstalled(toolName, tool)
355-
356353
runtime = config.Config.Runtimes()[tool.Runtime]
357354
isRuntimeInstalled = runtime == nil || config.Config.IsRuntimeInstalled(tool.Runtime, runtime)
358355
if !isRuntimeInstalled {
@@ -362,7 +359,6 @@ func genericRunTool(toolName string, workDirectory string, pathsToCheck []string
362359
return fmt.Errorf("failed to install %s runtime: %w", tool.Runtime, err)
363360
}
364361
runtime = config.Config.Runtimes()[tool.Runtime]
365-
isRuntimeInstalled = config.Config.IsRuntimeInstalled(tool.Runtime, runtime)
366362
}
367363

368364
} else {
@@ -375,83 +371,11 @@ func genericRunTool(toolName string, workDirectory string, pathsToCheck []string
375371
return fmt.Errorf("failed to install %s runtime: %w", tool.Runtime, err)
376372
}
377373
runtime = config.Config.Runtimes()[tool.Runtime]
378-
isRuntimeInstalled = config.Config.IsRuntimeInstalled(tool.Runtime, runtime)
379374
}
380-
381375
}
382-
383376
return runToolByTooName(toolName, workDirectory, pathsToCheck, autoFix, outputFile, outputFormat, tool, runtime)
384377
}
385378

386-
func runLizardAnalysis(workDirectory string, pathsToCheck []string, outputFile string, outputFormat string) error {
387-
// Ensure Lizard tool is configured and installed
388-
lizardTool := config.Config.Tools()["lizard"]
389-
isToolInstalled := config.Config.IsToolInstalled("lizard", lizardTool)
390-
391-
// Also check if the runtime is installed
392-
var isRuntimeInstalled bool
393-
if lizardTool != nil {
394-
pythonRuntime := config.Config.Runtimes()["python"]
395-
isRuntimeInstalled = pythonRuntime != nil && config.Config.IsRuntimeInstalled("python", pythonRuntime)
396-
}
397-
398-
if lizardTool == nil || !isToolInstalled || !isRuntimeInstalled {
399-
if lizardTool == nil {
400-
fmt.Println("Lizard tool configuration not found, adding and installing...")
401-
} else if !isToolInstalled {
402-
fmt.Println("Lizard tool is not installed, installing...")
403-
} else if !isRuntimeInstalled {
404-
fmt.Println("Python runtime is not installed, installing Lizard (which will install the runtime)...")
405-
}
406-
407-
err := config.InstallTool("lizard", lizardTool, "")
408-
if err != nil {
409-
return fmt.Errorf("failed to install lizard: %w", err)
410-
}
411-
412-
// Get the updated tool info after installation
413-
lizardTool = config.Config.Tools()["lizard"]
414-
if lizardTool == nil {
415-
return fmt.Errorf("lizard tool configuration still not found after installation")
416-
}
417-
fmt.Println("Lizard tool installed successfully")
418-
}
419-
420-
// Ensure Python runtime is available
421-
pythonRuntime := config.Config.Runtimes()["python"]
422-
if pythonRuntime == nil {
423-
return fmt.Errorf("python runtime not found - this should not happen after lizard installation")
424-
}
425-
426-
// Ensure python binary is available
427-
lizardBinary := lizardTool.Binaries["python"]
428-
if lizardBinary == "" {
429-
return fmt.Errorf("python binary not found in lizard tool configuration")
430-
}
431-
432-
// Get configuration patterns
433-
configFile, exists := tools.ConfigFileExists(config.Config, "lizard.yaml")
434-
var patterns []domain.PatternDefinition
435-
var err error
436-
437-
if exists {
438-
// Configuration exists, read from file
439-
patterns, err = lizard.ReadConfig(configFile)
440-
if err != nil {
441-
return fmt.Errorf("error reading config file: %v", err)
442-
}
443-
} else {
444-
fmt.Println("No configuration file found for Lizard, using default patterns, run init with repository token to get a custom configuration")
445-
patterns, err = tools.FetchDefaultEnabledPatterns(domain.Lizard)
446-
if err != nil {
447-
return fmt.Errorf("failed to fetch default patterns: %v", err)
448-
}
449-
}
450-
451-
// Run Lizard
452-
return lizard.RunLizard(workDirectory, lizardBinary, pathsToCheck, outputFile, outputFormat, patterns)
453-
}
454-
455379
var analyzeCmd = &cobra.Command{
456380
Use: "analyze",
457381
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)