-
Notifications
You must be signed in to change notification settings - Fork 8
/
fetch.go
177 lines (157 loc) · 4.09 KB
/
fetch.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
package main
import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
)
type ParseFunc func(v interface{}) *Versions
var (
ReLuaRocksVer = regexp.MustCompile(`(?si)href="(luarocks-([^"]+?)\.tar\.gz).*?href="(luarocks-[^"]+?\.tar\.gz\.asc)"`)
ReLuaVer = regexp.MustCompile(`(?si)<tr>\s*<td class="name"><a href="(lua-([^"]+?)\.tar\.gz).+?class="sum">([0-9a-f]+)</td>\s*</tr>`)
)
func parseLuaRocksVers(v interface{}) *Versions {
body := v.([]byte)
vers := NewVersions()
for _, m := range ReLuaRocksVer.FindAllSubmatch(body, -1) {
if !bytes.HasPrefix(m[3], m[1]) {
continue
}
name := string(m[1])
ver := string(m[2])
sum := "pgp:" + string(m[3])
if !vers.Add(name, ver, sum, ".tar.gz") {
printf("ignore unsupported version: %q", name)
}
}
return vers
}
func parseLuaJitVers(v interface{}) *Versions {
repodir := v.(string)
err := os.Chdir(repodir)
if err != nil {
eprintf("failed to chdir: %v", err)
return nil
}
defer os.Chdir(CWD)
// get list of remote branches
out := &bytes.Buffer{}
err = DoExecEx("git", out, os.Stderr, "branch", "-r")
if err != nil {
eprintf("failed to get branches: %v", err)
return nil
}
// parse branches
vers := NewVersions()
for _, line := range strings.Split(out.String(), "\n") {
line = strings.TrimSpace(line)
if strings.Contains(line, " ") {
// ignore unsupported branch
continue
}
// split line to remote and branch
// e.g. origin/HEAD -> "origin", "master"
origin := strings.Split(line, "/")
if len(origin) != 2 {
// ignore unsupported branch
continue
}
vers.AddBranch(origin[1], origin[0])
}
return vers
}
func parseLuaVers(v interface{}) *Versions {
body := v.([]byte)
vers := NewVersions()
for _, m := range ReLuaVer.FindAllSubmatch(body, -1) {
name := string(m[1])
ver := string(m[2])
sum := "sha256:" + string(m[3])
if !vers.Add(name, ver, sum, ".tar.gz") {
printf("ignore unsupported version: %q", name)
}
}
return vers
}
func makeVerFile(cfg *TargetConfig, vers *Versions) {
if err := vers.WriteFile(cfg.VersionFile); err != nil {
eprintf("failed to write version file: %v\n", err)
return
}
items, maxlen := vers.GetList()
for _, item := range items {
if cfg.RepoDir != "" {
format := fmt.Sprintf("%%-%ds %%s/%%s %%s", maxlen)
printf(format, item.Name, item.Remote, item.Name, cfg.ReleaseURL)
continue
}
format := fmt.Sprintf("%%-%ds %%s", maxlen)
url := cfg.DownloadURL + filepath.Clean(item.Name)
printf(format, item.Ver, url)
}
}
func CmdFetch() {
for _, target := range []struct {
cfg *TargetConfig
parse ParseFunc
}{
{LuaCfg, parseLuaVers},
{LuaJitCfg, parseLuaJitVers},
{LuaRocksCfg, parseLuaRocksVers},
} {
printf(
"\nfetch list of %q versions from %q...",
target.cfg.Name, target.cfg.ReleaseURL,
)
var vers *Versions
if target.cfg.RepoDir != "" {
// make temp dir
tmpdir, err := os.MkdirTemp(os.TempDir(), "lenv-")
if err != nil {
eprintf("failed to create temp dir: %v\n", err)
continue
}
defer os.RemoveAll(tmpdir)
// shallow clone with all branches
err = DoExec("git", "clone", "--depth", "1", "--no-single-branch", target.cfg.ReleaseURL, tmpdir)
if err != nil {
eprintf("failed to clone %q: %v\n", target.cfg.ReleaseURL, err)
continue
}
os.RemoveAll(target.cfg.RepoDir)
if err = os.Rename(tmpdir, target.cfg.RepoDir); err != nil {
eprintf("failed to rename %q to %q: %v\n", tmpdir, target.cfg.RepoDir, err)
continue
}
vers = target.parse(target.cfg.RepoDir)
} else {
rsp, err := http.Get(target.cfg.ReleaseURL)
if err != nil {
eprintf("failed to download %q: %v\n", target.cfg.ReleaseURL, err)
continue
}
defer rsp.Body.Close()
// check status code
if rsp.StatusCode != http.StatusOK {
eprintf("failed to download %q: %s\n", target.cfg.ReleaseURL, rsp.Status)
continue
}
b, err := io.ReadAll(rsp.Body)
if err != nil {
eprintf("failed to read body: %v\n", err)
continue
}
vers = target.parse(b)
}
if vers != nil {
makeVerFile(target.cfg, vers)
}
printf("")
}
printf("")
printf("done")
}