-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgithub.go
189 lines (176 loc) · 3.94 KB
/
github.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
package pkgconfig
import (
"archive/zip"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
var githubProj, wd string
func init() {
var err error
if wd, err = os.Getwd(); err == nil {
githubProj = extractproj(wd, os.PathSeparator)
}
}
var src = map[rune]string{'/': "/src/", '\\': `\src\`}
var projpath = map[rune]func(string) string{
'/': func(s string) string { return s },
'\\': func(s string) string { return strings.Replace(s, "\\", "/", -1) },
}
func extractproj(path string, sep rune) (proj string) {
n := strings.LastIndex(path, src[sep])
if n == -1 {
return
}
n += len(src[sep])
m := n
for i := 0; i < 3; i++ {
l := strings.Index(path[m:], string(sep))
if l == -1 {
m = len(path) + 1
break
}
m += l + 1
}
proj = projpath[sep](string(path[n : m-1]))
if !strings.HasPrefix(proj, "github.com") {
proj = ""
}
return
}
// LookupGithub TODO(rjeczalik): document
func LookupGithub(pkg string) (*PC, error) {
if githubProj == "" {
return nil, errors.New(`unable to guess project's URL from $CWD`)
}
return LookupGithubProj(pkg, githubProj)
}
var targets = map[string]struct{}{
"darwin_386": {},
"darwin_amd64": {},
"freebsd_386": {},
"freebsd_amd64": {},
"freebsd_arm": {},
"linux_386": {},
"linux_amd64": {},
"linux_arm": {},
"netbsd_386": {},
"netbsd_amd64": {},
"netbsd_arm": {},
"openbsd_386": {},
"openbsd_amd64": {},
"plan9_386": {},
"plan9_amd64": {},
"windows_386": {},
"windows_amd64": {},
}
const targetsMinKeyLen = 9
func validFile(path, pkg string) bool {
switch {
case strings.HasPrefix(path, "include/"):
i := len("include/")
if len(path) < i+len(pkg)+1 {
return false
}
n := strings.Index(path[i:], "/")
if n == -1 {
return false
}
return string(path[i:i+n]) == pkg
case strings.HasPrefix(path, "lib/"):
i := len("lib/")
if len(path) < i+targetsMinKeyLen+1+len(pkg)+1 {
return false
}
n := strings.Index(path[i:], "/")
if n == -1 {
return false
}
if _, ok := targets[string(path[i:i+n])]; !ok {
return false
}
i += n + 1
if n = strings.Index(path[i:], "/"); n == -1 {
return false
}
return string(path[i:i+n]) == pkg
}
return false
}
var replacefile = map[rune]func(string) string{
'/': func(s string) string { return s },
'\\': func(s string) string { return strings.Replace(s, "/", "\\", -1) },
}
func copyFile(gopath string, f *zip.File) (err error) {
rc, err := f.Open()
if err != nil {
return
}
defer rc.Close()
dir := filepath.Join(gopath, replacefile[os.PathSeparator](path.Dir(f.Name)))
if err = os.MkdirAll(dir, 0755); err != nil {
return
}
file, err := os.OpenFile(filepath.Join(dir, path.Base(f.Name)), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
if err != nil {
return
}
_, err = io.Copy(file, rc)
file.Close()
return
}
// LookupGithunProj TODO(rjeczalik): document
func LookupGithubProj(pkg, proj string) (*PC, error) {
path := os.Getenv("GOPATH")
if p := strings.Split(path, string(os.PathListSeparator)); len(path) != 0 {
path = p[0]
}
if path == "" {
return nil, errors.New("$GOPATH is empty")
}
url := fmt.Sprintf("http://%s/releases/download/pkg-config/%s.zip", proj, pkg)
res, err := http.Get(url)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode == http.StatusNotFound {
return nil, errors.New("not found: " + url)
}
f, err := ioutil.TempFile("", pkg)
if err != nil {
return nil, err
}
if _, err = io.Copy(f, res.Body); err != nil {
f.Close()
os.Remove(f.Name())
return nil, err
}
f.Close()
r, err := zip.OpenReader(f.Name())
if err != nil {
return nil, err
}
defer func() {
r.Close()
os.Remove(f.Name())
}()
for _, f := range r.File {
// Filter out directories.
if f.Name[len(f.Name)-1] != '/' {
if !validFile(f.Name, pkg) {
return nil, fmt.Errorf("unexcpected file %q", f.Name)
}
if err = copyFile(path, f); err != nil {
return nil, err
}
}
}
return LookupGopath(pkg)
}