1+ // Package configsetup contains defaults and helpers to generate
2+ // configuration for supported tools.
13package configsetup
24
35import (
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.
1619func 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.
6475func 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.
8798func 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.
105115func 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.
140149func 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.
172176func 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