-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (53 loc) · 1.37 KB
/
main.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
package main
import (
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strings"
"github.com/c4milo/unzipit"
)
const RWX_FILE = 0755
const FULL_FILE = 0777
var EXTENSIONS = []string{"zip", "tar.gz"}
func main() {
opts := getOpts()
if err := os.MkdirAll(opts.Out, FULL_FILE); err != nil {
log.Printf("Unable to create directory %s: %v\n", opts.Out, err)
os.Exit(1)
}
// TODO Spinner:
// - https://github.com/briandowns/spinner
// - https://github.com/tj/go-spin
// - https://github.com/tj/stack/blob/master/pkg/logger
log.Printf("downloading archive at %s", opts.URL)
// TODO Bring back checksum check
//stream, err := Download(opts.URL, opts.Checksum)
res, err := http.Get(opts.URL)
if err != nil {
log.Printf("error: %v\n", err)
os.Exit(1)
}
// TODO can force extention from the cli
for _, ext := range EXTENSIONS {
if strings.HasSuffix(opts.URL, ext) {
_, err := unzipit.UnpackStream(res.Body, opts.Out)
if err != nil {
log.Printf("error: %v\n", err)
os.Exit(1)
}
log.Printf("wrote file to %s\n", opts.Out)
return
}
}
// else
buf, _ := ioutil.ReadAll(res.Body)
partials := strings.Split(opts.URL, "/")
filename := partials[len(partials)-1]
if err := ioutil.WriteFile(path.Join(opts.Out, filename), buf, RWX_FILE); err != nil {
log.Printf("error: %v\n", err)
os.Exit(1)
}
log.Printf("wrote file to %s\n", opts.Out)
}