-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
56 lines (47 loc) · 816 Bytes
/
net.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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
func tempFile() *os.File {
w, err := os.Getwd()
if err != nil {
panic(err)
}
f, err := ioutil.TempFile(w, "atlas")
if err != nil {
panic(err)
}
return f
}
func getFile(url string, opt Opt) string {
f := tempFile()
defer f.Close()
res, err := http.Get(url)
if err != nil {
f.Close()
exit(err)
}
defer res.Body.Close()
if opt.header {
for key, values := range res.Header {
fmt.Printf("%s: ", key)
for _, value := range values {
fmt.Printf("%s ", value)
}
fmt.Println()
}
}
if res.StatusCode == 404 {
warn("request returned 404")
}
p := Progress{total: int(res.ContentLength)}
if _, err := io.Copy(f, io.TeeReader(res.Body, &p)); err != nil {
panic(err)
}
fmt.Println()
return f.Name()
}