@@ -69,69 +69,87 @@ func (c *ConfigType) Runtimes() map[string]*plugins.RuntimeInfo {
6969 return c .runtimes
7070}
7171
72- func (c * ConfigType ) AddRuntimes (configs []plugins.RuntimeConfig ) error {
73- // Process the runtime configurations using the plugins.ProcessRuntimes function
74- runtimeInfoMap , err := plugins .ProcessRuntimes (configs , c .runtimesDirectory )
75- if err != nil {
76- return err
77- }
72+ func (c * ConfigType ) addRuntimeToCodacyYaml (name string , version string ) error {
73+ codacyPath := filepath .Join (c .localCodacyDirectory , "codacy.yaml" )
7874
79- // Store the runtime information in the config
80- for name , info := range runtimeInfoMap {
81- c .runtimes [name ] = info
75+ // Read existing content
76+ content , err := os .ReadFile (codacyPath )
77+ if err != nil && ! os .IsNotExist (err ) {
78+ return fmt .Errorf ("error reading codacy.yaml: %w" , err )
79+ }
8280
83- // Update codacy.yaml with the new runtime
84- if err := addRuntimeToCodacyYaml (name , info .Version ); err != nil {
85- return fmt .Errorf ("failed to update codacy.yaml with runtime %s: %w" , name , err )
81+ // Parse existing YAML or create new structure
82+ var config map [string ]interface {}
83+ if len (content ) > 0 {
84+ if err := yaml .Unmarshal (content , & config ); err != nil {
85+ return fmt .Errorf ("error parsing codacy.yaml: %w" , err )
8686 }
87+ } else {
88+ config = make (map [string ]interface {})
8789 }
8890
89- return nil
90- }
91-
92- // addRuntimeToCodacyYaml adds or updates a runtime entry in .codacy/codacy.yaml as a YAML list
93- func addRuntimeToCodacyYaml (name string , version string ) error {
94- codacyPath := ".codacy/codacy.yaml"
95-
96- type CodacyConfig struct {
97- Runtimes []string `yaml:"runtimes"`
98- Tools []string `yaml:"tools"`
91+ // Get or create runtimes list
92+ runtimes , ok := config ["runtimes" ].([]interface {})
93+ if ! ok {
94+ runtimes = make ([]interface {}, 0 )
9995 }
10096
101- // Read existing config
102- var config CodacyConfig
103- if data , err := os .ReadFile (codacyPath ); err == nil {
104- if err := yaml .Unmarshal (data , & config ); err != nil {
105- return fmt .Errorf ("failed to parse %s: %w" , codacyPath , err )
106- }
107- }
97+ // Create runtime entry
98+ runtimeEntry := fmt .Sprintf ("%s@%s" , name , version )
10899
109- // Prepare the new runtime string
110- runtimeEntry := name + "@" + version
100+ // Check if runtime already exists and update it
111101 found := false
112- for i , entry := range config .Runtimes {
113- if strings .HasPrefix (entry , name + "@" ) {
114- config .Runtimes [i ] = runtimeEntry
115- found = true
116- break
102+ for i , r := range runtimes {
103+ if runtime , ok := r .(string ); ok {
104+ if strings .Split (runtime , "@" )[0 ] == name {
105+ runtimes [i ] = runtimeEntry
106+ found = true
107+ break
108+ }
117109 }
118110 }
111+
112+ // Add new runtime if not found
119113 if ! found {
120- config . Runtimes = append (config . Runtimes , runtimeEntry )
114+ runtimes = append (runtimes , runtimeEntry )
121115 }
122116
123- // Create .codacy directory if it doesn't exist
124- if err := os .MkdirAll (".codacy" , 0755 ); err != nil {
125- return fmt .Errorf ("failed to create .codacy directory: %w" , err )
126- }
117+ // Update config
118+ config ["runtimes" ] = runtimes
127119
128120 // Write back to .codacy/codacy.yaml
129121 yamlData , err := yaml .Marshal (config )
130122 if err != nil {
131- return fmt .Errorf ("failed to marshal config to YAML : %w" , err )
123+ return fmt .Errorf ("error marshaling codacy.yaml : %w" , err )
132124 }
133- if err := os .WriteFile (codacyPath , yamlData , 0644 ); err != nil {
134- return fmt .Errorf ("failed to write config to %s: %w" , codacyPath , err )
125+
126+ // Ensure directory exists
127+ if err := os .MkdirAll (filepath .Dir (codacyPath ), utils .DefaultDirPerms ); err != nil {
128+ return fmt .Errorf ("error creating .codacy directory: %w" , err )
129+ }
130+
131+ if err := os .WriteFile (codacyPath , yamlData , utils .DefaultFilePerms ); err != nil {
132+ return fmt .Errorf ("error writing codacy.yaml: %w" , err )
133+ }
134+
135+ return nil
136+ }
137+
138+ func (c * ConfigType ) AddRuntimes (configs []plugins.RuntimeConfig ) error {
139+ // Process the runtime configurations using the plugins.ProcessRuntimes function
140+ runtimeInfoMap , err := plugins .ProcessRuntimes (configs , c .runtimesDirectory )
141+ if err != nil {
142+ return err
143+ }
144+
145+ // Store the runtime information in the config
146+ for name , info := range runtimeInfoMap {
147+ c .runtimes [name ] = info
148+
149+ // Update codacy.yaml with the new runtime
150+ if err := c .addRuntimeToCodacyYaml (name , info .Version ); err != nil {
151+ return fmt .Errorf ("failed to update codacy.yaml with runtime %s: %w" , name , err )
152+ }
135153 }
136154
137155 return nil
0 commit comments