-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.go
129 lines (108 loc) · 2.57 KB
/
install.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
package vin
import (
"context"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"runtime"
"github.com/mholt/archiver/v3"
)
type ReadCloserWrapper interface {
Wrap(app App, reader io.ReadCloser, contentLength int64) io.ReadCloser
}
func (v *Vin) download(app App, url string, wrapper ReadCloserWrapper) (string, error) {
ctx := context.Background()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return "", err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("http response not OK: %d", resp.StatusCode)
}
tmpDir, err := ioutil.TempDir(v.TmpDir(), "")
if err != nil {
return "", err
}
defer func() {
if err != nil {
os.RemoveAll(tmpDir)
}
}()
archiveName := filepath.Base(url)
archivePath := filepath.Join(tmpDir, archiveName)
out, err := os.Create(archivePath)
if err != nil {
return "", err
}
defer out.Close()
reader := wrapper.Wrap(app, resp.Body, resp.ContentLength)
defer reader.Close()
if _, err := io.Copy(out, reader); err != nil {
return "", err
}
return archivePath, nil
}
func isExecutable(info os.FileInfo) bool {
if runtime.GOOS == "windows" {
return filepath.Ext(info.Name()) == ".exe"
}
return info.Mode()&0111 != 0
}
func (v *Vin) place(app App, src string) error {
if app.Name == "" {
app.Name = filepath.Base(src)
}
dst := filepath.Join(v.BinDir(), app.Name)
if err := os.Rename(src, dst); err != nil {
return err
}
return os.Chmod(dst, 0755)
}
// pickExecutable picks all executable files and moves them to the bin directory.
func (v *Vin) pickExecutable(app App, rootDir string) error {
return filepath.Walk(rootDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if isExecutable(info) {
if app.Name == "" {
app.Name = info.Name()
}
return v.place(app, path)
}
return nil
})
}
func (v *Vin) Install(app App, url string, wrapper ReadCloserWrapper) error {
archivePath, err := v.download(app, url, wrapper)
if err != nil {
return err
}
tmpDir := filepath.Dir(archivePath)
defer os.RemoveAll(tmpDir)
if name := filepath.Base(archivePath); !anyExtRegexp.MatchString(name) {
// the asset is a binary file
if app.Name == "" {
app.Name = name
}
return v.place(app, archivePath)
}
if err := archiver.Unarchive(archivePath, tmpDir); err != nil {
return err
}
if err := v.pickExecutable(app, tmpDir); err != nil {
return err
}
return nil
}