-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ref.go
120 lines (113 loc) · 2.89 KB
/
ref.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
package main
import (
"github.com/ScriptTiger/abc"
"os"
"strconv"
"strings"
"time"
)
//Function to display help text and exit
func help(err int) {
os.Stdout.WriteString(
"Usage: abc [options...] <url> <file>\n"+
" -agent <user-agent> Set User-Agent header\n"+
" -i <URL> Source URL\n"+
" -retry <number> Number of retries\n"+
" -nodebug Don't display debug messages, such as errors\n"+
" -nokeep If file already exists, delete and download new\n"+
" -noprogress Don't display progress\n"+
" -o <file> Destination file\n"+
" -range <range> Set Range header\n"+
" -timeout <duration> Set connection timeout\n")
os.Exit(err)
}
func main() {
//Declarations
var (
urlRaw *string
file *string
byteRange *string
agent *string
timeout *time.Duration
retry *uint
flags *uint8
err error
)
//Display help and exit if not enough arguments
if len(os.Args) < 2 {help(-1)}
//Push arguments to flag pointers
for i := 1; i < len(os.Args); i++ {
if strings.HasPrefix(os.Args[i], "-") {
switch strings.TrimPrefix(os.Args[i], "-") {
case "range":
i++
if byteRange == nil {byteRange = &os.Args[i]
} else {help(-2)}
continue
case "agent":
i++
if agent == nil {agent = &os.Args[i]
} else {help(-2)}
continue
case "i":
i++
if urlRaw == nil {urlRaw = &os.Args[i]
} else {help(-2)}
continue
case "timeout":
i++
if timeout == nil {
timeout = new(time.Duration)
*timeout, err = time.ParseDuration(os.Args[i])
if err != nil {
os.Stdout.WriteString(err.Error())
os.Exit(-2)
}
} else {help(-2)}
continue
case "nodebug":
if flags == nil {flags = new(uint8)}
if abc.NODEBUG&*flags == 0 {*flags |= abc.NODEBUG
} else {help(-2)}
continue
case "noprogress":
if flags == nil {flags = new(uint8)}
if abc.NOPROGRESS&*flags == 0 {*flags |= abc.NOPROGRESS
} else {help(-2)}
continue
case "nokeep":
if flags == nil {flags = new(uint8)}
if abc.NOKEEP&*flags == 0 {*flags |= abc.NOKEEP
} else {help(-2)}
continue
case "o":
i++
if file == nil {file = &os.Args[i]
} else {help(-2)}
continue
case "retry":
i++
if retry == nil {
retry = new(uint)
r, err := strconv.ParseUint(os.Args[i], 10, 32)
if err != nil {help(-2)}
*retry = uint(r)
} else {help(-2)}
continue
default:
help(-2)
}
} else if urlRaw == nil {urlRaw = &os.Args[i]
} else if file == nil {file = &os.Args[i]
} else {help(-2)}
}
totalSize, acceptRanges, err := abc.Download(urlRaw, file, byteRange, agent, timeout, retry, flags)
if err == nil {
if file == nil {
os.Stdout.WriteString(
"Content-Length = "+strconv.FormatInt(totalSize, 10)+
"\nAccept-Ranges = "+acceptRanges)
}
} else {os.Exit(1)}
os.Exit(0)
}