-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
241 lines (176 loc) · 4.9 KB
/
common.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"os"
"path"
"container/list"
"encoding/json"
"net/http"
"strings"
"errors"
"io"
"compress/gzip"
"fmt"
"archive/tar"
"io/ioutil"
)
const DIST_URL string = "https://nodejs.org/dist"
const DATA_DIR string = ".node-version"
const VERSIONS = "versions"
const BIN string = "bin"
const PERM uint32 = 0744
const DEFAULT string = "default"
func GetDataPath() string {
return path.Join(os.Getenv("HOME"), DATA_DIR)
}
func GetVersionsPath() string {
return path.Join(GetDataPath(), VERSIONS)
}
func EnsureDirExists(p string, perm uint32) error {
info, err := os.Stat(p)
fileMode := os.ModeDir | os.FileMode(perm)
if err != nil {
if os.IsNotExist(err) {
fmt.Println("creating directory " + p)
return os.MkdirAll(p, fileMode)
} else {
return err
}
} else if !info.IsDir() {
return errors.New(p + " is not a directory")
} else if info.Mode() != fileMode {
return errors.New(p + " has wrong permissions")
}
return nil
}
func GetTarballName(versionName string) string {
return "node-" + versionName + "-darwin-x64.tar.gz"
}
func FilterList(source *list.List, filter func (e *list.Element) bool) *list.List {
result := list.New()
for e := source.Front(); e != nil; e = e.Next() {
if filter(e) {
result.PushBack(e.Value)
}
}
return result
}
func FilterStringSlice(source *[]string, filter func(element *string) bool) *[]string {
var result []string
for _, v := range *source {
if filter(&v) {
result = append(result, v)
}
}
return &result
}
func GetRemoteVersions() (*list.List, error) {
url := strings.Join([]string{DIST_URL, "index.json" }, "/")
fmt.Println("loading versions... ")
fmt.Println(url)
resp, err := http.Get(url)
if err != nil { return nil, err }
if resp.Body == nil {
return nil, errors.New("some network problem had occured")
}else {
defer resp.Body.Close()
}
decoder := json.NewDecoder(resp.Body)
index := VersionIndex{}
err = decoder.Decode(&index)
if err != nil { return nil, err }
result := list.New()
for _, e := range index {
result.PushBack(e)
}
fmt.Println("done")
return result, nil
}
func GetLocalVersions() (*[]string, error) {
infoList, err := ioutil.ReadDir(GetVersionsPath())
if os.IsNotExist(err) {
result := make([]string, 0)
return &result, nil
}
if err != nil {
return nil, err
}
result := make([]string, len(infoList))
for index, info := range infoList {
result[index] = info.Name()
}
return &result, nil
}
func DownloadAndSave(url, location, name string) error {
fmt.Println("loading installation archive... ")
fmt.Println(url)
resp, err := http.Get(url)
if err != nil { return err }
if resp.Body == nil { return errors.New("some network problem has occured") }
defer resp.Body.Close()
if resp.StatusCode >= 400 {
return errors.New("unexpected response status code " + resp.Status)
}
err = EnsureDirExists(location, PERM)
if err != nil { return err }
filePath := path.Join(location, name)
file, err := os.OpenFile(filePath, os.O_CREATE | os.O_TRUNC | os.O_WRONLY, os.FileMode(PERM))
if err != nil { return err }
defer file.Close()
_, err = io.Copy(file, resp.Body)
if err != nil { return err }
fmt.Println("done")
return nil
}
func UnGZip(location, fileName string) error {
filePath := path.Join(location, fileName)
fmt.Println("uncompressing file...")
fmt.Println(filePath)
_, err := os.Stat(filePath)
if err != nil { return err }
sourceFile, err := os.Open(filePath)
if err != nil { return err }
defer sourceFile.Close()
gzipReader, err := gzip.NewReader(sourceFile)
if err != nil { return err }
defer gzipReader.Close()
if !strings.HasSuffix(filePath, ".gz") {
return errors.New("Unsupported tarball file extension")
}
destFilePath := filePath[0:len(filePath) - 3]
destFile, err := os.OpenFile(destFilePath, os.O_CREATE | os.O_TRUNC | os.O_WRONLY, os.FileMode(PERM))
if err != nil { return err }
defer destFile.Close()
_, err = io.Copy(destFile, gzipReader)
if err != nil { return err }
fmt.Println("done")
return nil
}
func UnTar(location, fileName, destLocation string) error {
sourcePath := path.Join(location, fileName)
fmt.Println("opening file...")
fmt.Println(sourcePath)
_, err := os.Stat(sourcePath)
if err != nil { return err }
sourceFile, err := os.Open(sourcePath)
if err != nil { return err }
defer sourceFile.Close()
tarReadder := tar.NewReader(sourceFile)
for {
hdr, err := tarReadder.Next()
if err == io.EOF { break }
if err != nil { return err }
destPath := path.Join(destLocation, hdr.Name)
if strings.HasSuffix(hdr.Name, "/") && hdr.Size == 0 {
err = os.MkdirAll(destPath, os.ModeDir | hdr.FileInfo().Mode())
if err != nil { return err }
}else {
file, err := os.OpenFile(destPath, os.O_CREATE | os.O_WRONLY, hdr.FileInfo().Mode())
if err != nil { return err }
_, err = io.Copy(file, tarReadder)
if err != nil { return err }
err = file.Close()
if err != nil { return err }
}
}
return nil
}