-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathruntime-utils.go
More file actions
137 lines (112 loc) · 4.11 KB
/
runtime-utils.go
File metadata and controls
137 lines (112 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package plugins
import (
"embed"
"fmt"
"path"
"runtime"
"strings"
"gopkg.in/yaml.v3"
)
//go:embed runtimes/*/plugin.yaml
var pluginsFS embed.FS
// ProcessRuntimes processes a list of runtime configurations and returns a map of runtime information
func ProcessRuntimes(configs []RuntimeConfig, runtimesDir string) (map[string]*RuntimeInfo, error) {
result := make(map[string]*RuntimeInfo)
for _, config := range configs {
runtimeInfo, err := processRuntime(config, runtimesDir)
if err != nil {
return nil, err
}
result[config.Name] = runtimeInfo
}
return result, nil
}
// ProcessRuntime processes a single runtime configuration and returns detailed runtime info
func processRuntime(config RuntimeConfig, runtimesDir string) (*RuntimeInfo, error) {
pluginConfig, err := GetPluginManager().GetRuntimeConfig(config.Name)
if err != nil {
return nil, fmt.Errorf("failed to load plugin for runtime %s: %w", config.Name, err)
}
// Map Go architecture and OS to runtime-specific values
mappedArch := GetMappedArch(pluginConfig.Download.ArchMapping, runtime.GOARCH)
mappedOS := GetMappedOS(pluginConfig.Download.OSMapping, runtime.GOOS)
// Get the appropriate extension
extension := GetExtension(pluginConfig.Download.Extension, runtime.GOOS)
// Get the filename using the template
fileName := GetFileName(pluginConfig.Download.FileNameTemplate, config.Version, mappedArch, runtime.GOOS)
customURLConfig, hasCustomURL := getCustomDownloadURL(pluginConfig.Download.CustomURLConfig, runtime.GOOS)
// Get the download URL using the template
downloadURL := ""
if hasCustomURL {
downloadURL = GetDownloadURL(customURLConfig, fileName, config.Version, mappedArch, mappedOS, extension, pluginConfig.Download.ReleaseVersion)
} else {
downloadURL = GetDownloadURL(pluginConfig.Download.URLTemplate, fileName, config.Version, mappedArch, mappedOS, extension, pluginConfig.Download.ReleaseVersion)
}
// For Python, we want to use a simpler directory structure
var installDir string
if config.Name == "python" {
installDir = path.Join(runtimesDir, "python")
} else if config.Name == "go" {
installDir = path.Join(runtimesDir, "go")
} else {
installDir = path.Join(runtimesDir, fileName)
}
// Create RuntimeInfo with essential information
info := &RuntimeInfo{
Name: config.Name,
Version: config.Version,
InstallDir: installDir,
DownloadURL: downloadURL,
FileName: fileName,
Extension: extension,
Binaries: make(map[string]string),
}
// Process binary paths
for _, binary := range pluginConfig.Binaries {
var binaryPath string
switch path := binary.Path.(type) {
case string:
// If path is a simple string, use it directly
binaryPath = path
case map[string]interface{}:
// If path is a map, get the OS-specific path
if osPath, ok := path[runtime.GOOS]; ok {
binaryPath = osPath.(string)
} else {
return nil, fmt.Errorf("no binary path specified for OS %s", runtime.GOOS)
}
default:
return nil, fmt.Errorf("invalid path format for binary %s", binary.Name)
}
fullPath := path.Join(installDir, binaryPath)
// Add file extension for Windows executables
if runtime.GOOS == "windows" && !strings.HasSuffix(fullPath, ".exe") {
fullPath += ".exe"
}
info.Binaries[binary.Name] = fullPath
}
return info, nil
}
// GetRuntimeVersions returns a map of runtime names to their default versions
func GetRuntimeVersions() map[string]string {
return GetPluginManager().GetRuntimeVersions()
}
// LoadPlugin loads a plugin configuration from the specified plugin directory
func loadPlugin(runtimeName string) (*runtimePlugin, error) {
// Always use forward slashes for embedded filesystem paths (for windows support)
pluginPath := fmt.Sprintf("runtimes/%s/plugin.yaml", runtimeName)
// Read from embedded filesystem
data, err := pluginsFS.ReadFile(pluginPath)
if err != nil {
return nil, fmt.Errorf("error reading plugin.yaml: %w", err)
}
var config PluginConfig
err = yaml.Unmarshal(data, &config)
if err != nil {
return nil, fmt.Errorf("error parsing plugin.yaml: %w", err)
}
return &runtimePlugin{
Config: config,
ConfigPath: pluginPath,
}, nil
}