Skip to content

Commit 141c07b

Browse files
dedup code
1 parent fba5b0e commit 141c07b

File tree

1 file changed

+18
-69
lines changed

1 file changed

+18
-69
lines changed

tools/language_config.go

Lines changed: 18 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ import (
2323
// This file is responsible for building the languages-config.yaml file.
2424
//
2525

26-
// GetToolLanguageMappingFromAPI gets the tool language mapping from the public API
27-
//
28-
// TODO: cache this with TTL time
29-
func GetToolLanguageMappingFromAPI() (map[string]domain.ToolLanguageInfo, error) {
30-
// Get tools from API (same as buildToolLanguageConfigFromAPI)
26+
// buildToolLanguageInfoFromAPI builds tool language information from API data
27+
// This is the core shared logic used by both GetToolLanguageMappingFromAPI and buildToolLanguageConfigFromAPI
28+
func buildToolLanguageInfoFromAPI() (map[string]domain.ToolLanguageInfo, error) {
29+
// Get all tools from API with their languages
3130
allTools, err := codacyclient.GetToolsVersions()
3231
if err != nil {
3332
return nil, fmt.Errorf("failed to get tools from API: %w", err)
@@ -45,7 +44,7 @@ func GetToolLanguageMappingFromAPI() (map[string]domain.ToolLanguageInfo, error)
4544
languageExtensionsMap[strings.ToLower(langTool.Name)] = langTool.FileExtensions
4645
}
4746

48-
// Build the tool to language mapping (same pattern as buildToolLanguageConfigFromAPI)
47+
// Build tool language configurations from API data
4948
result := make(map[string]domain.ToolLanguageInfo)
5049
supportedToolNames := make(map[string]bool)
5150

@@ -99,6 +98,13 @@ func GetToolLanguageMappingFromAPI() (map[string]domain.ToolLanguageInfo, error)
9998
return result, nil
10099
}
101100

101+
// GetToolLanguageMappingFromAPI gets the tool language mapping from the public API
102+
//
103+
// TODO: cache this with TTL time
104+
func GetToolLanguageMappingFromAPI() (map[string]domain.ToolLanguageInfo, error) {
105+
return buildToolLanguageInfoFromAPI()
106+
}
107+
102108
// GetDefaultToolLanguageMapping returns the default mapping of tools to their supported languages and file extensions
103109
// This function now uses the public API instead of hardcoded mappings.
104110
func GetDefaultToolLanguageMapping() map[string]domain.ToolLanguageInfo {
@@ -116,73 +122,16 @@ func GetDefaultToolLanguageMapping() map[string]domain.ToolLanguageInfo {
116122

117123
// buildToolLanguageConfigFromAPI builds tool language configuration using only API data
118124
func buildToolLanguageConfigFromAPI() ([]domain.ToolLanguageInfo, error) {
119-
// Get all tools from API with their languages
120-
allTools, err := codacyclient.GetToolsVersions()
121-
if err != nil {
122-
return nil, fmt.Errorf("failed to get tools from API: %w", err)
123-
}
124-
125-
// Get language file extensions from API
126-
languageTools, err := codacyclient.GetLanguageTools()
125+
// Use the shared logic to get tool info map
126+
toolInfoMap, err := buildToolLanguageInfoFromAPI()
127127
if err != nil {
128-
return nil, fmt.Errorf("failed to get language tools from API: %w", err)
129-
}
130-
131-
// Create map of language name to file extensions
132-
languageExtensionsMap := make(map[string][]string)
133-
for _, langTool := range languageTools {
134-
languageExtensionsMap[strings.ToLower(langTool.Name)] = langTool.FileExtensions
128+
return nil, err
135129
}
136130

137-
// Build tool language configurations from API data
131+
// Convert map to slice
138132
var configTools []domain.ToolLanguageInfo
139-
supportedToolNames := make(map[string]bool)
140-
141-
// Get supported tool names from metadata
142-
for _, meta := range domain.SupportedToolsMetadata {
143-
supportedToolNames[meta.Name] = true
144-
}
145-
146-
// Group tools by name and keep only supported ones
147-
toolsByName := make(map[string]domain.Tool)
148-
for _, tool := range allTools {
149-
if supportedToolNames[strings.ToLower(tool.ShortName)] {
150-
// Keep the tool with latest version (first one in the response)
151-
if _, exists := toolsByName[strings.ToLower(tool.ShortName)]; !exists {
152-
toolsByName[strings.ToLower(tool.ShortName)] = tool
153-
}
154-
}
155-
}
156-
157-
// Build configuration for each supported tool
158-
for toolName, tool := range toolsByName {
159-
configTool := domain.ToolLanguageInfo{
160-
Name: toolName,
161-
Languages: tool.Languages,
162-
Extensions: []string{},
163-
}
164-
165-
// Build extensions from API language data
166-
extensionsSet := make(map[string]struct{})
167-
for _, apiLang := range tool.Languages {
168-
lowerLang := strings.ToLower(apiLang)
169-
if extensions, exists := languageExtensionsMap[lowerLang]; exists {
170-
for _, ext := range extensions {
171-
extensionsSet[ext] = struct{}{}
172-
}
173-
}
174-
}
175-
176-
// Convert set to sorted slice
177-
for ext := range extensionsSet {
178-
configTool.Extensions = append(configTool.Extensions, ext)
179-
}
180-
slices.Sort(configTool.Extensions)
181-
182-
// Sort languages alphabetically
183-
slices.Sort(configTool.Languages)
184-
185-
configTools = append(configTools, configTool)
133+
for _, toolInfo := range toolInfoMap {
134+
configTools = append(configTools, toolInfo)
186135
}
187136

188137
// Sort tools by name for consistent output

0 commit comments

Comments
 (0)