-
Notifications
You must be signed in to change notification settings - Fork 67
/
deps.go
54 lines (45 loc) · 1.26 KB
/
deps.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
package core
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"regexp"
"strings"
)
func externalHttpSourceToPath(lib string, url string) (path string) {
home := HomeDir()
localBase := filepath.Join(home, ".jokerd", "deps", strings.SplitN(url, "//", 2)[1])
libBase := filepath.Join(strings.Split(lib, ".")...) + ".joke"
libPath := filepath.Join(localBase, libBase)
libPathDir := filepath.Dir(libPath)
if _, err := os.Stat(libPathDir); os.IsNotExist(err) {
os.MkdirAll(libPathDir, 0777)
}
if _, err := os.Stat(libPath); os.IsNotExist(err) {
if !strings.HasSuffix(url, ".joke") {
url = url + libBase
}
resp, err := http.Get(url)
PanicOnErr(err)
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
panic(RT.NewError(fmt.Sprintf("Unable to retrieve: %s\nServer response: %d", url, resp.StatusCode)))
}
out, err := os.Create(libPath)
defer out.Close()
PanicOnErr(err)
_, err = io.Copy(out, resp.Body)
PanicOnErr(err)
}
return libPath
}
func externalSourceToPath(lib string, url string) (path string) {
httpPath, _ := regexp.MatchString("http://|https://", url)
if httpPath {
return externalHttpSourceToPath(lib, url)
} else {
return filepath.Join(append([]string{url}, strings.Split(lib, ".")...)...) + ".joke"
}
}