-
Notifications
You must be signed in to change notification settings - Fork 1
/
common.go
87 lines (75 loc) · 1.36 KB
/
common.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
package main
import (
"archive/zip"
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/user"
)
const (
PlatformCommon = "common"
)
var (
ErrorPageNotExists = errors.New("WEB: Page not exists")
)
func isFileExists(path string) (isExist bool) {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
func httpGet(urlString string) (result []byte, err error) {
u, err := url.Parse(urlString)
if err != nil {
return
}
resp, err := http.Get(u.String())
if err != nil {
return
}
// Close the body
defer func() { _ = resp.Body.Close() }()
// If response not OK, it means page not exists
if resp.StatusCode != http.StatusOK {
err = ErrorPageNotExists
return
}
// Read data from body
result, err = ioutil.ReadAll(resp.Body)
if err != nil {
return
}
return
}
func downloadZip(url string) (zipReader *zip.Reader, err error) {
// Download the ZIP file
zipFile, err := httpGet(url)
if err != nil {
return
}
// Turn this array into a zip reader
zipReader, err = zip.NewReader(
bytes.NewReader(zipFile),
int64(len(zipFile)),
)
if err != nil {
return
}
return
}
func getHomeDir() (homeDir string, err error) {
usr, err := user.Current()
if err != nil {
return
}
homeDir = usr.HomeDir
return
}
func printProgress(current, total int) {
fmt.Printf("\r%d / %d", current, total)
if current == total {
fmt.Println()
}
}