🧱 Chunk is a download manager for slow and unstable servers
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge pull request #24 from cuducos/reorg

Splitting library and CLI

authored by

Daniel Fireman and committed by
GitHub
78be3922 b1413716

+34 -20
.gitignore cmd/chunk/.gitignore
+21
cmd/chunk/main.go
··· 1 + package main 2 + 3 + import ( 4 + "fmt" 5 + "log" 6 + "os" 7 + 8 + "github.com/cuducos/chunk" 9 + ) 10 + 11 + func main() { 12 + chunk := chunk.DefaultDownloader() 13 + prog := newProgress() 14 + for status := range chunk.Download(os.Args[1:len(os.Args)]...) { 15 + if status.Error != nil { 16 + log.Fatal(status.Error) 17 + } 18 + prog.update(status) 19 + } 20 + fmt.Printf("\r%s\nDownloaded to: %s", prog.String(), os.TempDir()) 21 + }
+9 -3
example.go cmd/chunk/download_progress.go
··· 4 4 "context" 5 5 "fmt" 6 6 "time" 7 + 8 + "github.com/cuducos/chunk" 7 9 ) 8 10 9 11 func humanSize(s float64) string { ··· 43 45 } 44 46 45 47 func (p *downloadProgress) String() string { 46 - perc := float64(p.done()) / float64(p.total()) 48 + total := float64(p.total()) 49 + perc := 0.0 50 + if total > 0 { 51 + perc = float64(p.done()) / total 52 + } 47 53 speed := float64(p.done()) / time.Since(p.startedAt).Seconds() 48 54 return fmt.Sprintf( 49 55 "Downloading %s of %s\t%.2f%%\t%s/s", ··· 54 60 ) 55 61 } 56 62 57 - func (p *downloadProgress) update(d DownloadStatus) { 63 + func (p *downloadProgress) update(d chunk.DownloadStatus) { 58 64 p.files[d.DownloadedFilePath] = file{d.FileSizeBytes, d.DownloadedFileBytes} 59 65 if p.done() == p.total() { 60 66 p.close() 61 67 } 62 68 } 63 69 64 - func NewProgress() *downloadProgress { 70 + func newProgress() *downloadProgress { 65 71 ctx, cancel := context.WithCancel(context.Background()) 66 72 bar := downloadProgress{make(map[string]file), time.Now(), cancel} 67 73 tick := time.Tick(1 * time.Second)
+1 -14
main.go downloader.go
··· 1 - package main 1 + package chunk 2 2 3 3 import ( 4 4 "bytes" 5 5 "context" 6 6 "fmt" 7 - "log" 8 7 "net/http" 9 8 "os" 10 9 "path/filepath" ··· 304 303 305 304 return &d 306 305 } 307 - 308 - func main() { 309 - chunk := DefaultDownloader() 310 - prog := NewProgress() 311 - for status := range chunk.Download(os.Args[1:len(os.Args)]...) { 312 - if status.Error != nil { 313 - log.Fatal(status.Error) 314 - } 315 - prog.update(status) 316 - } 317 - fmt.Printf("\r%s\nDownloaded to: %s", prog.String(), os.TempDir()) 318 - }
+1 -1
main_test.go downloader_test.go
··· 1 - package main 1 + package chunk 2 2 3 3 import ( 4 4 "context"
+1 -1
progress.go
··· 1 - package main 1 + package chunk 2 2 3 3 import ( 4 4 "encoding/gob"
+1 -1
progress_test.go
··· 1 - package main 1 + package chunk 2 2 3 3 import ( 4 4 "os"