|
| 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