-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdownload.go
304 lines (253 loc) · 7.09 KB
/
download.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
package main
import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
alpm "github.com/jguer/go-alpm"
)
// Decide what download method to use:
// Use the config option when the destination does not already exits
// If .git exists in the destination use git
// Otherwise use a tarrball
func shouldUseGit(path string) bool {
_, err := os.Stat(path)
if os.IsNotExist(err) {
return config.GitClone
}
_, err = os.Stat(filepath.Join(path, ".git"))
return err == nil || os.IsExist(err)
}
func downloadFile(path string, url string) (err error) {
// Create the file
out, err := os.Create(path)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Writer the body to file
_, err = io.Copy(out, resp.Body)
return err
}
func gitHasDiff(path string, name string) (bool, error) {
stdout, stderr, err := capture(passToGit(filepath.Join(path, name), "rev-parse", "HEAD", "HEAD@{upstream}"))
if err != nil {
return false, fmt.Errorf("%s%s", stderr, err)
}
lines := strings.Split(stdout, "\n")
head := lines[0]
upstream := lines[1]
return head != upstream, nil
}
func gitDownload(url string, path string, name string) (bool, error) {
_, err := os.Stat(filepath.Join(path, name, ".git"))
if os.IsNotExist(err) {
cmd := passToGit(path, "clone", "--no-progress", url, name)
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
_, stderr, err := capture(cmd)
if err != nil {
return false, fmt.Errorf("error cloning %s: %s", name, stderr)
}
return true, nil
} else if err != nil {
return false, fmt.Errorf("error reading %s", filepath.Join(path, name, ".git"))
}
cmd := passToGit(filepath.Join(path, name), "fetch")
cmd.Env = append(os.Environ(), "GIT_TERMINAL_PROMPT=0")
_, stderr, err := capture(cmd)
if err != nil {
return false, fmt.Errorf("error fetching %s: %s", name, stderr)
}
return false, nil
}
func gitMerge(path string, name string) error {
_, stderr, err := capture(passToGit(filepath.Join(path, name), "reset", "--hard", "HEAD"))
if err != nil {
return fmt.Errorf("error resetting %s: %s", name, stderr)
}
_, stderr, err = capture(passToGit(filepath.Join(path, name), "merge", "--no-edit", "--ff"))
if err != nil {
return fmt.Errorf("error merging %s: %s", name, stderr)
}
return nil
}
func gitDiff(path string, name string) error {
err := show(passToGit(filepath.Join(path, name), "diff", "HEAD..HEAD@{upstream}"))
return err
}
// DownloadAndUnpack downloads url tgz and extracts to path.
func downloadAndUnpack(url string, path string) error {
err := os.MkdirAll(path, 0755)
if err != nil {
return err
}
fileName := filepath.Base(url)
tarLocation := filepath.Join(path, fileName)
defer os.Remove(tarLocation)
err = downloadFile(tarLocation, url)
if err != nil {
return err
}
_, stderr, err := capture(exec.Command(config.TarBin, "-xf", tarLocation, "-C", path))
if err != nil {
return fmt.Errorf("%s", stderr)
}
return nil
}
func getPkgbuilds(pkgs []string) error {
missing := false
wd, err := os.Getwd()
if err != nil {
return err
}
pkgs = removeInvalidTargets(pkgs)
aur, repo, err := packageSlices(pkgs)
for n := range aur {
_, pkg := splitDBFromName(aur[n])
aur[n] = pkg
}
info, err := aurInfoPrint(aur)
if err != nil {
return err
}
if len(repo) > 0 {
missing, err = getPkgbuildsfromABS(repo, wd)
if err != nil {
return err
}
}
if len(aur) > 0 {
allBases := getBases(info)
bases := make([]Base, 0)
for _, base := range allBases {
name := base.Pkgbase()
_, err = os.Stat(filepath.Join(wd, name))
switch {
case err != nil && !os.IsNotExist(err):
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
continue
case os.IsNotExist(err), cmdArgs.existsArg("f", "force"), shouldUseGit(filepath.Join(wd, name)):
if err = os.RemoveAll(filepath.Join(wd, name)); err != nil {
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
continue
}
default:
fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
continue
}
bases = append(bases, base)
}
if _, err = downloadPkgbuilds(bases, nil, wd); err != nil {
return err
}
missing = missing || len(aur) != len(info)
}
if missing {
err = fmt.Errorf("")
}
return err
}
// GetPkgbuild downloads pkgbuild from the ABS.
func getPkgbuildsfromABS(pkgs []string, path string) (bool, error) {
var wg sync.WaitGroup
var mux sync.Mutex
var errs MultiError
names := make(map[string]string)
missing := make([]string, 0)
downloaded := 0
dbList, err := alpmHandle.SyncDBs()
if err != nil {
return false, err
}
for _, pkgN := range pkgs {
var pkg *alpm.Package
var err error
var url string
pkgDB, name := splitDBFromName(pkgN)
if pkgDB != "" {
if db, err := alpmHandle.SyncDBByName(pkgDB); err == nil {
pkg = db.Pkg(name)
}
} else {
dbList.ForEach(func(db alpm.DB) error {
if pkg = db.Pkg(name); pkg != nil {
return fmt.Errorf("")
}
return nil
})
}
if pkg == nil {
missing = append(missing, name)
continue
}
name = pkg.Base()
if name == "" {
name = pkg.Name()
}
switch pkg.DB().Name() {
case "core", "extra", "testing":
url = "https://git.archlinux.org/svntogit/packages.git/snapshot/packages/" + name + ".tar.gz"
case "community", "multilib", "community-testing", "multilib-testing":
url = "https://git.archlinux.org/svntogit/community.git/snapshot/packages/" + name + ".tar.gz"
default:
missing = append(missing, name)
continue
}
_, err = os.Stat(filepath.Join(path, name))
switch {
case err != nil && !os.IsNotExist(err):
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
continue
case os.IsNotExist(err), cmdArgs.existsArg("f", "force"):
if err = os.RemoveAll(filepath.Join(path, name)); err != nil {
fmt.Fprintln(os.Stderr, bold(red(smallArrow)), err)
continue
}
default:
fmt.Printf("%s %s %s\n", yellow(smallArrow), cyan(name), "already downloaded -- use -f to overwrite")
continue
}
names[name] = url
}
if len(missing) != 0 {
fmt.Println(yellow(bold(smallArrow)), "Missing ABS packages: ", cyan(strings.Join(missing, " ")))
}
download := func(pkg string, url string) {
defer wg.Done()
if err := downloadAndUnpack(url, cacheHome); err != nil {
errs.Add(fmt.Errorf("%s Failed to get pkgbuild: %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(err.Error()))))
return
}
_, stderr, err := capture(exec.Command("mv", filepath.Join(cacheHome, "packages", pkg, "trunk"), filepath.Join(path, pkg)))
mux.Lock()
downloaded++
if err != nil {
errs.Add(fmt.Errorf("%s Failed to move %s: %s", bold(red(arrow)), bold(cyan(pkg)), bold(red(string(stderr)))))
} else {
fmt.Printf(bold(cyan("::"))+" Downloaded PKGBUILD from ABS (%d/%d): %s\n", downloaded, len(names), cyan(pkg))
}
mux.Unlock()
}
count := 0
for name, url := range names {
wg.Add(1)
go download(name, url)
count++
if count%25 == 0 {
wg.Wait()
}
}
wg.Wait()
errs.Add(os.RemoveAll(filepath.Join(cacheHome, "packages")))
return len(missing) != 0, errs.Return()
}