/
pget.go
206 lines (169 loc) · 4.28 KB
/
pget.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package pget
import (
"bufio"
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/Songmu/prompter"
"github.com/asaskevich/govalidator"
"github.com/pkg/errors"
)
// Pget structs
type Pget struct {
Trace bool
Output string
Procs int
URLs []string
args []string
timeout int
useragent string
referer string
}
// New for pget package
func New() *Pget {
return &Pget{
Trace: false,
Procs: runtime.NumCPU(), // default
timeout: 10,
}
}
// Run execute methods in pget package
func (pget *Pget) Run(ctx context.Context, version string, args []string) error {
if err := pget.Ready(version, args); err != nil {
return errTop(err)
}
// TODO(codehex): calc maxIdleConnsPerHost
client := newDownloadClient(16)
target, err := Check(ctx, &CheckConfig{
URLs: pget.URLs,
Timeout: time.Duration(pget.timeout) * time.Second,
Client: client,
})
if err != nil {
return err
}
filename := target.Filename
var dir string
if pget.Output != "" {
fi, err := os.Stat(pget.Output)
if err == nil && fi.IsDir() {
dir = pget.Output
} else {
dir, filename = filepath.Split(pget.Output)
if dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
return errors.Wrapf(err, "failed to create diretory at %s", dir)
}
}
}
}
opts := []DownloadOption{
WithUserAgent(pget.useragent, version),
WithReferer(pget.referer),
}
return Download(ctx, &DownloadConfig{
Filename: filename,
Dirname: dir,
ContentLength: target.ContentLength,
Procs: pget.Procs,
URLs: target.URLs,
Client: client,
}, opts...)
}
const (
warningNumConnection = 4
warningMessage = "[WARNING] Using a large number of connections to 1 URL can lead to DOS attacks.\n" +
"In most cases, `4` or less is enough. In addition, the case is increasing that if you use multiple connections to 1 URL does not increase the download speed with the spread of CDNs.\n" +
"See: https://github.com/Code-Hex/pget#disclaimer\n" +
"\n" +
"Would you execute knowing these?\n"
)
// Ready method define the variables required to Download.
func (pget *Pget) Ready(version string, args []string) error {
opts, err := pget.parseOptions(args, version)
if err != nil {
return errors.Wrap(errTop(err), "failed to parse command line args")
}
if opts.Trace {
pget.Trace = opts.Trace
}
if opts.Timeout > 0 {
pget.timeout = opts.Timeout
}
if err := pget.parseURLs(); err != nil {
return errors.Wrap(err, "failed to parse of url")
}
if opts.NumConnection > warningNumConnection && !prompter.YN(warningMessage, false) {
return makeIgnoreErr()
}
pget.Procs = opts.NumConnection * len(pget.URLs)
if opts.Output != "" {
pget.Output = opts.Output
}
if opts.UserAgent != "" {
pget.useragent = opts.UserAgent
}
if opts.Referer != "" {
pget.referer = opts.Referer
}
return nil
}
func (pget *Pget) parseOptions(argv []string, version string) (*Options, error) {
var opts Options
if len(argv) == 0 {
stdout.Write(opts.usage(version))
return nil, makeIgnoreErr()
}
o, err := opts.parse(argv, version)
if err != nil {
return nil, errors.Wrap(err, "failed to parse command line options")
}
if opts.Help {
stdout.Write(opts.usage(version))
return nil, makeIgnoreErr()
}
if opts.Update {
result, err := opts.isupdate(version)
if err != nil {
return nil, errors.Wrap(err, "failed to parse command line options")
}
stdout.Write(result)
return nil, makeIgnoreErr()
}
pget.args = o
return &opts, nil
}
func (pget *Pget) parseURLs() error {
// find url in args
for _, argv := range pget.args {
if govalidator.IsURL(argv) {
pget.URLs = append(pget.URLs, argv)
}
}
if len(pget.URLs) < 1 {
fmt.Fprintf(stdout, "Please input url separate with space or newline\n")
fmt.Fprintf(stdout, "Start download with ^D\n")
// scanning url from stdin
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
scan := scanner.Text()
urls := strings.Split(scan, " ")
for _, url := range urls {
if govalidator.IsURL(url) {
pget.URLs = append(pget.URLs, url)
}
}
}
if err := scanner.Err(); err != nil {
return errors.Wrap(err, "failed to parse url from stdin")
}
if len(pget.URLs) < 1 {
return errors.New("urls not found in the arguments passed")
}
}
return nil
}