Skip to content

Commit 91e0038

Browse files
refactor: update initialization logic to use API for tool versions and languages
1 parent 4962c8b commit 91e0038

File tree

2 files changed

+43
-52
lines changed

2 files changed

+43
-52
lines changed

cmd/init.go

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -56,68 +56,49 @@ var initCmd = &cobra.Command{
5656
log.Fatalf("Failed to detect languages: %v", err)
5757
}
5858

59-
// Map detected languages to tools
60-
var enabledTools []domain.Tool
61-
toolVersions := map[string]string{
62-
ESLint: "8.57.0",
63-
Trivy: "0.59.1",
64-
PyLint: "3.3.6",
65-
PMD: "6.55.0",
66-
DartAnalyzer: "3.7.2",
67-
Semgrep: "1.78.0",
68-
Lizard: "1.17.19",
59+
// Get all available tools from the API
60+
availableTools, err := codacyclient.GetToolsVersions()
61+
if err != nil {
62+
log.Fatalf("Failed to get tools versions: %v", err)
6963
}
7064

71-
// Map languages to tools
72-
languageToTools := map[string][]string{
73-
"JavaScript": {ESLint},
74-
"TypeScript": {ESLint},
75-
"Python": {PyLint},
76-
"Java": {PMD},
77-
"Dart": {DartAnalyzer},
78-
"Go": {Semgrep},
79-
"Ruby": {Semgrep},
80-
"PHP": {Semgrep},
81-
"C": {Semgrep},
82-
"C++": {Semgrep},
83-
"C#": {Semgrep},
84-
"Kotlin": {Semgrep},
85-
"Swift": {Semgrep},
86-
"Scala": {PMD},
87-
"Rust": {Semgrep},
65+
// Map tools by name for easier lookup
66+
toolsByName := make(map[string]domain.Tool)
67+
for _, tool := range availableTools {
68+
toolsByName[strings.ToLower(tool.Name)] = tool
8869
}
8970

71+
// Enable tools based on detected languages
72+
var enabledTools []domain.Tool
73+
toolsEnabled := make(map[string]bool)
74+
9075
// Always enable Trivy for security scanning
91-
enabledTools = append(enabledTools, domain.Tool{
92-
Uuid: Trivy,
93-
Name: "trivy",
94-
Version: toolVersions[Trivy],
95-
})
76+
if trivyTool, ok := toolsByName["trivy"]; ok {
77+
enabledTools = append(enabledTools, trivyTool)
78+
toolsEnabled[trivyTool.Uuid] = true
79+
}
9680

9781
// Enable tools based on detected languages
98-
toolsEnabled := make(map[string]bool)
9982
for langName := range languages {
100-
if tools, ok := languageToTools[langName]; ok {
101-
for _, toolID := range tools {
102-
if !toolsEnabled[toolID] {
103-
toolsEnabled[toolID] = true
104-
enabledTools = append(enabledTools, domain.Tool{
105-
Uuid: toolID,
106-
Name: toolNameFromUUID(toolID),
107-
Version: toolVersions[toolID],
108-
})
83+
for _, tool := range availableTools {
84+
if !toolsEnabled[tool.Uuid] {
85+
for _, supportedLang := range tool.Languages {
86+
if strings.EqualFold(langName, supportedLang) {
87+
enabledTools = append(enabledTools, tool)
88+
toolsEnabled[tool.Uuid] = true
89+
break
90+
}
10991
}
11092
}
11193
}
11294
}
11395

11496
// Always enable Lizard for complexity analysis if any supported language is detected
11597
if shouldEnableLizard(languages) {
116-
enabledTools = append(enabledTools, domain.Tool{
117-
Uuid: Lizard,
118-
Name: "lizard",
119-
Version: toolVersions[Lizard],
120-
})
98+
if lizardTool, ok := toolsByName["lizard"]; ok && !toolsEnabled[lizardTool.Uuid] {
99+
enabledTools = append(enabledTools, lizardTool)
100+
toolsEnabled[lizardTool.Uuid] = true
101+
}
121102
}
122103

123104
// Create configuration files
@@ -138,8 +119,17 @@ var initCmd = &cobra.Command{
138119
}
139120

140121
fmt.Println("\nEnabled tools:")
122+
// Create a map of supported tool UUIDs for quick lookup
123+
supportedTools := make(map[string]bool)
124+
for _, uuid := range AvailableTools {
125+
supportedTools[uuid] = true
126+
}
127+
128+
// Only show tools that are both enabled and supported by the CLI
141129
for _, tool := range enabledTools {
142-
fmt.Printf(" - %s@%s\n", tool.Name, tool.Version)
130+
if supportedTools[tool.Uuid] {
131+
fmt.Printf(" - %s@%s\n", tool.Name, tool.Version)
132+
}
143133
}
144134
} else {
145135
err := buildRepositoryConfigurationFiles(initFlags.ApiToken)

domain/tool.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ type ToolsResponse struct {
77

88
// Tool represents a tool in the Codacy API
99
type Tool struct {
10-
Uuid string `json:"uuid"`
11-
Name string `json:"name"`
12-
Version string `json:"version"`
13-
Settings struct {
10+
Uuid string `json:"uuid"`
11+
Name string `json:"name"`
12+
Version string `json:"version"`
13+
Languages []string `json:"languages"`
14+
Settings struct {
1415
Enabled bool `json:"isEnabled"`
1516
HasConfigurationFile bool `json:"hasConfigurationFile"`
1617
UsesConfigurationFile bool `json:"usesConfigurationFile"`

0 commit comments

Comments
 (0)