|
3 | 3 | package configsetup |
4 | 4 |
|
5 | 5 | import ( |
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" |
| 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" |
15 | 15 | ) |
16 | 16 |
|
17 | 17 | // KeepToolsWithLatestVersion filters the tools to keep only the latest |
18 | 18 | // version of each tool family. |
19 | 19 | func KeepToolsWithLatestVersion(tools []domain.Tool) ( |
20 | | - toolsWithLatestVersion []domain.Tool, |
21 | | - uuidToName map[string]string, |
22 | | - familyToVersions map[string][]string, |
| 20 | + toolsWithLatestVersion []domain.Tool, |
| 21 | + uuidToName map[string]string, |
| 22 | + familyToVersions map[string][]string, |
23 | 23 | ) { |
24 | | - latestTools := map[string]domain.Tool{} |
25 | | - uuidToName = map[string]string{} |
26 | | - seen := map[string][]domain.Tool{} |
| 24 | + latestTools := map[string]domain.Tool{} |
| 25 | + uuidToName = map[string]string{} |
| 26 | + seen := map[string][]domain.Tool{} |
27 | 27 |
|
28 | | - for _, tool := range tools { |
29 | | - processToolForLatest(tool, latestTools, uuidToName, seen) |
30 | | - } |
| 28 | + for _, tool := range tools { |
| 29 | + processToolForLatest(tool, latestTools, uuidToName, seen) |
| 30 | + } |
31 | 31 |
|
32 | | - familyToVersions = buildFamilyVersionMap(seen) |
| 32 | + familyToVersions = buildFamilyVersionMap(seen) |
33 | 33 |
|
34 | | - for _, tool := range latestTools { |
35 | | - toolsWithLatestVersion = append(toolsWithLatestVersion, tool) |
36 | | - } |
| 34 | + for _, tool := range latestTools { |
| 35 | + toolsWithLatestVersion = append(toolsWithLatestVersion, tool) |
| 36 | + } |
37 | 37 |
|
38 | | - return |
| 38 | + return |
39 | 39 | } |
40 | 40 |
|
41 | 41 | // processToolForLatest updates the latest tool per family and tracking maps. |
42 | 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 | | - } |
| 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 | 55 | } |
56 | 56 |
|
57 | 57 | // buildFamilyVersionMap builds a map of tool family to discovered versions. |
58 | 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 |
| 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 |
72 | 72 | } |
73 | 73 |
|
74 | 74 | // BuildDefaultConfigurationFiles creates default configuration files for all tools. |
@@ -103,12 +103,12 @@ func CreateConfigurationFilesForDiscoveredTools(discoveredToolNames map[string]s |
103 | 103 | currentCliMode = "local" // Default to local |
104 | 104 | } |
105 | 105 |
|
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) |
| 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) |
112 | 112 | } |
113 | 113 |
|
114 | 114 | // createRemoteToolConfigurationsForDiscovered creates tool configurations for remote mode based on cloud settings. |
@@ -147,29 +147,29 @@ func createRemoteToolConfigurationsForDiscovered(discoveredToolNames map[string] |
147 | 147 |
|
148 | 148 | // selectCorrectToolUUID selects the correct UUID for a tool based on its version. |
149 | 149 | func selectCorrectToolUUID(toolName string, defaultVersions map[string]string) string { |
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 "" |
| 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 "" |
173 | 173 | } |
174 | 174 |
|
175 | 175 | // createDefaultConfigurationsForSpecificTools creates default configurations for specific tools only. |
|
0 commit comments