Skip to content

Commit 6eed7d1

Browse files
committed
Release v0.0.2
Added timeout option for checking method Improved usage messages Stable now
1 parent 25eb37e commit 6eed7d1

File tree

3 files changed

+33
-37
lines changed

3 files changed

+33
-37
lines changed

option.go

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"fmt"
66
"os"
7-
"reflect"
87

98
"github.com/jessevdk/go-flags"
109
"github.com/pkg/errors"
@@ -16,6 +15,7 @@ type Options struct {
1615
Version bool `short:"v" long:"version" description:"display the version of pget and exit"`
1716
Procs int `short:"p" long:"procs" description:"split ratio to download file"`
1817
Output string `short:"o" long:"output" description:"output file to FILENAME"`
18+
Timeout int `long:"timeout" description:"timeout of checking request in seconds"`
1919
Trace bool `long:"trace" description:"display detail error messages"`
2020
// File string `long:"file" description:"urls has same hash in a file to download"`
2121
}
@@ -35,26 +35,17 @@ func (opts *Options) parse(argv []string) ([]string, error) {
3535
func (opts Options) usage() []byte {
3636
buf := bytes.Buffer{}
3737

38-
fmt.Fprintf(&buf, "Pget "+version+", a parallel file download client\n"+
38+
fmt.Fprintf(&buf, msg+
3939
`Usage: pget [options] URL
4040
4141
Options:
42+
-h, --help print usage and exit
43+
-v, --version display the version of pget and exit
44+
-p, --procs <num> split ratio to download file
45+
-o, --output <filename> output file to FILENAME
46+
--timeout <seconds> timeout of checking request in seconds
47+
--trace display detail error messages
4248
`)
4349

44-
var description string
45-
t := reflect.TypeOf(opts)
46-
47-
for i := 0; i < t.NumField(); i++ {
48-
tag := t.Field(i).Tag
49-
50-
if sh := tag.Get("short"); sh != "" {
51-
description = fmt.Sprintf("-%s, --%s", sh, tag.Get("long"))
52-
} else {
53-
description = fmt.Sprintf("--%s", tag.Get("long"))
54-
}
55-
56-
fmt.Fprintf(&buf, " %-20s %s\n", description, tag.Get("description"))
57-
}
58-
5950
return buf.Bytes()
6051
}

pget.go

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,26 @@ package pget
22

33
import (
44
"fmt"
5-
"net/url"
65
"os"
76
"runtime"
87

98
"github.com/asaskevich/govalidator"
109
"github.com/pkg/errors"
1110
)
1211

13-
const version = "0.0.1"
12+
const (
13+
version = "0.0.2"
14+
msg = "Pget v" + version + ", parallel file download client\n"
15+
)
1416

1517
// Pget structs
1618
type Pget struct {
1719
Trace bool
18-
procs int
19-
args []string
20-
url string
2120
Utils
21+
procs int
22+
args []string
23+
url string
24+
timeout int
2225
}
2326

2427
type ignore struct {
@@ -32,8 +35,10 @@ type cause interface {
3235
// New for pget package
3336
func New() *Pget {
3437
return &Pget{
35-
Trace: false,
36-
Utils: &Data{},
38+
Trace: false,
39+
Utils: &Data{},
40+
procs: 2, // default
41+
timeout: 10,
3742
}
3843
}
3944

@@ -84,12 +89,14 @@ func (pget *Pget) ready() error {
8489
pget.Trace = opts.Trace
8590
}
8691

87-
if opts.Procs <= 0 {
88-
pget.procs = 2 // default
89-
} else {
92+
if opts.Procs > 2 {
9093
pget.procs = opts.Procs
9194
}
9295

96+
if opts.Timeout > 0 {
97+
pget.timeout = opts.Timeout
98+
}
99+
93100
if err := pget.parseURLs(); err != nil {
94101
return errors.Wrap(err, "failed to parse of url")
95102
}
@@ -144,7 +151,7 @@ func (pget *Pget) parseOptions(opts *Options, argv []string) error {
144151
}
145152

146153
if opts.Version {
147-
os.Stdout.Write([]byte("Pget " + version + ", a parallel file download client\n"))
154+
os.Stdout.Write([]byte(msg))
148155
return pget.makeIgnoreErr()
149156
}
150157

@@ -167,11 +174,5 @@ func (pget *Pget) parseURLs() error {
167174
return errors.New("url has not been set in argument")
168175
}
169176

170-
u, err := url.Parse(pget.url)
171-
if err != nil {
172-
return errors.Wrap(err, "failed to url parse")
173-
}
174-
pget.url = u.String()
175-
176177
return nil
177178
}

requests.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"io"
66
"net/http"
77
"os"
8+
"time"
89

910
"github.com/pkg/errors"
1011
"golang.org/x/net/context"
@@ -28,7 +29,10 @@ func (p *Pget) Checking() error {
2829
url := p.url
2930

3031
// checking
31-
res, err := http.Get(url)
32+
client := http.Client{
33+
Timeout: time.Duration(p.timeout) * time.Second,
34+
}
35+
res, err := client.Get(url)
3236
if err != nil {
3337
return errors.Wrap(err, "failed to head request: "+url)
3438
}
@@ -38,8 +42,8 @@ func (p *Pget) Checking() error {
3842
return errors.Errorf("not supported range access: %s", url)
3943
}
4044

41-
// To perform to the correct "range access"
42-
// get of the last url in the redirect
45+
// To perform with the correct "range access"
46+
// get the last url in the redirect
4347
_url := res.Request.URL.String()
4448
if p.isNotLastURL(_url) {
4549
p.url = _url

0 commit comments

Comments
 (0)