/
main.go
128 lines (99 loc) · 2.22 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
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
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"net/url"
"os"
"path"
"time"
"github.com/cavaliercoder/grab"
)
// test app to evaluate the grab http client for downloading OS updates
// we've had trouble downloading over Cat-M connections where the download
// will stall, so the this app is used to debug these downloads.
func httpDownload(url, fn string) {
file, err := os.Create(fn)
if err != nil {
log.Println("App update: error creating file: ", err)
return
}
defer file.Close()
var netClient = &http.Client{
Timeout: 30 * time.Minute,
}
// Get the data
resp, err := netClient.Get(url)
if err != nil {
log.Println("App update: error getting update file: ", err)
return
}
defer resp.Body.Close()
// copy to file
_, err = io.Copy(file, resp.Body)
if err != nil {
log.Println("App update: error downloading update file: ", err)
return
}
log.Println("Download finished")
}
func grabDownload(url, fn string) {
file, err := os.Create(fn)
if err != nil {
log.Println("App update: error creating file: ", err)
return
}
defer file.Close()
client := grab.NewClient()
req, err := grab.NewRequest(fn, url)
// ...
resp := client.Do(req)
t := time.NewTicker(time.Second)
defer t.Stop()
for {
select {
case <-t.C:
fmt.Printf("%.02f%% complete, %.02f B/sec\n",
resp.Progress()*100,
resp.BytesPerSecond())
case <-resp.Done:
if err := resp.Err(); err != nil {
log.Println("Error downloading: ", err)
}
return
}
}
}
func usage() {
fmt.Println("Usage: ")
flag.PrintDefaults()
os.Exit(-1)
}
func main() {
flagGohttp := flag.Bool("gohttp", false, "use Go http client")
flagGrab := flag.Bool("grab", false, "use grab http client")
flagURL := flag.String("url", "", "URL of file to download")
flag.Parse()
if *flagURL == "" {
log.Println("Must set URL")
}
if !*flagGohttp && !*flagGrab {
log.Println("Must set -gohttp or -grab option", *flagGohttp, *flagGrab)
usage()
}
url, err := url.Parse(*flagURL)
if err != nil {
log.Println("Error parsing url: ", err)
os.Exit(-1)
}
urlPath := url.EscapedPath()
_, fn := path.Split(urlPath)
if *flagGohttp {
httpDownload(url.String(), fn)
}
if *flagGrab {
grabDownload(url.String(), fn)
}
}