forked from git-hooks/git-hooks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
193 lines (165 loc) · 3.17 KB
/
util.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package main
import (
"archive/tar"
"bytes"
"compress/gzip"
"github.com/urfave/cli"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"reflect"
"strings"
)
func getGitRepoRoot() (string, error) {
return gitExec("rev-parse --show-toplevel")
}
func getGitDirPath() (string, error) {
return gitExec("rev-parse --git-dir")
}
func gitExec(args ...string) (string, error) {
wd, err := os.Getwd()
if err != nil {
return "", err
}
return gitExecWithDir(wd, args...)
}
func gitExecWithDir(dir string, args ...string) (string, error) {
args = strings.Split(strings.Join(args, " "), " ")
cmd := exec.Command("git", args...)
cmd.Dir = dir
if out, err := cmd.Output(); err == nil {
return string(bytes.Trim(out, "\n")), nil
} else {
return "", err
}
}
func bind(f interface{}, args ...interface{}) func(c *cli.Context) {
callable := reflect.ValueOf(f)
arguments := make([]reflect.Value, len(args))
for i, arg := range args {
arguments[i] = reflect.ValueOf(arg)
}
return func(c *cli.Context) {
callable.Call(arguments)
}
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// Download file from url.
// Downloaded file stored in temporary directory
func downloadFromUrl(url string) (fileName string, err error) {
file, err := ioutil.TempFile(os.TempDir(), NAME)
if err != nil {
return
}
defer file.Close()
fileName = file.Name()
response, err := http.Get(url)
if err != nil {
return
}
defer response.Body.Close()
_, err = io.Copy(file, response.Body)
if err != nil {
return
}
return
}
func extract(fileName string) (tmpFileName string, err error) {
file, err := ioutil.TempFile(os.TempDir(), NAME)
if err != nil {
return
}
defer file.Close()
tmpFileName = file.Name()
targz, err := os.Open(fileName)
if err != nil {
return
}
defer targz.Close()
gr, err := gzip.NewReader(targz)
if err != nil {
return
}
defer gr.Close()
tr := tar.NewReader(gr)
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
return tmpFileName, err
}
if hdr.Typeflag != tar.TypeDir {
_, err = io.Copy(file, tr)
if err != nil {
return tmpFileName, err
}
}
}
return
}
func installBinary(src string) (err error) {
dest, err := absExePath(os.Args[0])
if err != nil {
return
}
out, err := os.Create(dest)
if err != nil {
return
}
defer out.Close()
err = out.Chmod(0755)
if err != nil {
return
}
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
_, err = io.Copy(out, in)
if err != nil {
return
}
return
}
// return fullpath to executable file.
func absExePath(exe string) (name string, err error) {
name = exe
if name[0] == '.' {
name, err = filepath.Abs(name)
if err != nil {
name = filepath.Clean(name)
}
} else {
name, err = exec.LookPath(filepath.Clean(name))
}
if err != nil {
return
}
// follow symlink
fileinfo, err := os.Lstat(name)
if err != nil {
return
}
if fileinfo.Mode()&os.ModeSymlink != 0 {
name, err = os.Readlink(name)
}
return
}
func isExecutable(info os.FileInfo) bool {
return info.Mode()&0111 != 0
}