-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdep.go
234 lines (204 loc) · 5.57 KB
/
dep.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
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/mholt/archiver"
"github.com/spf13/viper"
"gopkg.in/alecthomas/kingpin.v2"
)
var (
depfile = kingpin.Flag("depfile", "The name of the dependency file. Defaults to '.dep'").Default(".dep").ExistingFile()
registry = kingpin.Flag("registry", "The location of the registry, where to find the dependency.").URL()
)
func main() {
kingpin.Version("0.0.1")
kingpin.Parse()
viper.SetDefault("SevenZipExecutable", "C:/Program Files/7-Zip/7z.exe")
viper.SetDefault("DependencyDirectory", "dep")
viper.SetDefault("Repositories", []string{})
viper.SetConfigName("config") // name of config file (without extension)
viper.AddConfigPath("$HOME/.deprc") // call multiple times to add many search paths
viper.AddConfigPath(".") // optionally look for config in the working directory
viper.AutomaticEnv()
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
panic(fmt.Errorf("Fatal error config file: %s", err))
}
// now gather all required dependencies
dependencies := make(map[string]string)
f, e := os.OpenFile(*depfile, os.O_RDWR, 0755)
defer f.Close()
r := bufio.NewReader(f)
s, e := Readln(r)
for e == nil {
if !strings.HasPrefix(s, "#") {
kv := strings.Split(s, " ")
if len(kv) == 2 {
dependencies[kv[0]] = kv[1]
}
}
s, e = Readln(r)
}
err = os.RemoveAll(viper.GetString("DependencyDirectory"))
if err != nil {
panic(err)
}
err = os.Mkdir(viper.GetString("DependencyDirectory"), 0755)
if err != nil {
panic(err)
}
// try to download the dependencies
for dep, ver := range dependencies {
e = nil
for _, source := range viper.GetStringSlice("Repositories") {
if _, e = Download(dep, ver, source, ".zip"); e == nil {
fmt.Print("Resolved " + dep + " " + ver + "\n")
break
} else if _, e = Download(dep, ver, source, ".7z"); e == nil {
fmt.Print("Resolved " + dep + " " + ver + "\n")
break
} else if _, e = CopyFromDisk(dep, ver, source); e == nil {
fmt.Print("Resolved " + dep + " " + ver + "\n")
break
}
}
if e != nil {
fmt.Printf("Error while trying to download %s %s: %s\n", dep, ver, e)
}
}
}
// CopyFromDisk tries to copy the dependency dep in version ver from folder dir
func CopyFromDisk(dep string, ver string, dir string) (string, error) {
path := dir + "/" + dep + "-" + ver + ".*"
files, _ := filepath.Glob(path)
if len(files) == 0 {
return path, fmt.Errorf("CopyFromDisk: dependency could not be found")
}
path = files[0]
if strings.HasSuffix(path, ".zip") || strings.HasSuffix(path, ".7z") {
file, err := ioutil.TempFile(".", "temp_")
if err != nil {
return path, err
}
defer os.Remove(file.Name())
defer file.Close()
err = CopyFile(path, file)
if err != nil {
return path, err
}
dest := viper.GetString("DependencyDirectory") + "/" + dep
if strings.HasSuffix(path, ".zip") {
err = archiver.Zip.Open(file.Name(), dest)
if err != nil {
return path, err
}
} else {
file.Close()
cmd := exec.Command("7z", "e", file.Name(), "-o"+dest, "-r", "-t7z", "-aoa")
_, err := cmd.CombinedOutput()
if err != nil {
return path, err
}
}
} else {
file, err := os.Create(viper.GetString("DependencyDirectory") + "/" + dep + filepath.Ext(path))
if err != nil {
return path, err
}
defer file.Close()
err = CopyFile(path, file)
if err != nil {
return path, err
}
}
return path, nil
}
// CopyFile copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func CopyFile(src string, out *os.File) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}
// Download tries to download the dependency dep in version ver from URL url.
func Download(dep string, ver string, url string, ext string) (string, error) {
url = url + "/" + dep + "-" + ver + ext
// Create the file
// out, err := os.Create("temp" + ext)
file, err := ioutil.TempFile(".", "temp_")
if err != nil {
return url, err
}
defer os.Remove(file.Name())
defer file.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return url, err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return url, fmt.Errorf("Download: The file could not be downloaded")
}
// Write the body to file
_, err = io.Copy(file, resp.Body)
if err != nil {
return url, err
}
// Unarchive
dir := viper.GetString("DependencyDirectory") + "/" + dep
// err = os.Remove(dir)
// if err != nil {
// return url, err
// }
err = os.MkdirAll(dir, 0755)
if err != nil {
return url, err
}
if ext == ".zip" {
err = archiver.Zip.Open(file.Name(), dir)
if err != nil {
return url, err
}
} else if ext == ".7z" {
file.Close()
cmd := exec.Command("7z", "e", file.Name(), "-o"+dir, "-r", "-t7z", "-aoa")
_, err := cmd.CombinedOutput()
if err != nil {
return url, err
}
}
return url, nil
}
// Readln returns a single line (without the ending \n)
// from the input buffered reader.
// An error is returned iff there is an error with the
// buffered reader.
func Readln(r *bufio.Reader) (string, error) {
var (
isPrefix = true
err error
line, ln []byte
)
for isPrefix && err == nil {
line, isPrefix, err = r.ReadLine()
ln = append(ln, line...)
}
return string(ln), err
}