Skip to content

Commit b21643c

Browse files
Refactor runtime and tools list update functions to a generic versioned list handler for improved code reuse and maintainability.
1 parent 6e31cf5 commit b21643c

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

config/config.go

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,39 +88,32 @@ func (c *ConfigType) loadConfigOrInitializeEmpty(codacyPath string) (map[string]
8888
return config, nil
8989
}
9090

91-
// updateRuntimesList updates or adds a runtime entry in the runtimes list
92-
func updateRuntimesList(runtimes []interface{}, name, version string) []interface{} {
93-
runtimeEntry := fmt.Sprintf("%s@%s", name, version)
94-
95-
// Check if runtime already exists and update it
96-
for i, r := range runtimes {
97-
if runtime, ok := r.(string); ok {
98-
if strings.Split(runtime, "@")[0] == name {
99-
runtimes[i] = runtimeEntry
100-
return runtimes
91+
// updateVersionedList updates or adds an entry in the format "name@version" to a list, avoiding duplicates
92+
func updateVersionedList(entries []interface{}, name, version string) []interface{} {
93+
newEntry := fmt.Sprintf("%s@%s", name, version)
94+
95+
// Check if entry with the same name already exists and update it
96+
for i, entry := range entries {
97+
if entryStr, ok := entry.(string); ok {
98+
if strings.Split(entryStr, "@")[0] == name {
99+
entries[i] = newEntry
100+
return entries
101101
}
102102
}
103103
}
104104

105-
// Add new runtime if not found
106-
return append(runtimes, runtimeEntry)
105+
// Add new entry if not found
106+
return append(entries, newEntry)
107+
}
108+
109+
// updateRuntimesList updates or adds a runtime entry in the runtimes list
110+
func updateRuntimesList(runtimes []interface{}, name, version string) []interface{} {
111+
return updateVersionedList(runtimes, name, version)
107112
}
108113

109114
// updateToolsList updates the tools list in the configuration, avoiding duplicates
110115
func updateToolsList(tools []interface{}, name, version string) []interface{} {
111-
toolEntry := fmt.Sprintf("%s@%s", name, version)
112-
113-
// Check if tool already exists
114-
for i, tool := range tools {
115-
if toolStr, ok := tool.(string); ok && strings.HasPrefix(toolStr, name+"@") {
116-
// Replace existing tool
117-
tools[i] = toolEntry
118-
return tools
119-
}
120-
}
121-
122-
// Add new tool
123-
return append(tools, toolEntry)
116+
return updateVersionedList(tools, name, version)
124117
}
125118

126119
// writeConfig writes the config back to the YAML file

0 commit comments

Comments
 (0)