Skip to content

Commit 21e6e35

Browse files
committed
fix Codacy issues
1 parent 2d4418d commit 21e6e35

File tree

6 files changed

+135
-111
lines changed

6 files changed

+135
-111
lines changed

cmd/configsetup/codacy_yaml.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package configsetup provides helpers to create Codacy configuration
2+
// files (codacy.yaml and related tool/runtime sections).
13
package configsetup
24

35
import (
@@ -17,6 +19,8 @@ type RuntimePluginConfig struct {
1719
DefaultVersion string `yaml:"default_version"`
1820
}
1921

22+
// ConfigFileTemplate returns the content of codacy.yaml given the enabled
23+
// tools. It lists required runtimes and tools with their versions.
2024
func ConfigFileTemplate(tools []domain.Tool) string {
2125
toolsMap := make(map[string]bool)
2226
toolVersions := make(map[string]string)
@@ -47,7 +51,7 @@ func ConfigFileTemplate(tools []domain.Tool) string {
4751
return sb.String()
4852
}
4953

50-
// getToolVersion returns the version for a tool, preferring tool.Version over default
54+
// getToolVersion returns the version for a tool, preferring tool.Version over default.
5155
func getToolVersion(tool domain.Tool, defaultVersions map[string]string) string {
5256
if tool.Version != "" {
5357
return tool.Version
@@ -60,9 +64,9 @@ func getToolVersion(tool domain.Tool, defaultVersions map[string]string) string
6064
return ""
6165
}
6266

63-
// addRequiredRuntime adds the runtime requirement for a tool
64-
func addRequiredRuntime(toolUuid string, neededRuntimes map[string]bool, runtimeDependencies map[string]string) {
65-
if meta, ok := domain.SupportedToolsMetadata[toolUuid]; ok {
67+
// addRequiredRuntime adds the runtime requirement for a tool.
68+
func addRequiredRuntime(toolUUID string, neededRuntimes map[string]bool, runtimeDependencies map[string]string) {
69+
if meta, ok := domain.SupportedToolsMetadata[toolUUID]; ok {
6670
if runtime, ok := runtimeDependencies[meta.Name]; ok {
6771
if meta.Name == "dartanalyzer" {
6872
// For dartanalyzer, default to dart runtime
@@ -74,7 +78,7 @@ func addRequiredRuntime(toolUuid string, neededRuntimes map[string]bool, runtime
7478
}
7579
}
7680

77-
// buildRuntimesSection builds the runtimes section of the configuration
81+
// buildRuntimesSection builds the runtimes section of the configuration.
7882
func buildRuntimesSection(sb *strings.Builder, tools []domain.Tool, neededRuntimes map[string]bool, runtimeVersions map[string]string, runtimeDependencies map[string]string) {
7983
sb.WriteString("runtimes:\n")
8084

@@ -86,7 +90,7 @@ func buildRuntimesSection(sb *strings.Builder, tools []domain.Tool, neededRuntim
8690
writeRuntimesList(sb, neededRuntimes, runtimeVersions)
8791
}
8892

89-
// addAllSupportedRuntimes adds all runtimes needed by supported tools
93+
// addAllSupportedRuntimes adds all runtimes needed by supported tools.
9094
func addAllSupportedRuntimes(neededRuntimes map[string]bool, runtimeDependencies map[string]string) {
9195
supportedTools, err := plugins.GetSupportedTools()
9296
if err != nil {
@@ -105,7 +109,7 @@ func addAllSupportedRuntimes(neededRuntimes map[string]bool, runtimeDependencies
105109
}
106110
}
107111

108-
// writeRuntimesList writes the sorted runtimes list to the string builder
112+
// writeRuntimesList writes the sorted runtimes list to the string builder.
109113
func writeRuntimesList(sb *strings.Builder, neededRuntimes map[string]bool, runtimeVersions map[string]string) {
110114
var sortedRuntimes []string
111115
for runtime := range neededRuntimes {
@@ -118,7 +122,7 @@ func writeRuntimesList(sb *strings.Builder, neededRuntimes map[string]bool, runt
118122
}
119123
}
120124

121-
// buildToolsSection builds the tools section of the configuration
125+
// buildToolsSection builds the tools section of the configuration.
122126
func buildToolsSection(sb *strings.Builder, tools []domain.Tool, toolsMap map[string]bool, toolVersions map[string]string, defaultVersions map[string]string) {
123127
sb.WriteString("tools:\n")
124128

@@ -129,7 +133,7 @@ func buildToolsSection(sb *strings.Builder, tools []domain.Tool, toolsMap map[st
129133
}
130134
}
131135

132-
// writeEnabledTools writes the enabled tools to the string builder
136+
// writeEnabledTools writes the enabled tools to the string builder.
133137
func writeEnabledTools(sb *strings.Builder, toolsMap map[string]bool, toolVersions map[string]string) {
134138
var sortedTools []string
135139
for uuid, meta := range domain.SupportedToolsMetadata {
@@ -150,7 +154,7 @@ func writeEnabledTools(sb *strings.Builder, toolsMap map[string]bool, toolVersio
150154
}
151155
}
152156

153-
// writeAllSupportedTools writes all supported tools to the string builder
157+
// writeAllSupportedTools writes all supported tools to the string builder.
154158
func writeAllSupportedTools(sb *strings.Builder, defaultVersions map[string]string) {
155159
supportedTools, err := plugins.GetSupportedTools()
156160
if err != nil {

cmd/configsetup/config_creators.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// Package configsetup provides helpers to create Codacy configuration
2+
// and helper files locally.
13
package configsetup
24

35
import (
@@ -12,6 +14,8 @@ import (
1214
"gopkg.in/yaml.v3"
1315
)
1416

17+
// CreateLanguagesConfigFileLocal writes the languages configuration file to
18+
// the provided directory using data from the Codacy API.
1519
func CreateLanguagesConfigFileLocal(toolsConfigDir string) error {
1620
// Build tool language configurations from API
1721
configTools, err := tools.BuildLanguagesConfigFromAPI()
@@ -33,12 +37,16 @@ func CreateLanguagesConfigFileLocal(toolsConfigDir string) error {
3337
return writeConfigFile(filepath.Join(toolsConfigDir, constants.LanguagesConfigFileName), data)
3438
}
3539

40+
// CreateGitIgnoreFile creates a default .gitignore in the local Codacy
41+
// directory to avoid committing generated files.
3642
func CreateGitIgnoreFile() error {
3743
gitIgnorePath := filepath.Join(config.Config.LocalCodacyDirectory(), constants.GitIgnoreFileName)
3844
content := "# Codacy CLI\ntools-configs/\n.gitignore\ncli-config.yaml\nlogs/\n"
3945
return writeConfigFile(gitIgnorePath, []byte(content))
4046
}
4147

48+
// CreateConfigurationFiles generates project configuration files
49+
// (codacy.yaml and cli-config.yaml).
4250
func CreateConfigurationFiles(tools []domain.Tool, cliLocalMode bool, flags domain.InitFlags) error {
4351
// Create project config file
4452
configContent := ConfigFileTemplate(tools)
@@ -55,11 +63,10 @@ func CreateConfigurationFiles(tools []domain.Tool, cliLocalMode bool, flags doma
5563
return nil
5664
}
5765

58-
// buildCliConfigContent creates the CLI configuration content
66+
// buildCliConfigContent creates the CLI configuration content.
5967
func buildCliConfigContent(cliLocalMode bool, initFlags domain.InitFlags) string {
60-
if cliLocalMode {
61-
return fmt.Sprintf("mode: local")
62-
} else {
63-
return fmt.Sprintf("mode: remote\nprovider: %s\norganization: %s\nrepository: %s", initFlags.Provider, initFlags.Organization, initFlags.Repository)
64-
}
68+
if cliLocalMode {
69+
return fmt.Sprintf("mode: local")
70+
}
71+
return fmt.Sprintf("mode: remote\nprovider: %s\norganization: %s\nrepository: %s", initFlags.Provider, initFlags.Organization, initFlags.Repository)
6572
}

cmd/configsetup/default_config.go

Lines changed: 93 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,77 @@
1+
// Package configsetup contains defaults and helpers to generate
2+
// configuration for supported tools.
13
package configsetup
24

35
import (
4-
"fmt"
5-
"log"
6-
"strings"
7-
8-
codacyclient "codacy/cli-v2/codacy-client"
9-
"codacy/cli-v2/config"
10-
"codacy/cli-v2/domain"
11-
"codacy/cli-v2/plugins"
12-
"codacy/cli-v2/tools"
6+
"fmt"
7+
"log"
8+
"strings"
9+
10+
codacyclient "codacy/cli-v2/codacy-client"
11+
"codacy/cli-v2/config"
12+
"codacy/cli-v2/domain"
13+
"codacy/cli-v2/plugins"
14+
"codacy/cli-v2/tools"
1315
)
1416

15-
// KeepToolsWithLatestVersion filters the tools to keep only the latest version of each tool family.
17+
// KeepToolsWithLatestVersion filters the tools to keep only the latest
18+
// version of each tool family.
1619
func KeepToolsWithLatestVersion(tools []domain.Tool) (
17-
toolsWithLatestVersion []domain.Tool,
18-
uuidToName map[string]string,
19-
familyToVersions map[string][]string,
20+
toolsWithLatestVersion []domain.Tool,
21+
uuidToName map[string]string,
22+
familyToVersions map[string][]string,
2023
) {
21-
latestTools := map[string]domain.Tool{}
22-
uuidToName = map[string]string{}
23-
seen := map[string][]domain.Tool{}
24-
familyToVersions = map[string][]string{}
25-
26-
for _, tool := range tools {
27-
meta, ok := domain.SupportedToolsMetadata[tool.Uuid]
28-
if !ok {
29-
continue
30-
}
24+
latestTools := map[string]domain.Tool{}
25+
uuidToName = map[string]string{}
26+
seen := map[string][]domain.Tool{}
3127

32-
// Track all tools seen per family
33-
seen[meta.Name] = append(seen[meta.Name], tool)
28+
for _, tool := range tools {
29+
processToolForLatest(tool, latestTools, uuidToName, seen)
30+
}
3431

35-
// Pick the best version
36-
current, exists := latestTools[meta.Name]
37-
if !exists || domain.SupportedToolsMetadata[current.Uuid].Priority > meta.Priority {
38-
latestTools[meta.Name] = tool
39-
uuidToName[tool.Uuid] = meta.Name
40-
}
41-
}
32+
familyToVersions = buildFamilyVersionMap(seen)
4233

43-
// Populate final list and version map for logging
44-
for family, tools := range seen {
45-
var versions []string
46-
for _, t := range tools {
47-
v := t.Version
48-
if v == "" {
49-
v = "(unknown)"
50-
}
51-
versions = append(versions, v)
52-
}
53-
familyToVersions[family] = versions
54-
}
34+
for _, tool := range latestTools {
35+
toolsWithLatestVersion = append(toolsWithLatestVersion, tool)
36+
}
5537

56-
for _, tool := range latestTools {
57-
toolsWithLatestVersion = append(toolsWithLatestVersion, tool)
58-
}
38+
return
39+
}
40+
41+
// processToolForLatest updates the latest tool per family and tracking maps.
42+
func processToolForLatest(tool domain.Tool, latestTools map[string]domain.Tool, uuidToName map[string]string, seen map[string][]domain.Tool) {
43+
meta, ok := domain.SupportedToolsMetadata[tool.Uuid]
44+
if !ok {
45+
return
46+
}
47+
48+
seen[meta.Name] = append(seen[meta.Name], tool)
49+
50+
current, exists := latestTools[meta.Name]
51+
if !exists || domain.SupportedToolsMetadata[current.Uuid].Priority > meta.Priority {
52+
latestTools[meta.Name] = tool
53+
uuidToName[tool.Uuid] = meta.Name
54+
}
55+
}
5956

60-
return
57+
// buildFamilyVersionMap builds a map of tool family to discovered versions.
58+
func buildFamilyVersionMap(seen map[string][]domain.Tool) map[string][]string {
59+
familyToVersions := make(map[string][]string)
60+
for family, tools := range seen {
61+
var versions []string
62+
for _, t := range tools {
63+
v := t.Version
64+
if v == "" {
65+
v = "(unknown)"
66+
}
67+
versions = append(versions, v)
68+
}
69+
familyToVersions[family] = versions
70+
}
71+
return familyToVersions
6172
}
6273

63-
// BuildDefaultConfigurationFiles creates default configuration files for all tools
74+
// BuildDefaultConfigurationFiles creates default configuration files for all tools.
6475
func BuildDefaultConfigurationFiles(toolsConfigDir string, flags domain.InitFlags) error {
6576
// Get default tool versions to determine correct UUIDs
6677
defaultVersions := plugins.GetToolVersions()
@@ -83,7 +94,7 @@ func BuildDefaultConfigurationFiles(toolsConfigDir string, flags domain.InitFlag
8394
return createToolConfigurationsForUUIDs(allUUIDs, toolsConfigDir, flags)
8495
}
8596

86-
// CreateConfigurationFilesForDiscoveredTools creates tool configuration files for discovered tools
97+
// CreateConfigurationFilesForDiscoveredTools creates tool configuration files for discovered tools.
8798
func CreateConfigurationFilesForDiscoveredTools(discoveredToolNames map[string]struct{}, toolsConfigDir string, initFlags domain.InitFlags) error {
8899
// Determine CLI mode
89100
currentCliMode, err := config.Config.GetCliMode()
@@ -92,18 +103,16 @@ func CreateConfigurationFilesForDiscoveredTools(discoveredToolNames map[string]s
92103
currentCliMode = "local" // Default to local
93104
}
94105

95-
if currentCliMode == "remote" && initFlags.ApiToken != "" {
96-
// Remote mode - create configurations based on cloud repository settings
97-
return createRemoteToolConfigurationsForDiscovered(discoveredToolNames, initFlags)
98-
} else {
99-
// Local mode - create default configurations for discovered tools
100-
return createDefaultConfigurationsForSpecificTools(discoveredToolNames, toolsConfigDir, initFlags)
101-
}
106+
if currentCliMode == "remote" && initFlags.ApiToken != "" {
107+
// Remote mode - create configurations based on cloud repository settings
108+
return createRemoteToolConfigurationsForDiscovered(discoveredToolNames, initFlags)
109+
}
110+
// Local mode - create default configurations for discovered tools
111+
return createDefaultConfigurationsForSpecificTools(discoveredToolNames, toolsConfigDir, initFlags)
102112
}
103113

104-
// createRemoteToolConfigurationsForDiscovered creates tool configurations for remote mode based on cloud settings
114+
// createRemoteToolConfigurationsForDiscovered creates tool configurations for remote mode based on cloud settings.
105115
func createRemoteToolConfigurationsForDiscovered(discoveredToolNames map[string]struct{}, initFlags domain.InitFlags) error {
106-
107116
// Get repository tools from API
108117
apiTools, err := tools.GetRepositoryTools(initFlags)
109118
if err != nil {
@@ -136,39 +145,34 @@ func createRemoteToolConfigurationsForDiscovered(discoveredToolNames map[string]
136145
return createToolConfigurationFiles(configuredTools, initFlags)
137146
}
138147

139-
// selectCorrectToolUUID selects the correct UUID for a tool based on its version
148+
// selectCorrectToolUUID selects the correct UUID for a tool based on its version.
140149
func selectCorrectToolUUID(toolName string, defaultVersions map[string]string) string {
141-
version, hasVersion := defaultVersions[toolName]
142-
143-
// Special case for PMD: choose PMD7 UUID for version 7.x, PMD UUID for version 6.x
144-
if toolName == "pmd" && hasVersion {
145-
if strings.HasPrefix(version, "7.") {
146-
return domain.PMD7
147-
} else {
148-
return domain.PMD
149-
}
150-
}
151-
152-
// Special case for ESLint: choose ESLint9 UUID for version 9.x, ESLint UUID for older versions
153-
if toolName == "eslint" && hasVersion {
154-
if strings.HasPrefix(version, "9.") {
155-
return domain.ESLint9
156-
} else {
157-
return domain.ESLint
158-
}
159-
}
160-
161-
// For other tools, find the first matching UUID
162-
for uuid, meta := range domain.SupportedToolsMetadata {
163-
if meta.Name == toolName {
164-
return uuid
165-
}
166-
}
167-
168-
return ""
150+
version := defaultVersions[toolName]
151+
152+
switch toolName {
153+
case "pmd":
154+
if strings.HasPrefix(version, "7.") {
155+
return domain.PMD7
156+
}
157+
return domain.PMD
158+
case "eslint":
159+
if strings.HasPrefix(version, "9.") {
160+
return domain.ESLint9
161+
}
162+
return domain.ESLint
163+
}
164+
165+
// For other tools, find the first matching UUID
166+
for uuid, meta := range domain.SupportedToolsMetadata {
167+
if meta.Name == toolName {
168+
return uuid
169+
}
170+
}
171+
172+
return ""
169173
}
170174

171-
// createDefaultConfigurationsForSpecificTools creates default configurations for specific tools only
175+
// createDefaultConfigurationsForSpecificTools creates default configurations for specific tools only.
172176
func createDefaultConfigurationsForSpecificTools(discoveredToolNames map[string]struct{}, toolsConfigDir string, initFlags domain.InitFlags) error {
173177
fmt.Printf("Creating default configurations for %d discovered tools...\n", len(discoveredToolNames))
174178

0 commit comments

Comments
 (0)