-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
164 lines (140 loc) · 3.3 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
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
package main
import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"time"
"github.com/ananthb/chonker"
"github.com/cavaliergopher/grab/v3"
"github.com/dustin/go-humanize"
"golang.org/x/term"
)
var (
chunkSize string
continueNoRange bool
metricsFile string
outputFile string
quiet bool
workers uint
printVersion bool
version = "devel"
)
func init() {
flag.StringVar(&chunkSize, "c", "1MiB", "chunk size (e.g. 1MiB, 1GiB)")
flag.BoolVar(&continueNoRange, "continue", false, "continue download without range support")
flag.StringVar(&metricsFile, "m", "", "write prometheus metrics to file (default: disabled)")
flag.StringVar(&outputFile, "o", "", "output file or directory (default: current directory)")
flag.BoolVar(&quiet, "q", false, "quiet")
flag.UintVar(&workers, "w", 4, "number of workers")
flag.BoolVar(&printVersion, "v", false, "print version and exit")
}
func main() {
flag.Parse()
if printVersion {
fmt.Printf("chonker (%s)\n", version)
return
}
csize, err := humanize.ParseBytes(chunkSize)
if err != nil {
exit(err)
}
if flag.NArg() != 1 {
exit(fmt.Errorf("missing URL"))
}
url := flag.Arg(0)
// Write prometheus metrics periodically
var metricsTicker *time.Ticker
if metricsFile != "" {
metricsTicker = time.NewTicker(5 * time.Second)
defer metricsTicker.Stop()
go func() {
for range metricsTicker.C {
writeMetricsFile(metricsFile)
}
}()
}
cc, err := chonker.NewClient(nil, csize, workers)
if err != nil {
exit(err)
}
gc := grab.NewClient()
gc.HTTPClient = cc
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
req, err := grab.NewRequest(outputFile, url)
if err != nil {
exit(err)
}
req.IgnoreRemoteTime = true
req = req.WithContext(ctx)
resp := gc.Do(req)
isTerm := term.IsTerminal(int(os.Stdout.Fd()))
if !quiet {
fmt.Printf("Downloading %s\n", resp.Request.URL())
if resp.DidResume {
fmt.Printf("Resuming download from (%.2f%%)\n", 100*resp.Progress())
}
}
var updateTicker *time.Ticker
if !quiet {
updateTicker = time.NewTicker(1 * time.Second)
defer updateTicker.Stop()
go func() {
for range updateTicker.C {
if isTerm {
// Clear the current line
fmt.Print("\033[2K\r")
}
fmt.Printf(
"Transferred %s/%s (%.2f%%) in %s at %s/s. ETA %s.",
humanize.IBytes(uint64(resp.BytesComplete())),
humanize.IBytes(uint64(resp.Size())),
100*resp.Progress(),
resp.Duration().Round(time.Second),
humanize.IBytes(uint64(resp.BytesPerSecond())),
humanize.Time(resp.ETA()),
)
}
}()
}
<-resp.Done
if updateTicker != nil {
updateTicker.Stop()
}
if metricsTicker != nil {
metricsTicker.Stop()
writeMetricsFile(metricsFile)
}
if err := resp.Err(); err != nil {
exit(err)
}
if !quiet {
if isTerm {
fmt.Print("\033[2K\r")
}
fmt.Printf(
"Downloaded %s in %s at %s/s.\n",
humanize.IBytes(uint64(resp.BytesComplete())),
resp.Duration().Round(time.Second),
humanize.IBytes(uint64(resp.BytesPerSecond())),
)
}
}
func exit(msg any) {
if !quiet {
fmt.Fprintln(os.Stderr, msg)
}
os.Exit(1)
}
func writeMetricsFile(filename string) {
f, err := os.Create(filename)
if err != nil {
panic(err)
}
chonker.StatsForNerds.WritePrometheus(f)
if err := f.Close(); err != nil {
panic(err)
}
}