Skip to content

Commit 998f24c

Browse files
committed
Added verbose Mode, Makefile, Travis CI and Improved CLI
1 parent b5df639 commit 998f24c

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

.travis.yml

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
language: go
2+
3+
go:
4+
- 1.7.x
5+
- 1.8.x
6+
- 1.9.x
7+
- master

Makefile

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
build:
2+
GOOS=linux GOARCH=amd64 go build -o=pluto.linux.amd64
3+
GOOS=linux GOARCH=386 go build -o=pluto.linux.i386
4+
GOOS=windows GOARCH=amd64 go build -o=pluto.amd64.exe
5+
GOOS=windows GOARCH=386 go build -o=pluto.i386.exe
6+
7+
clean:
8+
rm pluto.linux.i386
9+
rm pluto.linux.amd64
10+
rm pluto.i386.exe
11+
rm pluto.amd64.exe

pluto/pluto.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66
"io/ioutil"
7+
"log"
78
"net/http"
89
"net/url"
910
"os"
@@ -28,7 +29,7 @@ type FileMeta struct {
2829
// then downloads the file by dividing it into given number of parts and downloading all parts concurrently.
2930
// If any error occurs in the downloading stage of any part, It'll wait for 2 seconds, Discard the existing part and restart it.
3031
// Discarding whatever bytes were downloaded isn't exactly a smart, So, I'll also be implementing a feature where it can skip over what is already downloaded.
31-
func Download(linkp *url.URL, parts int) (*os.File, error) {
32+
func Download(linkp *url.URL, parts int, verbose bool) (*os.File, error) {
3233

3334
if linkp == nil {
3435
return nil, fmt.Errorf("No URL Provided")
@@ -63,10 +64,10 @@ func Download(linkp *url.URL, parts int) (*os.File, error) {
6364
}
6465
}
6566

66-
return startDownload(workers)
67+
return startDownload(workers, verbose)
6768
}
6869

69-
func startDownload(w []*worker) (*os.File, error) {
70+
func startDownload(w []*worker, verbose bool) (*os.File, error) {
7071

7172
var wg sync.WaitGroup
7273
wg.Add(len(w))
@@ -91,6 +92,10 @@ func startDownload(w []*worker) (*os.File, error) {
9192
cerr <- err
9293
return
9394
}
95+
96+
if verbose {
97+
log.Printf("error in downloading a part: %v", err)
98+
}
9499
continue
95100
}
96101

pluto_cli.go

+17-11
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func main() {
2727
os.Exit(0)
2828
}()
2929

30-
parts := flag.Int("part", 16, "Number of Download parts")
31-
30+
parts := flag.Int("part", 32, "Number of Download parts")
31+
verbose := flag.Bool("verbose", false, "Enable Verbose Mode")
3232
flag.Parse()
3333
urls := []string{}
3434
u := ""
@@ -40,7 +40,7 @@ func main() {
4040
log.Fatalln("No URL Provided")
4141
}
4242

43-
download(u, *parts)
43+
download(u, *parts, *verbose)
4444
} else {
4545

4646
if strings.Contains(os.Args[1], "--part") {
@@ -57,33 +57,39 @@ func main() {
5757
}
5858
}
5959
for _, v := range urls {
60-
download(v, *parts)
60+
download(v, *parts, *verbose)
6161
}
6262
}
6363

6464
}
6565

66-
func download(u string, parts int) {
66+
func download(u string, parts int, verbose bool) {
6767
a := time.Now()
6868

69+
defer func() { select {} }()
70+
6971
up, err := url.Parse(u)
7072
if err != nil {
71-
log.Fatalln("Invalid URL")
73+
log.Println("Invalid URL")
74+
return
7275
}
7376

74-
fname := filepath.Base(up.String())
77+
fname := strings.Split(filepath.Base(up.String()), "?")[0]
78+
fmt.Printf("Starting Download with %d parts\n", parts)
7579

7680
fmt.Printf("Downloading %s\n", up.String())
7781

78-
f, err := pluto.Download(up, parts)
82+
f, err := pluto.Download(up, parts, verbose)
7983
if err != nil {
80-
log.Fatalln(err)
84+
log.Println(err)
85+
return
8186
}
8287
defer f.Close()
8388

8489
file, err := os.Create(fname)
8590
if err != nil {
86-
log.Fatalln(err.Error())
91+
log.Println(err.Error())
92+
return
8793
}
8894

8995
defer file.Close()
@@ -92,6 +98,6 @@ func download(u string, parts int) {
9298
fmt.Printf("Downloaded %s in %s\n", up.String(), time.Since(a))
9399
_, err = io.Copy(file, f)
94100
if err != nil {
95-
log.Fatalln(err.Error())
101+
log.Println(err.Error())
96102
}
97103
}

0 commit comments

Comments
 (0)