/
install.go
161 lines (139 loc) · 3.48 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
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
package cmds
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strconv"
"strings"
"github.com/cheggaaa/pb"
"github.com/codegangsta/cli"
"github.com/qiniu/log"
)
var InstallFlag = cli.Command{
Name: "install",
Aliases: []string{"i"},
Usage: fmt.Sprintf("Install binary from https://gobuild.io"),
Flags: []cli.Flag{
cli.BoolFlag{
Name: "debug",
Usage: "Enable debug log",
},
},
Action: InstallAction,
}
var GOBIN string
// Determine binary path
func init() {
var paths []string
gobin := os.Getenv("GOBIN")
if gobin != "" {
paths = filepath.SplitList(gobin)
}
if len(paths) > 0 {
GOBIN = paths[0]
return
}
paths = filepath.SplitList(os.Getenv("GOPATH"))
if len(paths) > 0 {
GOBIN = filepath.Join(paths[0], "bin")
return
}
log.Fatal("Make sure you set env GOPATH or GOBIN")
os.ExpandEnv("$HOME/.gopack/src")
}
func getInsPath(names ...string) string {
base := os.ExpandEnv("$HOME/.gopack/")
names = append([]string{base}, names...)
target := filepath.Join(names...)
os.MkdirAll(filepath.Dir(target), 0755)
return target
}
// http://dn-gobuild5.qbox.me/gorelease/gorelease/gorelease/master/darwin-amd64/gorelease.zip
// Need to update
func downloadSource(name string) (dest string, err error) {
parts := strings.Split(name, "/")
if len(parts) != 2 {
return "", fmt.Errorf("name: %s can only contains on /", name)
}
owner, repo := parts[0], parts[1]
osarch := runtime.GOOS + "-" + runtime.GOARCH
url := fmt.Sprintf("http://dn-gobuild5.qbox.me/gorelease/%s/%s/master/%s/%s.zip",
owner, repo, osarch, repo)
prompt("Downloading %v", url)
log.Debug("download:", url)
dest = getInsPath("src", fmt.Sprintf("%s.zip", repo))
resp, err := http.Get(url)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
err = errors.New("Http status not 200")
return
}
lengthStr := resp.Header.Get("Content-Length")
if lengthStr == "" {
err = errors.New("Content length is empty")
return
}
length, _ := strconv.Atoi(lengthStr)
bar := pb.New(length).SetUnits(pb.U_BYTES)
outFd, err := os.Create(dest)
if err != nil {
return
}
defer outFd.Close()
bar.Start()
reader := bar.NewProxyReader(resp.Body)
_, err = io.Copy(outFd, reader)
return
}
func deployPackage(pkgName, path string, binDir string) error {
cmd := exec.Command("unzip", "-o", "-d", getInsPath("opt", pkgName), path)
log.Debug("zip command:", cmd.Args)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
return err
}
baseName := filepath.Base(pkgName)
symlink := filepath.Join(GOBIN, baseName)
prompt("Symlink %v", symlink)
// for linux and darwin
os.Remove(symlink)
// TODO: need to resolve multi binaries
return os.Symlink(getInsPath("opt", pkgName, filepath.Base(pkgName)), symlink)
}
func prompt(format string, args ...interface{}) {
format = "==> " + strings.TrimPrefix(format, "\n") + "\n"
fmt.Printf(format, args...)
}
func InstallAction(c *cli.Context) {
if c.Bool("debug") {
log.SetOutputLevel(log.Ldebug)
}
if len(c.Args()) < 1 {
log.Fatal("Need at lease one argument")
}
// log.Println(GOBIN)
pkgName := c.Args().First()
// TODO: use myname for now
if len(strings.Split(pkgName, "/")) == 1 {
pkgName = "gobuild-official/" + pkgName
}
prompt("Repository %v", pkgName)
dest, err := downloadSource(pkgName)
if err != nil {
log.Fatal(err)
}
err = deployPackage(pkgName, dest, GOBIN)
if err != nil {
log.Fatal(err)
}
prompt("Program [%s] installed", pkgName)
}