-
Notifications
You must be signed in to change notification settings - Fork 6
/
bincache.go
213 lines (175 loc) · 5.18 KB
/
bincache.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package onetimeserver
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
)
const baseURL = "https://github.com/osheroff/onetimeserver-binaries/raw/master"
func getBinaryCachePath(pkg string, subpath string, program string, version string) string {
dir := fmt.Sprintf("%s/.onetimeserver/bin/%s/%s%s", os.Getenv("HOME"), pkg, version, subpath)
err := os.MkdirAll(dir, 0755)
if err != nil {
log.Fatal(err)
}
return fmt.Sprint(dir, "/", program)
}
func makeHTTPRequestURL(url string) *http.Response {
log.Printf("fetching %s\n", url)
resp, err := http.Get(url)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode == 404 {
return nil
} else if resp.StatusCode != 200 {
log.Fatal(fmt.Sprintf("Got status %d fetching %s", resp.StatusCode, url))
return nil
} else {
return resp
}
}
func makeHTTPRequest(pkg string, subpath string, os string, program string, version string) *http.Response {
url := fmt.Sprintf("%s/%s/%s/%s/%s?raw=true", baseURL, pkg, os, version, program)
return makeHTTPRequestURL(url)
}
func buildInstallCachePath(pkg string, version string) string {
return fmt.Sprintf("%s/.onetimeserver/install/%s/%s", os.Getenv("HOME"), pkg, version)
}
func GetInstallPathCache(pkg string, version string) string {
dir := fmt.Sprintf("%s/.onetimeserver/install/%s/%s", os.Getenv("HOME"), pkg, version)
err := os.MkdirAll(dir, 0755)
if err != nil {
log.Fatal(err)
}
return dir
}
func CopyFromInstallCache(pkg string, version string, destPath string) bool {
dir := buildInstallCachePath(pkg, version)
dirFiles := fmt.Sprintf("%s/.", dir)
stat, err := os.Stat(dir)
if err == nil && stat.IsDir() {
os.MkdirAll(destPath, 0755)
cmd := exec.Command("cp", "-avp", dirFiles, destPath)
cmd.Run()
return true
} else {
return false
}
}
func CopyToInstallCache(pkg string, version string, sourcePath string) {
dir := buildInstallCachePath(pkg, version)
err := os.MkdirAll(dir, 0755)
if err != nil {
log.Fatal(err)
}
sourceFiles := fmt.Sprintf("%s/.", sourcePath)
cmd := exec.Command("cp", "-avp", sourceFiles, dir)
cmd.Run()
}
func RemoveFromInstallCache(pkg string, version string, sourcePath string, filename string) {
dir := buildInstallCachePath(pkg, version)
file := fmt.Sprintf("%s/%s", dir, filename)
os.Remove(file)
}
func GetBinary(pkg string, subpath string, program string, version string) string {
path := getBinaryCachePath(pkg, subpath, program, version)
_, err := os.Stat(path)
if err == nil {
return path
}
resp := makeHTTPRequest(pkg, subpath, runtime.GOOS, program, version)
if resp == nil {
resp = makeHTTPRequest(pkg, subpath, runtime.GOOS, program, "common")
}
if resp == nil {
resp = makeHTTPRequest(pkg, subpath, "common", program, version)
}
if resp == nil {
log.Fatal(fmt.Sprintf("Couldn't find %s/%s %s for platform", pkg, program, version))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Fatal(err)
}
defer file.Close()
file.Write(body)
return path
}
func GetManifest(pkg string) string {
manifestPath := fmt.Sprintf("%s/.onetimeserver/bin/%s/manifest.json", os.Getenv("HOME"), pkg)
_, err := os.Stat(manifestPath)
if err == nil {
return manifestPath
}
url := fmt.Sprintf("%s/%s/manifest.json?raw=true", baseURL, pkg)
resp := makeHTTPRequestURL(url)
if resp == nil {
log.Fatal(fmt.Sprintf("Couldn't find manifest at %s", url))
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
file, err := os.OpenFile(manifestPath, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
log.Fatal(err)
}
defer file.Close()
file.Write(body)
return manifestPath
}
func downloadRange(pkg string, version string, files []interface{}) {
for _, item := range files {
file := item.(string)
split := strings.Split(file, "/")
GetBinary(pkg, strings.Join(split[0:len(split)-1], "/"), split[len(split)-1], version)
}
}
func DownloadFromManifest(pkg string, version string) {
path := GetManifest(pkg)
var result map[string]interface{}
jsonFile, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
defer jsonFile.Close()
bytes, _ := ioutil.ReadAll(jsonFile)
json.Unmarshal(bytes, &result)
all, ok := result["all"]
if !ok {
log.Fatal("No key for 'all' in manifest.json!")
}
downloadRange(pkg, version, all.([]interface{}))
ver, version_ok := result[version]
if !version_ok {
log.Fatal(fmt.Sprintf("No key for '%s' in manifest.json!", version))
}
version_map := ver.(map[string]interface{})
common, ok := version_map["common"]
if !ok {
log.Fatal(fmt.Sprintf("No key for '%s/common' in manifest.json!", version))
}
downloadRange(pkg, version, common.([]interface{}))
platform, platform_ok := version_map[runtime.GOOS]
if platform_ok {
downloadRange(pkg, version, platform.([]interface{}))
}
}
func MakeSymlink(pkg string, subpath string, program string, version string, alias string) {
linkFrom := getBinaryCachePath(pkg, subpath, program, version)
linkTo := getBinaryCachePath(pkg, subpath, alias, version)
fmt.Printf("symlinking from %s to %s\n", linkFrom, linkTo)
os.Symlink(linkFrom, linkTo)
}