-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinstall.go
99 lines (86 loc) · 1.77 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
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
)
func install() {
idx := findPara(1)
if idx == -1 {
fmt.Println("need a go package path")
return
}
if os.Args[idx] == "install" {
idx = findPara(idx + 1)
}
if idx == -1 {
fmt.Println("need a go package path")
return
}
var ver, pkgPath = "master", os.Args[idx]
idx = findPara(idx + 1)
if idx != -1 {
ver = os.Args[idx]
}
ss := strings.Split(pkgPath, "/")
if len(ss) < 2 {
fmt.Println("not a go package path")
return
}
if !strings.Contains(ss[0], ".") {
pkgPath = "github.com/" + pkgPath
}
url := fmt.Sprintf(downTempl, pkgPath, ver, runtime.GOOS, runtime.GOARCH)
vPrintln("getting from", url)
resp, err := http.Get(url)
if err != nil {
fmt.Println(err)
return
}
if resp.StatusCode != 200 {
fmt.Println("error status code:", resp.StatusCode)
return
}
dir, bin := getPath(), ss[len(ss)-1]
binPath := filepath.Join(dir, bin+".zip")
vPrintln("writting to", binPath)
f, err := os.OpenFile(binPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0777)
if err != nil {
fmt.Println(err)
return
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
fmt.Println(err)
return
}
extractDir := filepath.Join(dir, "temp")
os.MkdirAll(extractDir, os.ModePerm)
vPrintln("unzip to", extractDir)
err = unzip(extractDir, binPath)
if err != nil {
fmt.Println(err)
return
}
dst := filepath.Join(dir, bin)
src := filepath.Join(extractDir, bin)
vPrintln("moving", src, "to", dst)
if runtime.GOOS == "windows" {
err = os.Rename(src+".exe", dst+".exe")
} else {
err = os.Rename(src, dst)
}
if err != nil {
fmt.Println(err)
return
}
vPrintln("clear", extractDir)
os.RemoveAll(extractDir)
vPrintln("clear", binPath)
os.Remove(binPath)
}