This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
base.go
77 lines (68 loc) · 1.84 KB
/
base.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
package rules
import (
"errors"
"fmt"
"strings"
c "github.com/just-install/just-install-updater-go/jiup/rules/common"
)
// R represents a rule for an application.
type R struct {
V c.VersionExtractorFunc
D c.DownloadExtractorFunc
}
var rules = map[string]R{}
// Rule registers a rule.
func Rule(pkg string, versionExtractor c.VersionExtractorFunc, downloadExtractor c.DownloadExtractorFunc) {
if _, ok := rules[pkg]; ok {
panic("rule for " + pkg + " already registered")
}
rules[pkg] = R{wrapV(versionExtractor), wrapD(downloadExtractor)}
}
// GetRule gets a rule if it exists.
func GetRule(pkg string) (c.VersionExtractorFunc, c.DownloadExtractorFunc, bool) {
if rule, ok := rules[pkg]; ok {
return rule.V, rule.D, true
}
return nil, nil, false
}
// GetRules gets all rules.
func GetRules() map[string]R {
return rules
}
func wrapV(f c.VersionExtractorFunc) c.VersionExtractorFunc {
return func() (version string, err error) {
version, err = f()
if err != nil {
return "", err
}
if strings.TrimSpace(version) == "" {
return "", errors.New("version is empty")
}
return version, nil
}
}
func wrapD(f c.DownloadExtractorFunc) c.DownloadExtractorFunc {
return func(version string) (x86 *string, x86_64 *string, err error) {
x86, x86_64, err = f(version)
if x86 != nil {
if err != nil {
return nil, nil, err
}
if strings.TrimSpace(*x86) == "" {
return nil, nil, errors.New("x86 link is empty")
}
if !strings.HasPrefix(*x86, "http") {
return nil, nil, fmt.Errorf("x86 link (%s) does not start with http", *x86)
}
}
if x86_64 != nil {
if strings.TrimSpace(*x86_64) == "" {
return nil, nil, errors.New("x86_64 link is empty")
}
if !strings.HasPrefix(*x86_64, "http") {
return nil, nil, fmt.Errorf("x86_64 link (%s) does not start with http", *x86_64)
}
}
return x86, x86_64, nil
}
}