Skip to content

Commit edd7158

Browse files
Refactor configuration management functions to be more generic and reduce code duplication. Updated runtime and tool handling functions to use a unified entry list update method. Improved addToCodacyYaml function for better maintainability. (#149)
1 parent c80dceb commit edd7158

File tree

1 file changed

+27
-55
lines changed

1 file changed

+27
-55
lines changed

config/config.go

Lines changed: 27 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -88,39 +88,23 @@ 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+
// updateEntryList updates or adds an entry in a list, avoiding duplicates
92+
func updateEntryList(list []interface{}, name, version string) []interface{} {
93+
entry := fmt.Sprintf("%s@%s", name, version)
94+
95+
// Check if entry already exists and update it
96+
for i, item := range list {
97+
if itemStr, ok := item.(string); ok {
98+
// Extract the name part before "@" to check for matches
99+
if parts := strings.Split(itemStr, "@"); len(parts) > 0 && parts[0] == name {
100+
list[i] = entry
101+
return list
101102
}
102103
}
103104
}
104105

105-
// Add new runtime if not found
106-
return append(runtimes, runtimeEntry)
107-
}
108-
109-
// updateToolsList updates the tools list in the configuration, avoiding duplicates
110-
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)
106+
// Add new entry if not found
107+
return append(list, entry)
124108
}
125109

126110
// writeConfig writes the config back to the YAML file
@@ -142,48 +126,36 @@ func (c *ConfigType) writeConfig(codacyPath string, config map[string]interface{
142126
return nil
143127
}
144128

145-
// addRuntimeToCodacyYaml adds or updates a runtime entry in codacy.yaml as a YAML list
146-
func (c *ConfigType) addRuntimeToCodacyYaml(name string, version string) error {
129+
// addToCodacyYaml is a generic function to add or update entries in codacy.yaml
130+
func (c *ConfigType) addToCodacyYaml(listKey string, name string, version string) error {
147131
codacyPath := filepath.Join(c.localCodacyDirectory, "codacy.yaml")
148132

149133
config, err := c.loadConfigOrInitializeEmpty(codacyPath)
150134
if err != nil {
151135
return err
152136
}
153137

154-
// Get or create runtimes list
155-
runtimes, ok := config["runtimes"].([]interface{})
138+
// Get or create list
139+
list, ok := config[listKey].([]interface{})
156140
if !ok {
157-
runtimes = make([]interface{}, 0)
141+
list = make([]interface{}, 0)
158142
}
159143

160-
// Update runtimes list
161-
runtimes = updateRuntimesList(runtimes, name, version)
162-
config["runtimes"] = runtimes
144+
// Update list
145+
list = updateEntryList(list, name, version)
146+
config[listKey] = list
163147

164148
return c.writeConfig(codacyPath, config)
165149
}
166150

151+
// addRuntimeToCodacyYaml adds or updates a runtime entry in codacy.yaml as a YAML list
152+
func (c *ConfigType) addRuntimeToCodacyYaml(name string, version string) error {
153+
return c.addToCodacyYaml("runtimes", name, version)
154+
}
155+
167156
// addToolToCodacyYaml adds a tool to the codacy.yaml file
168157
func (c *ConfigType) addToolToCodacyYaml(name string, version string) error {
169-
codacyPath := filepath.Join(c.localCodacyDirectory, "codacy.yaml")
170-
171-
config, err := c.loadConfigOrInitializeEmpty(codacyPath)
172-
if err != nil {
173-
return err
174-
}
175-
176-
// Get or create tools list
177-
tools, ok := config["tools"].([]interface{})
178-
if !ok {
179-
tools = make([]interface{}, 0)
180-
}
181-
182-
// Update tools list
183-
tools = updateToolsList(tools, name, version)
184-
config["tools"] = tools
185-
186-
return c.writeConfig(codacyPath, config)
158+
return c.addToCodacyYaml("tools", name, version)
187159
}
188160

189161
func (c *ConfigType) AddRuntimes(configs []plugins.RuntimeConfig) error {

0 commit comments

Comments
 (0)