Skip to content

Commit 3d37bab

Browse files
WIP: working for node, not for java, python also worked before but not working right now
1 parent ca92c23 commit 3d37bab

File tree

4 files changed

+426
-9
lines changed

4 files changed

+426
-9
lines changed

config/config.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
88

99
"codacy/cli-v2/plugins"
1010
"codacy/cli-v2/utils"
11+
12+
"gopkg.in/yaml.v3"
1113
)
1214

1315
type ConfigType struct {
@@ -231,3 +233,20 @@ func (c *ConfigType) IsToolInstalled(name string, tool *plugins.ToolInfo) bool {
231233

232234
// Global singleton config-file
233235
var Config = ConfigType{}
236+
237+
// SaveConfig saves the current configuration to codacy.yaml
238+
func (c *ConfigType) SaveConfig() error {
239+
// Convert config to YAML
240+
yamlData, err := yaml.Marshal(c)
241+
if err != nil {
242+
return fmt.Errorf("failed to marshal config to YAML: %w", err)
243+
}
244+
245+
// Write to codacy.yaml
246+
err = os.WriteFile("codacy.yaml", yamlData, 0644)
247+
if err != nil {
248+
return fmt.Errorf("failed to write config to codacy.yaml: %w", err)
249+
}
250+
251+
return nil
252+
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
package config
2+
3+
import (
4+
"codacy/cli-v2/plugins"
5+
"codacy/cli-v2/utils"
6+
"codacy/cli-v2/utils/logger"
7+
"fmt"
8+
"os"
9+
"strings"
10+
11+
"github.com/sirupsen/logrus"
12+
"gopkg.in/yaml.v3"
13+
)
14+
15+
// InstallRuntimeStrict installs a runtime with strict path handling and validation
16+
func InstallRuntimeStrict(name string, runtimeInfo *plugins.RuntimeInfo) error {
17+
// Create target directory if it doesn't exist
18+
if err := os.MkdirAll(runtimeInfo.InstallDir, utils.DefaultDirPerms); err != nil {
19+
return fmt.Errorf("failed to create installation directory: %w", err)
20+
}
21+
22+
// Download the runtime archive
23+
logger.Info("Downloading runtime", logrus.Fields{
24+
"runtime": name,
25+
"version": runtimeInfo.Version,
26+
"url": runtimeInfo.DownloadURL,
27+
})
28+
29+
archivePath, err := utils.DownloadFile(runtimeInfo.DownloadURL, Config.RuntimesDirectory())
30+
if err != nil {
31+
return fmt.Errorf("failed to download runtime archive: %w", err)
32+
}
33+
34+
// Extract the archive
35+
logger.Info("Extracting runtime", logrus.Fields{
36+
"runtime": name,
37+
"version": runtimeInfo.Version,
38+
"archive": archivePath,
39+
})
40+
41+
archive, err := os.Open(archivePath)
42+
if err != nil {
43+
return fmt.Errorf("failed to open archive: %w", err)
44+
}
45+
defer archive.Close()
46+
47+
// Extract based on archive type
48+
if strings.HasSuffix(archivePath, ".zip") {
49+
err = utils.ExtractZip(archivePath, Config.RuntimesDirectory())
50+
} else {
51+
err = utils.ExtractTarGz(archive, Config.RuntimesDirectory())
52+
}
53+
if err != nil {
54+
return fmt.Errorf("failed to extract runtime archive: %w", err)
55+
}
56+
57+
// Set executable permissions on binaries
58+
logger.Info("Setting binary permissions", logrus.Fields{
59+
"runtime": name,
60+
"version": runtimeInfo.Version,
61+
})
62+
63+
for binaryName, binaryPath := range runtimeInfo.Binaries {
64+
// Skip if binary doesn't exist yet
65+
if _, err := os.Stat(binaryPath); os.IsNotExist(err) {
66+
logger.Debug("Binary not found, skipping", logrus.Fields{
67+
"binary": binaryName,
68+
"path": binaryPath,
69+
})
70+
continue
71+
}
72+
73+
// Set executable permissions
74+
if err := os.Chmod(binaryPath, 0755); err != nil {
75+
return fmt.Errorf("failed to set permissions for binary %s: %w", binaryName, err)
76+
}
77+
}
78+
79+
// Verify installation
80+
if !Config.IsRuntimeInstalled(name, runtimeInfo) {
81+
return fmt.Errorf("runtime %s was installed but binaries are not available", name)
82+
}
83+
84+
logger.Info("Runtime installation completed", logrus.Fields{
85+
"runtime": name,
86+
"version": runtimeInfo.Version,
87+
})
88+
89+
// Update codacy.yaml with the new runtime
90+
if err := updateRuntimeInCodacyYaml(name, runtimeInfo.Version); err != nil {
91+
return fmt.Errorf("failed to update codacy.yaml with runtime %s: %w", name, err)
92+
}
93+
94+
return nil
95+
}
96+
97+
// updateRuntimeInCodacyYaml adds or updates a runtime entry in .codacy/codacy.yaml as a YAML list
98+
func updateRuntimeInCodacyYaml(name string, version string) error {
99+
codacyPath := ".codacy/codacy.yaml"
100+
101+
type CodacyConfig struct {
102+
Runtimes []string `yaml:"runtimes"`
103+
Tools []string `yaml:"tools"`
104+
}
105+
106+
// Read existing config
107+
var config CodacyConfig
108+
if data, err := os.ReadFile(codacyPath); err == nil {
109+
if err := yaml.Unmarshal(data, &config); err != nil {
110+
return fmt.Errorf("failed to parse %s: %w", codacyPath, err)
111+
}
112+
}
113+
114+
// Prepare the new runtime string
115+
runtimeEntry := name + "@" + version
116+
found := false
117+
for i, entry := range config.Runtimes {
118+
if strings.HasPrefix(entry, name+"@") {
119+
config.Runtimes[i] = runtimeEntry
120+
found = true
121+
break
122+
}
123+
}
124+
if !found {
125+
config.Runtimes = append(config.Runtimes, runtimeEntry)
126+
}
127+
128+
// Write back to .codacy/codacy.yaml
129+
yamlData, err := yaml.Marshal(config)
130+
if err != nil {
131+
return fmt.Errorf("failed to marshal config to YAML: %w", err)
132+
}
133+
if err := os.WriteFile(codacyPath, yamlData, 0644); err != nil {
134+
return fmt.Errorf("failed to write config to %s: %w", codacyPath, err)
135+
}
136+
137+
logger.Info("Runtime entry updated in .codacy/codacy.yaml", logrus.Fields{
138+
"runtime": name,
139+
"version": version,
140+
})
141+
return nil
142+
}

0 commit comments

Comments
 (0)