-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
81 lines (63 loc) · 1.58 KB
/
client.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
package apk
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
)
var (
APKIndexTarball = "APKINDEX.tar.gz"
BaseDir = "/var/cache/vinyl/vin/apk"
)
// Client connects to the Alpine package CDN in order to downloan
// indices, packages
type Client struct {
BaseURL string
}
// DownloadIndex downloads an index file to a temporary directory,
// ready for parsing and processing, usually with apk.NewAPKIndex()
func (c Client) DownloadIndex() (path string, err error) {
u := fmt.Sprintf("%s/%s", c.BaseURL, APKIndexTarball)
dir, err := ioutil.TempDir("", "")
if err != nil {
return
}
return download(u, dir)
}
// DownloadPackage accepts a package, as parsed from an APKIndex file, and
// stores it in the vinyl cache directory
func (c Client) DownloadPackage(pkg *Package) (path string, err error) {
u := fmt.Sprintf("%s/%s-%s.apk", c.BaseURL, pkg.Name, pkg.Version)
dir := filepath.Join(BaseDir, pkg.Name, pkg.Version)
err = os.MkdirAll(dir, 0750)
if err != nil {
return
}
return download(u, dir)
}
func download(u string, dir string) (path string, err error) {
u1, err := url.Parse(u)
if err != nil {
return
}
path = filepath.Join(dir, filepath.Base(u1.Path))
out, err := os.Create(path) // #nosec: G304
if err != nil {
return
}
defer out.Close() // #nosec: G307
resp, err := http.Get(u1.String())
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = fmt.Errorf("downloading %s: expected status 200, received %s", u1, resp.Status)
return
}
_, err = io.Copy(out, resp.Body)
return
}