/
parser.go
330 lines (294 loc) · 9.88 KB
/
parser.go
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package pkgparse
import (
"errors"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/candrewlee14/webman/config"
"github.com/candrewlee14/webman/utils"
"github.com/fatih/color"
"gopkg.in/yaml.v3"
)
// RenameItem is for package renames when installing
type RenameItem struct {
From string `yaml:"from"`
To string `yaml:"to"`
}
// OsInfo is specific information for a package based on OS
type OsInfo struct {
Name string `yaml:"name"`
Ext string `yaml:"ext"`
BinPaths SingleOrMulti `yaml:"bin_path"`
ExtractHasRoot bool `yaml:"extract_has_root"`
IsRawBinary bool `yaml:"is_raw_binary"`
FilenameFormatOverride string `yaml:"filename_format_override"`
Renames []RenameItem `yaml:"renames"`
InstallNote string `yaml:"install_note"`
RemoveNote string `yaml:"remove_note"`
}
// OsArchPair is a mapping of OS to ARCH
type OsArchPair struct {
Os string `yaml:"os"`
Arch string `yaml:"arch"`
}
// PkgConfig is a package configuration
type PkgConfig struct {
Title string `yaml:"-"`
Tagline string `yaml:"tagline"`
About string `yaml:"about"`
InstallNote string `yaml:"install_note"`
RemoveNote string `yaml:"remove_note"`
InfoUrl string `yaml:"info_url"`
ReleasesUrl string `yaml:"releases_url"`
BaseDownloadUrl string `yaml:"base_download_url"`
GitUser string `yaml:"git_user"`
GitRepo string `yaml:"git_repo"`
GiteaURL string `yaml:"gitea_url"`
SourceUrl string `yaml:"source_url"`
FilenameFormat string `yaml:"filename_format"`
VersionFormat string `yaml:"version_format"`
LatestStrategy string `yaml:"latest_strategy"`
ForceLatest bool `yaml:"force_latest"`
AllowPrerelease bool `yaml:"allow_prerelease"`
ArchLinuxPkgName string `yaml:"arch_linux_pkg_name"`
OsMap map[string]OsInfo `yaml:"os_map"`
ArchMap map[string]string `yaml:"arch_map"`
Ignore []OsArchPair `yaml:"ignore"`
}
// InstallNotes combines package-level and OS-level installation notes
func (pkgConf *PkgConfig) InstallNotes() string {
var installNotes string
pkgOS := GOOStoPkgOs[utils.GOOS]
note := pkgConf.InstallNote
osNote := pkgConf.OsMap[pkgOS].InstallNote
if note != "" || osNote != "" {
installNotes += color.BlueString("== %s\n", pkgConf.Title)
}
if note != "" {
installNotes += color.YellowString(note) + "\n"
}
if osNote != "" {
installNotes += color.YellowString(osNote) + "\n"
}
return installNotes
}
// RemoveNotes combines package-level and OS-level removal notes
func (pkgConf *PkgConfig) RemoveNotes() string {
var removeNotes string
pkgOS := GOOStoPkgOs[utils.GOOS]
note := pkgConf.RemoveNote
osNote := pkgConf.OsMap[pkgOS].RemoveNote
if note != "" || osNote != "" {
removeNotes += color.BlueString("== %s\n", pkgConf.Title)
}
if note != "" {
removeNotes += color.YellowString(note) + "\n"
}
if osNote != "" {
removeNotes += color.YellowString(osNote) + "\n"
}
return removeNotes
}
// GOOStoPkgOs is a mapping of GOOS to webman-specific OS names
var GOOStoPkgOs = map[string]string{
"darwin": "macos",
"windows": "win",
"linux": "linux",
}
// SingleOrMulti is for YAML values that could be a single string or multiple
type SingleOrMulti struct {
Values []string
}
// UnmarshalYAML implements yaml.Unmarshaler
func (sm *SingleOrMulti) UnmarshalYAML(unmarshal func(interface{}) error) error {
var multi []string
err := unmarshal(&multi)
if err != nil {
var single string
err := unmarshal(&single)
if err != nil {
return err
}
sm.Values = make([]string, 1)
sm.Values[0] = single
} else {
sm.Values = multi
}
return nil
}
// GetMyBinPaths gets all bin paths for this pakcage on this OS
func (pkgConf *PkgConfig) GetMyBinPaths() ([]string, error) {
osStr, exists := GOOStoPkgOs[utils.GOOS]
if !exists {
return []string{}, fmt.Errorf("unsupported OS")
}
osInfo, exists := pkgConf.OsMap[osStr]
if !exists {
return []string{}, fmt.Errorf("package does not support this OS")
}
if osInfo.IsRawBinary {
return []string{pkgConf.Title}, nil
}
if len(osInfo.BinPaths.Values) == 0 {
osInfo.BinPaths = SingleOrMulti{[]string{""}}
}
return osInfo.BinPaths.Values, nil
}
// GetRenames gets all renames for this package on this OS
func (pkgConf *PkgConfig) GetRenames() ([]RenameItem, error) {
osStr, exists := GOOStoPkgOs[utils.GOOS]
if !exists {
return []RenameItem{}, fmt.Errorf("unsupported OS")
}
osInfo, exists := pkgConf.OsMap[osStr]
if !exists {
return []RenameItem{}, fmt.Errorf("package does not support this OS")
}
return osInfo.Renames, nil
}
// ParsePkgConfig parses an io.Reader as a package configuration
func ParsePkgConfig(name string, r io.Reader) (*PkgConfig, error) {
dat, err := io.ReadAll(r)
if err != nil {
return nil, err
}
var pkgConf PkgConfig
if err = yaml.Unmarshal(dat, &pkgConf); err != nil {
return nil, fmt.Errorf("unable to parse package recipe for %s: %v", name, err)
}
pkgConf.Title = name
pkgConf.BaseDownloadUrl = strings.ReplaceAll(pkgConf.BaseDownloadUrl, "[GIT_USER]", pkgConf.GitUser)
pkgConf.BaseDownloadUrl = strings.ReplaceAll(pkgConf.BaseDownloadUrl, "[GIT_REPO]", pkgConf.GitRepo)
pkgConf.InfoUrl = strings.ReplaceAll(pkgConf.InfoUrl, "[GIT_USER]", pkgConf.GitUser)
pkgConf.InfoUrl = strings.ReplaceAll(pkgConf.InfoUrl, "[GIT_REPO]", pkgConf.GitRepo)
pkgConf.ReleasesUrl = strings.ReplaceAll(pkgConf.InfoUrl, "[GIT_USER]", pkgConf.GitUser)
pkgConf.ReleasesUrl = strings.ReplaceAll(pkgConf.InfoUrl, "[GIT_REPO]", pkgConf.GitRepo)
pkgConf.SourceUrl = strings.ReplaceAll(pkgConf.SourceUrl, "[GIT_USER]", pkgConf.GitUser)
pkgConf.SourceUrl = strings.ReplaceAll(pkgConf.SourceUrl, "[GIT_REPO]", pkgConf.GitRepo)
pkgConf.GiteaURL = strings.TrimRight(pkgConf.GiteaURL, "/")
return &pkgConf, nil
}
// ParsePkgConfigLocal checks all known repos for a given package
func ParsePkgConfigLocal(pkgRepos []*config.PkgRepo, pkg string) (*PkgConfig, error) {
var pkgConfPath string
for _, pkgRepo := range pkgRepos {
pkgPath := filepath.Join(pkgRepo.Path(), "pkgs", pkg+utils.PkgRecipeExt)
_, err := os.Stat(pkgPath)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
return nil, err
}
continue
}
pkgConfPath = pkgPath
break
}
if pkgConfPath == "" {
return nil, fmt.Errorf("no package recipe exists for %s", pkg)
}
fi, err := os.Open(pkgConfPath)
if err != nil {
return nil, err
}
defer fi.Close()
return ParsePkgConfig(pkg, fi)
}
// GetLatestVersion uses the configuration's latest-strategy to determine the latest version of the package
func (pkgConf *PkgConfig) GetLatestVersion() (*string, error) {
var version string
switch pkgConf.LatestStrategy {
case "github-release":
rel, err := getLatestGithubReleaseTag(pkgConf.GitUser, pkgConf.GitRepo, pkgConf.AllowPrerelease)
if err != nil {
return nil, err
}
version = rel.TagName
case "arch-linux-community":
rel, err := getLatestArchLinuxPkgVersion(pkgConf.ArchLinuxPkgName)
if err != nil {
return nil, err
}
version = rel.PkgVer
case "gitea-release":
rel, err := getLatestGiteaReleaseTag(pkgConf.GiteaURL, pkgConf.GitUser, pkgConf.GitRepo, pkgConf.AllowPrerelease)
if err != nil {
return nil, err
}
version = rel.TagName
}
if version == "" {
return nil, fmt.Errorf("no implemented latest version resolution strategy for %q",
pkgConf.LatestStrategy)
}
parsedVer, err := ParseVersion(version, pkgConf.VersionFormat)
if err != nil {
return nil, fmt.Errorf("unable to parse version: %v", err)
}
return parsedVer, nil
}
func ParseVersion(versionStr string, versionFmt string) (*string, error) {
if versionFmt == "" {
versionFmt = "[VER]"
}
versionMatchExp := strings.Replace(versionFmt, "[VER]", "(.+)", 1)
exp, err := regexp.Compile(versionMatchExp)
if err != nil {
return nil, fmt.Errorf("failed to compile regex based on version_format: %v", err)
}
matchedVer := exp.FindStringSubmatch(versionStr)
if len(matchedVer) != 2 || matchedVer[1] == "" {
return nil, fmt.Errorf("failed to match version %q with given version_format", versionStr)
}
return &matchedVer[1], nil
}
func (pkgConf *PkgConfig) GetAssetStemExtUrl(version string) (*string, *string, *string, error) {
pkgOs, exists := GOOStoPkgOs[utils.GOOS]
if !exists {
return nil, nil, nil, fmt.Errorf("unsupported operating system")
}
osInf, exists := pkgConf.OsMap[pkgOs]
if !exists {
return nil, nil, nil, fmt.Errorf("package has no binary for operating system: %s", pkgOs)
}
archStr, exists := pkgConf.ArchMap[utils.GOARCH]
if !exists {
return nil, nil, nil, fmt.Errorf("package has no binary for architecture: %s", utils.GOARCH)
}
baseUrl := pkgConf.BaseDownloadUrl
baseUrl = strings.ReplaceAll(baseUrl, "[VER]", version)
baseUrl = strings.ReplaceAll(baseUrl, "[OS]", osInf.Name)
baseUrl = strings.ReplaceAll(baseUrl, "[ARCH]", archStr)
baseUrl = strings.ReplaceAll(baseUrl, "[EXT]", osInf.Ext)
fileStem := pkgConf.FilenameFormat
if osInf.FilenameFormatOverride != "" {
fileStem = osInf.FilenameFormatOverride
}
fileStem = strings.ReplaceAll(fileStem, "[VER]", version)
fileStem = strings.ReplaceAll(fileStem, "[OS]", osInf.Name)
fileStem = strings.ReplaceAll(fileStem, "[ARCH]", archStr)
fileStem = strings.ReplaceAll(fileStem, ".[EXT]", "")
dot := ""
if osInf.Ext != "" {
dot = "."
}
stem := baseUrl + fileStem + dot + osInf.Ext
return &fileStem, &osInf.Ext, &stem, nil
}
// ParsePkgConfigPath parses a package configuration from a repo path (~/.webman/recipes/webman) and package name (age)
// It combines them to parse (~/.webman/recipes/webman/pkgs/age.webman-pkg.yaml) and assigns the name (age)
func ParsePkgConfigPath(repoPath, pkg string) (*PkgConfig, error) {
fi, err := os.Open(filepath.Join(repoPath, "pkgs", pkg+utils.PkgRecipeExt))
if err != nil {
return nil, err
}
defer fi.Close()
pkgConfig, err := ParsePkgConfig(pkg, fi)
if err != nil {
return nil, err
}
return pkgConfig, nil
}