|
| 1 | +package plugins |
| 2 | + |
| 3 | +import ( |
| 4 | + "codacy/cli-v2/config" |
| 5 | + "codacy/cli-v2/utils" |
| 6 | + "embed" |
| 7 | + "fmt" |
| 8 | + "log" |
| 9 | + "os" |
| 10 | + "path" |
| 11 | + "path/filepath" |
| 12 | + "runtime" |
| 13 | + "strings" |
| 14 | + "text/template" |
| 15 | + |
| 16 | + "gopkg.in/yaml.v2" |
| 17 | +) |
| 18 | + |
| 19 | +//go:embed runtimes/*/plugin.yaml |
| 20 | +var runtimePlugins embed.FS |
| 21 | + |
| 22 | +type PluginSpec struct { |
| 23 | + Name string `yaml:"name"` |
| 24 | + Description string `yaml:"description"` |
| 25 | + Download struct { |
| 26 | + URLTemplate string `yaml:"url_template"` |
| 27 | + FileNameTemplate string `yaml:"file_name_template"` |
| 28 | + Extension map[string]string `yaml:"extension"` |
| 29 | + ArchMapping map[string]string `yaml:"arch_mapping"` |
| 30 | + } `yaml:"download"` |
| 31 | + Binaries []struct { |
| 32 | + Name string `yaml:"name"` |
| 33 | + Path string `yaml:"path"` |
| 34 | + } `yaml:"binaries"` |
| 35 | +} |
| 36 | + |
| 37 | +// getPluginSpec loads a runtime plugin specification by name |
| 38 | +func getPluginSpec(runtimeName string) (*PluginSpec, error) { |
| 39 | + pluginPath := fmt.Sprintf("runtimes/%s/plugin.yaml", runtimeName) |
| 40 | + |
| 41 | + data, err := runtimePlugins.ReadFile(pluginPath) |
| 42 | + if err != nil { |
| 43 | + return nil, fmt.Errorf("failed to read plugin specification for runtime %s: %v", runtimeName, err) |
| 44 | + } |
| 45 | + |
| 46 | + var spec PluginSpec |
| 47 | + if err := yaml.Unmarshal(data, &spec); err != nil { |
| 48 | + return nil, fmt.Errorf("failed to parse plugin specification for runtime %s: %v", runtimeName, err) |
| 49 | + } |
| 50 | + |
| 51 | + return &spec, nil |
| 52 | +} |
| 53 | + |
| 54 | +// getExtension returns the appropriate file extension based on OS |
| 55 | +func getExtension(spec *PluginSpec, goos string) string { |
| 56 | + if ext, ok := spec.Download.Extension[goos]; ok { |
| 57 | + return ext |
| 58 | + } |
| 59 | + return spec.Download.Extension["default"] |
| 60 | +} |
| 61 | + |
| 62 | +// getArchMapping maps Go architecture to runtime architecture |
| 63 | +func getArchMapping(spec *PluginSpec, goarch string) string { |
| 64 | + if arch, ok := spec.Download.ArchMapping[goarch]; ok { |
| 65 | + return arch |
| 66 | + } |
| 67 | + return goarch |
| 68 | +} |
| 69 | + |
| 70 | +// executeTemplate processes a template string with provided data |
| 71 | +func executeTemplate(tmplString string, data map[string]string) (string, error) { |
| 72 | + tmpl, err := template.New("template").Parse(tmplString) |
| 73 | + if err != nil { |
| 74 | + return "", err |
| 75 | + } |
| 76 | + |
| 77 | + var result strings.Builder |
| 78 | + if err := tmpl.Execute(&result, data); err != nil { |
| 79 | + return "", err |
| 80 | + } |
| 81 | + |
| 82 | + return result.String(), nil |
| 83 | +} |
| 84 | + |
| 85 | +// generateRuntimeInfo creates runtime info based on the plugin specification |
| 86 | +func generateRuntimeInfo(r *config.Runtime, spec *PluginSpec, goos, goarch string) (map[string]string, error) { |
| 87 | + // Prepare template data |
| 88 | + data := map[string]string{ |
| 89 | + "Version": r.Version(), |
| 90 | + "OS": goos, |
| 91 | + "Arch": getArchMapping(spec, goarch), |
| 92 | + } |
| 93 | + |
| 94 | + // Process file name template |
| 95 | + fileName, err := executeTemplate(spec.Download.FileNameTemplate, data) |
| 96 | + if err != nil { |
| 97 | + return nil, fmt.Errorf("failed to generate file name: %v", err) |
| 98 | + } |
| 99 | + |
| 100 | + // Add fileName to template data for URL generation |
| 101 | + data["FileName"] = fileName |
| 102 | + data["Extension"] = getExtension(spec, goos) |
| 103 | + |
| 104 | + info := map[string]string{ |
| 105 | + "fileName": fileName, |
| 106 | + "installDir": path.Join(config.Config.RuntimesDirectory(), fileName), |
| 107 | + } |
| 108 | + |
| 109 | + // Add binary paths |
| 110 | + for _, binary := range spec.Binaries { |
| 111 | + info[binary.Name] = path.Join(config.Config.RuntimesDirectory(), fileName, binary.Path) |
| 112 | + } |
| 113 | + |
| 114 | + return info, nil |
| 115 | +} |
| 116 | + |
| 117 | +// getDownloadURL generates the download URL for a runtime |
| 118 | +func getDownloadURL(r *config.Runtime, spec *PluginSpec, goos, goarch string) (string, error) { |
| 119 | + // Generate file name and prepare template data |
| 120 | + info, err := generateRuntimeInfo(r, spec, goos, goarch) |
| 121 | + if err != nil { |
| 122 | + return "", err |
| 123 | + } |
| 124 | + |
| 125 | + data := map[string]string{ |
| 126 | + "Version": r.Version(), |
| 127 | + "OS": goos, |
| 128 | + "Arch": getArchMapping(spec, goarch), |
| 129 | + "FileName": info["fileName"], |
| 130 | + "Extension": getExtension(spec, goos), |
| 131 | + } |
| 132 | + |
| 133 | + // Process URL template |
| 134 | + url, err := executeTemplate(spec.Download.URLTemplate, data) |
| 135 | + if err != nil { |
| 136 | + return "", fmt.Errorf("failed to generate download URL: %v", err) |
| 137 | + } |
| 138 | + |
| 139 | + return url, nil |
| 140 | +} |
| 141 | + |
| 142 | +// InstallRuntime installs a single runtime using its plugin specification |
| 143 | +func InstallRuntime(r *config.Runtime) error { |
| 144 | + goos := runtime.GOOS |
| 145 | + goarch := runtime.GOARCH |
| 146 | + |
| 147 | + spec, err := getPluginSpec(r.Name()) |
| 148 | + if err != nil { |
| 149 | + return err |
| 150 | + } |
| 151 | + |
| 152 | + downloadURL, err := getDownloadURL(r, spec, goos, goarch) |
| 153 | + if err != nil { |
| 154 | + return err |
| 155 | + } |
| 156 | + |
| 157 | + fileName := filepath.Base(downloadURL) |
| 158 | + filePath := filepath.Join(config.Config.RuntimesDirectory(), fileName) |
| 159 | + |
| 160 | + // Check if runtime archive already exists |
| 161 | + t, err := os.Open(filePath) |
| 162 | + if err == nil { |
| 163 | + defer t.Close() |
| 164 | + log.Printf("%s is already downloaded, skipping download...", r.Name()) |
| 165 | + } else { |
| 166 | + // Download the runtime archive |
| 167 | + log.Printf("Downloading %s version %s...", r.Name(), r.Version()) |
| 168 | + downloadedFile, err := utils.DownloadFile(downloadURL, config.Config.RuntimesDirectory()) |
| 169 | + if err != nil { |
| 170 | + return fmt.Errorf("failed to download %s: %v", r.Name(), err) |
| 171 | + } |
| 172 | + |
| 173 | + t, err = os.Open(downloadedFile) |
| 174 | + if err != nil { |
| 175 | + return fmt.Errorf("failed to open downloaded file: %v", err) |
| 176 | + } |
| 177 | + defer t.Close() |
| 178 | + } |
| 179 | + |
| 180 | + log.Printf("Extracting %s...", r.Name()) |
| 181 | + |
| 182 | + // Extract based on file extension |
| 183 | + extension := getExtension(spec, goos) |
| 184 | + if extension == "zip" { |
| 185 | + err = utils.ExtractZip(filePath, config.Config.RuntimesDirectory()) |
| 186 | + } else { |
| 187 | + err = utils.ExtractTarGz(t, config.Config.RuntimesDirectory()) |
| 188 | + } |
| 189 | + |
| 190 | + if err != nil { |
| 191 | + return fmt.Errorf("failed to extract %s: %v", r.Name(), err) |
| 192 | + } |
| 193 | + |
| 194 | + log.Printf("Successfully installed %s version %s", r.Name(), r.Version()) |
| 195 | + return nil |
| 196 | +} |
| 197 | + |
| 198 | +// InstallRuntimes installs multiple runtimes based on the provided map |
| 199 | +func InstallRuntimes(runtimes map[string]*config.Runtime) error { |
| 200 | + for _, runtime := range runtimes { |
| 201 | + if err := InstallRuntime(runtime); err != nil { |
| 202 | + return fmt.Errorf("failed to install runtime %s: %v", runtime.Name(), err) |
| 203 | + } |
| 204 | + } |
| 205 | + return nil |
| 206 | +} |
0 commit comments