Skip to content

Commit b43c19d

Browse files
handle complexity issue
1 parent c970ca0 commit b43c19d

File tree

1 file changed

+35
-23
lines changed

1 file changed

+35
-23
lines changed

config/config.go

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,55 +69,45 @@ func (c *ConfigType) Runtimes() map[string]*plugins.RuntimeInfo {
6969
return c.runtimes
7070
}
7171

72-
func (c *ConfigType) addRuntimeToCodacyYaml(name string, version string) error {
73-
codacyPath := filepath.Join(c.localCodacyDirectory, "codacy.yaml")
74-
75-
// Read existing content
72+
// readOrCreateConfig reads the existing YAML config or creates a new one
73+
func (c *ConfigType) readOrCreateConfig(codacyPath string) (map[string]interface{}, error) {
7674
content, err := os.ReadFile(codacyPath)
7775
if err != nil && !os.IsNotExist(err) {
78-
return fmt.Errorf("error reading codacy.yaml: %w", err)
76+
return nil, fmt.Errorf("error reading codacy.yaml: %w", err)
7977
}
8078

81-
// Parse existing YAML or create new structure
8279
var config map[string]interface{}
8380
if len(content) > 0 {
8481
if err := yaml.Unmarshal(content, &config); err != nil {
85-
return fmt.Errorf("error parsing codacy.yaml: %w", err)
82+
return nil, fmt.Errorf("error parsing codacy.yaml: %w", err)
8683
}
8784
} else {
8885
config = make(map[string]interface{})
8986
}
9087

91-
// Get or create runtimes list
92-
runtimes, ok := config["runtimes"].([]interface{})
93-
if !ok {
94-
runtimes = make([]interface{}, 0)
95-
}
88+
return config, nil
89+
}
9690

97-
// Create runtime entry
91+
// updateRuntimesList updates or adds a runtime entry in the runtimes list
92+
func updateRuntimesList(runtimes []interface{}, name, version string) []interface{} {
9893
runtimeEntry := fmt.Sprintf("%s@%s", name, version)
9994

10095
// Check if runtime already exists and update it
101-
found := false
10296
for i, r := range runtimes {
10397
if runtime, ok := r.(string); ok {
10498
if strings.Split(runtime, "@")[0] == name {
10599
runtimes[i] = runtimeEntry
106-
found = true
107-
break
100+
return runtimes
108101
}
109102
}
110103
}
111104

112105
// Add new runtime if not found
113-
if !found {
114-
runtimes = append(runtimes, runtimeEntry)
115-
}
116-
117-
// Update config
118-
config["runtimes"] = runtimes
106+
return append(runtimes, runtimeEntry)
107+
}
119108

120-
// Write back to .codacy/codacy.yaml
109+
// writeConfig writes the config back to the YAML file
110+
func (c *ConfigType) writeConfig(codacyPath string, config map[string]interface{}) error {
121111
yamlData, err := yaml.Marshal(config)
122112
if err != nil {
123113
return fmt.Errorf("error marshaling codacy.yaml: %w", err)
@@ -135,6 +125,28 @@ func (c *ConfigType) addRuntimeToCodacyYaml(name string, version string) error {
135125
return nil
136126
}
137127

128+
// addRuntimeToCodacyYaml adds or updates a runtime entry in codacy.yaml as a YAML list
129+
func (c *ConfigType) addRuntimeToCodacyYaml(name string, version string) error {
130+
codacyPath := filepath.Join(c.localCodacyDirectory, "codacy.yaml")
131+
132+
config, err := c.readOrCreateConfig(codacyPath)
133+
if err != nil {
134+
return err
135+
}
136+
137+
// Get or create runtimes list
138+
runtimes, ok := config["runtimes"].([]interface{})
139+
if !ok {
140+
runtimes = make([]interface{}, 0)
141+
}
142+
143+
// Update runtimes list
144+
runtimes = updateRuntimesList(runtimes, name, version)
145+
config["runtimes"] = runtimes
146+
147+
return c.writeConfig(codacyPath, config)
148+
}
149+
138150
func (c *ConfigType) AddRuntimes(configs []plugins.RuntimeConfig) error {
139151
// Process the runtime configurations using the plugins.ProcessRuntimes function
140152
runtimeInfoMap, err := plugins.ProcessRuntimes(configs, c.runtimesDirectory)

0 commit comments

Comments
 (0)