-
Notifications
You must be signed in to change notification settings - Fork 3
/
dl.go
178 lines (164 loc) · 3.75 KB
/
dl.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
package ghdl
import (
"archive/tar"
"archive/zip"
"compress/gzip"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"github.com/beetcb/ghdl/helper/pg"
humanize "github.com/dustin/go-humanize"
)
var (
ErrNeedInstall = errors.New(
"detected deb/rpm/apk package, download directly")
ErrNoBin = errors.New("binary file not found")
)
type GHReleaseDl struct {
BinaryName string
Url string
Size int64
}
// Download asset from github release to `path`
//
// dl.BinaryName shall be replaced with absolute path mutably
func (dl *GHReleaseDl) DlTo(path string) (err error) {
dl.BinaryName, err = filepath.Abs(filepath.Join(path, dl.BinaryName))
if err != nil {
return err
}
req, err := http.NewRequest("GET", dl.Url, nil)
if err != nil {
return err
}
req.Header.Set("Accept", "application/vnd.github.v3+json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
tmpfile, err := os.Create(dl.BinaryName + ".tmp")
if err != nil {
return err
}
defer tmpfile.Close()
// create progress tui
starter := func(updater func(float64)) {
if _, err := io.Copy(tmpfile, &pg.ProgressBytesReader{Reader: resp.Body, Handler: func(p int) {
updater(float64(p) / float64(dl.Size))
}}); err != nil {
panic(err)
}
}
pg.Progress(starter, humanize.Bytes(uint64(dl.Size)))
return nil
}
// Extract binary file from the downloaded temporary file.
//
// Currently supporting unarchiving `tar` and decompressing `zip` `gravezip`.
//
// Package format `deb` `rpm` `apk` will be downloaded directly
func (dl GHReleaseDl) ExtractBinary() error {
tmpfileName := dl.BinaryName + ".tmp"
openfile, err := os.Open(tmpfileName)
if err != nil {
return err
}
fileExt := filepath.Ext(dl.Url)
var decompressedBinary io.Reader
switch fileExt {
case ".zip":
zipFile, err := dl.UnZipBinary(openfile)
if err != nil {
return err
}
decompressedBinary, err = zipFile.Open()
if err != nil {
return err
}
case ".gz":
if strings.Contains(dl.Url, ".tar.gz") {
decompressedBinary, err = dl.UnTargzBinary(openfile)
if err != nil {
return err
}
} else {
decompressedBinary, err = dl.UnGzBinary(openfile)
if err != nil {
return err
}
}
case "":
decompressedBinary = openfile
case ".deb", ".rpm", ".apk", ".msi", ".exe", ".dmg":
fileName := dl.BinaryName + fileExt
if err := os.Rename(tmpfileName, fileName); err != nil {
panic(err)
}
return ErrNeedInstall
default:
defer os.Remove(tmpfileName)
return fmt.Errorf("unsupported file format: %v", fileExt)
}
defer os.Remove(tmpfileName)
defer openfile.Close()
out, err := os.Create(dl.BinaryName)
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, decompressedBinary); err != nil {
return err
}
return nil
}
func (dl GHReleaseDl) UnZipBinary(r *os.File) (*zip.File, error) {
b := filepath.Base(dl.BinaryName)
zipR, err := zip.NewReader(r, dl.Size)
if err != nil {
return nil, err
}
for _, f := range zipR.File {
if filepath.Base(f.Name) == b || len(zipR.File) == 1 {
return f, nil
}
}
return nil, ErrNoBin
}
func (GHReleaseDl) UnGzBinary(r *os.File) (*gzip.Reader, error) {
gzR, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
defer gzR.Close()
return gzR, nil
}
func (dl GHReleaseDl) UnTargzBinary(r *os.File) (*tar.Reader, error) {
b := filepath.Base(dl.BinaryName)
gzR, err := gzip.NewReader(r)
if err != nil {
return nil, err
}
defer gzR.Close()
tarR := tar.NewReader(gzR)
for {
header, err := tarR.Next()
if err == io.EOF {
break
}
if err != nil {
return nil, err
}
if (header.Typeflag != tar.TypeDir) && filepath.Base(header.Name) == b {
if err != nil {
return nil, err
}
return tarR, nil
}
}
return nil, ErrNoBin
}