Skip to content

Commit

Permalink
Release v0.0.2
Browse files Browse the repository at this point in the history
Added timeout option for checking method
Improved usage messages

Stable now
  • Loading branch information
Code-Hex committed Jul 7, 2016
1 parent 25eb37e commit 6eed7d1
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
25 changes: 8 additions & 17 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"fmt"
"os"
"reflect"

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

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

var description string
t := reflect.TypeOf(opts)

for i := 0; i < t.NumField(); i++ {
tag := t.Field(i).Tag

if sh := tag.Get("short"); sh != "" {
description = fmt.Sprintf("-%s, --%s", sh, tag.Get("long"))
} else {
description = fmt.Sprintf("--%s", tag.Get("long"))
}

fmt.Fprintf(&buf, " %-20s %s\n", description, tag.Get("description"))
}

return buf.Bytes()
}
35 changes: 18 additions & 17 deletions pget.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,26 @@ package pget

import (
"fmt"
"net/url"
"os"
"runtime"

"github.com/asaskevich/govalidator"
"github.com/pkg/errors"
)

const version = "0.0.1"
const (
version = "0.0.2"
msg = "Pget v" + version + ", parallel file download client\n"
)

// Pget structs
type Pget struct {
Trace bool
procs int
args []string
url string
Utils
procs int
args []string
url string
timeout int
}

type ignore struct {
Expand All @@ -32,8 +35,10 @@ type cause interface {
// New for pget package
func New() *Pget {
return &Pget{
Trace: false,
Utils: &Data{},
Trace: false,
Utils: &Data{},
procs: 2, // default
timeout: 10,
}
}

Expand Down Expand Up @@ -84,12 +89,14 @@ func (pget *Pget) ready() error {
pget.Trace = opts.Trace
}

if opts.Procs <= 0 {
pget.procs = 2 // default
} else {
if opts.Procs > 2 {
pget.procs = opts.Procs
}

if opts.Timeout > 0 {
pget.timeout = opts.Timeout
}

if err := pget.parseURLs(); err != nil {
return errors.Wrap(err, "failed to parse of url")
}
Expand Down Expand Up @@ -144,7 +151,7 @@ func (pget *Pget) parseOptions(opts *Options, argv []string) error {
}

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

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

u, err := url.Parse(pget.url)
if err != nil {
return errors.Wrap(err, "failed to url parse")
}
pget.url = u.String()

return nil
}
10 changes: 7 additions & 3 deletions requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"os"
"time"

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

// checking
res, err := http.Get(url)
client := http.Client{
Timeout: time.Duration(p.timeout) * time.Second,
}
res, err := client.Get(url)
if err != nil {
return errors.Wrap(err, "failed to head request: "+url)
}
Expand All @@ -38,8 +42,8 @@ func (p *Pget) Checking() error {
return errors.Errorf("not supported range access: %s", url)
}

// To perform to the correct "range access"
// get of the last url in the redirect
// To perform with the correct "range access"
// get the last url in the redirect
_url := res.Request.URL.String()
if p.isNotLastURL(_url) {
p.url = _url
Expand Down

0 comments on commit 6eed7d1

Please sign in to comment.