-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathshared.go
More file actions
189 lines (167 loc) · 5.04 KB
/
shared.go
File metadata and controls
189 lines (167 loc) · 5.04 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package plugins
import (
"bytes"
"strings"
"text/template"
)
// ExtensionConfig defines the file extension based on OS
type ExtensionConfig struct {
Linux string `yaml:"linux"`
Windows string `yaml:"windows"`
Default string `yaml:"default"`
}
type CustomURLConfig struct {
Linux string `yaml:"linux"`
Windows string `yaml:"windows"`
MacOS string `yaml:"macos"`
Default string `yaml:"default"`
}
// DownloadConfig holds the download configuration from the plugin.yaml
type DownloadConfig struct {
URLTemplate string `yaml:"url_template"`
CustomURLConfig CustomURLConfig `yaml:"custom_url_config,omitempty"`
FileNameTemplate string `yaml:"file_name_template"`
Extension ExtensionConfig `yaml:"extension"`
ArchMapping map[string]string `yaml:"arch_mapping"`
OSMapping map[string]string `yaml:"os_mapping"`
ReleaseVersion string `yaml:"release_version,omitempty"`
}
// Binary represents a binary executable provided by the runtime or tool
type Binary struct {
Name string `yaml:"name"`
Path interface{} `yaml:"path"` // Can be either string or map[string]string
}
// PluginConfig holds the structure of the plugin.yaml file
type PluginConfig struct {
Name string `yaml:"name"`
Description string `yaml:"description"`
Download DownloadConfig `yaml:"download"`
Binaries []Binary `yaml:"binaries"`
DefaultVersion string `yaml:"default_version"`
}
// RuntimeConfig represents configuration for a runtime
type RuntimeConfig struct {
Name string
Version string
}
// RuntimeInfo contains all processed information about a runtime
type RuntimeInfo struct {
Name string
Version string
InstallDir string
DownloadURL string
FileName string
Extension string
Binaries map[string]string // Map of binary name to full path
}
// templateData holds the data to be used in template substitution
type templateData struct {
Version string
MajorVersion string
FileName string
OS string
Arch string
Extension string
ReleaseVersion string
}
// GetMappedArch returns the architecture mapping for the current system
func GetMappedArch(archMapping map[string]string, goarch string) string {
// Check if there's a mapping for this architecture
if mappedArch, ok := archMapping[goarch]; ok {
return mappedArch
}
// Return the original architecture if no mapping exists
return goarch
}
// GetExtension returns the appropriate file extension based on the OS
func GetExtension(extension ExtensionConfig, goos string) string {
if goos == "windows" {
return extension.Windows
}
if goos == "linux" && extension.Linux != "" {
return extension.Linux
}
return extension.Default
}
func getCustomDownloadURL(customURLConfig CustomURLConfig, goos string) (string, bool) {
switch goos {
case "linux":
if customURLConfig.Linux != "" {
return customURLConfig.Linux, true
}
case "windows":
if customURLConfig.Windows != "" {
return customURLConfig.Windows, true
}
case "darwin":
if customURLConfig.MacOS != "" {
return customURLConfig.MacOS, true
}
}
if customURLConfig.Default != "" {
return customURLConfig.Default, true
}
return "", false
}
// GetMajorVersion extracts the major version from a version string (e.g. "17.0.10" -> "17")
func GetMajorVersion(version string) string {
if idx := strings.Index(version, "."); idx != -1 {
return version[:idx]
}
return version
}
// GetFileName generates the filename based on the template
func GetFileName(fileNameTemplate string, version string, mappedArch string, goos string) string {
// Prepare template data
data := templateData{
Version: version,
MajorVersion: GetMajorVersion(version),
OS: goos,
Arch: mappedArch,
}
// Execute template substitution for filename
tmpl, err := template.New("filename").Parse(fileNameTemplate)
if err != nil {
return ""
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, data)
if err != nil {
return ""
}
return buf.String()
}
// GetDownloadURL generates the download URL based on the template
func GetDownloadURL(urlTemplate string, fileName string, version string, mappedArch string, mappedOS string, extension string, releaseVersion string) string {
// Prepare template data
data := templateData{
Version: version,
MajorVersion: GetMajorVersion(version),
FileName: fileName,
OS: mappedOS,
Arch: mappedArch,
Extension: extension,
ReleaseVersion: releaseVersion,
}
// Execute template substitution for URL
tmpl, err := template.New("url").Parse(urlTemplate)
if err != nil {
return ""
}
var buf bytes.Buffer
err = tmpl.Execute(&buf, data)
if err != nil {
return ""
}
url := buf.String()
return url
}
// GetMappedOS returns the OS mapping for the current system
func GetMappedOS(osMapping map[string]string, goos string) string {
// Check if there's a mapping for this OS
if mappedOS, ok := osMapping[goos]; ok {
return mappedOS
}
// Return the original OS if no mapping exists
return goos
}