io example fix
Vladimir Bauer
7 years ago
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "io" | |
| 5 | "net/http" | |
| 6 | "os" | |
| 7 | "path/filepath" | |
| 8 | "time" | |
| 9 | ||
| 10 | "github.com/vbauerster/mpb/v4" | |
| 11 | "github.com/vbauerster/mpb/v4/decor" | |
| 12 | ) | |
| 13 | ||
| 14 | func main() { | |
| 15 | url := "https://github.com/onivim/oni/releases/download/v0.3.4/Oni-0.3.4-amd64-linux.deb" | |
| 16 | ||
| 17 | resp, err := http.Get(url) | |
| 18 | if err != nil { | |
| 19 | panic(err) | |
| 20 | } | |
| 21 | defer resp.Body.Close() | |
| 22 | ||
| 23 | if resp.StatusCode != http.StatusOK { | |
| 24 | fmt.Printf("Server return non-200 status: %s\n", resp.Status) | |
| 25 | return | |
| 26 | } | |
| 27 | ||
| 28 | size := resp.ContentLength | |
| 29 | ||
| 30 | // create dest | |
| 31 | destName := filepath.Base(url) | |
| 32 | dest, err := os.Create(destName) | |
| 33 | if err != nil { | |
| 34 | fmt.Printf("Can't create %s: %v\n", destName, err) | |
| 35 | return | |
| 36 | } | |
| 37 | defer dest.Close() | |
| 38 | ||
| 39 | p := mpb.New( | |
| 40 | mpb.WithWidth(60), | |
| 41 | mpb.WithRefreshRate(180*time.Millisecond), | |
| 42 | ) | |
| 43 | ||
| 44 | bar := p.AddBar(size, mpb.BarStyle("[=>-|"), | |
| 45 | mpb.PrependDecorators( | |
| 46 | decor.CountersKibiByte("% 6.1f / % 6.1f"), | |
| 47 | ), | |
| 48 | mpb.AppendDecorators( | |
| 49 | decor.EwmaETA(decor.ET_STYLE_MMSS, float64(size)/2048), | |
| 50 | decor.Name(" ] "), | |
| 51 | decor.AverageSpeed(decor.UnitKiB, "% .2f"), | |
| 52 | ), | |
| 53 | ) | |
| 54 | ||
| 55 | // create proxy reader | |
| 56 | reader := bar.ProxyReader(resp.Body) | |
| 57 | ||
| 58 | // and copy from reader, ignoring errors | |
| 59 | io.Copy(dest, reader) | |
| 60 | ||
| 61 | p.Wait() | |
| 62 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "io" | |
| 5 | "log" | |
| 6 | "net/http" | |
| 7 | "os" | |
| 8 | "path/filepath" | |
| 9 | "sync" | |
| 10 | ||
| 11 | "github.com/vbauerster/mpb/v4" | |
| 12 | "github.com/vbauerster/mpb/v4/decor" | |
| 13 | ) | |
| 14 | ||
| 15 | func main() { | |
| 16 | log.SetOutput(os.Stderr) | |
| 17 | ||
| 18 | url1 := "https://homebrew.bintray.com/bottles/youtube-dl-2016.12.12.sierra.bottle.tar.gz" | |
| 19 | url2 := "https://homebrew.bintray.com/bottles/libtiff-4.0.7.sierra.bottle.tar.gz" | |
| 20 | ||
| 21 | var wg sync.WaitGroup | |
| 22 | p := mpb.New(mpb.WithWidth(64), mpb.WithWaitGroup(&wg)) | |
| 23 | ||
| 24 | for i, url := range [...]string{url1, url2} { | |
| 25 | wg.Add(1) | |
| 26 | name := fmt.Sprintf("url%d:", i+1) | |
| 27 | go download(&wg, p, name, url, i) | |
| 28 | } | |
| 29 | ||
| 30 | p.Wait() | |
| 31 | } | |
| 32 | ||
| 33 | func download(wg *sync.WaitGroup, p *mpb.Progress, name, url string, n int) { | |
| 34 | defer wg.Done() | |
| 35 | resp, err := http.Get(url) | |
| 36 | if err != nil { | |
| 37 | log.Printf("%s: %v", name, err) | |
| 38 | return | |
| 39 | } | |
| 40 | defer resp.Body.Close() | |
| 41 | ||
| 42 | if resp.StatusCode != http.StatusOK { | |
| 43 | err = fmt.Errorf("non-200 status: %s", resp.Status) | |
| 44 | log.Printf("%s: %v", name, err) | |
| 45 | return | |
| 46 | } | |
| 47 | ||
| 48 | size := resp.ContentLength | |
| 49 | ||
| 50 | // create dest | |
| 51 | destName := filepath.Base(url) | |
| 52 | dest, err := os.Create(destName) | |
| 53 | if err != nil { | |
| 54 | err = fmt.Errorf("Can't create %s: %v", destName, err) | |
| 55 | log.Printf("%s: %v", name, err) | |
| 56 | return | |
| 57 | } | |
| 58 | ||
| 59 | // create bar with appropriate decorators | |
| 60 | bar := p.AddBar(size, mpb.BarPriority(n), | |
| 61 | mpb.PrependDecorators( | |
| 62 | decor.Name(name, decor.WC{W: len(name) + 1, C: decor.DidentRight}), | |
| 63 | decor.CountersKibiByte("%6.1f / %6.1f", decor.WCSyncWidth), | |
| 64 | ), | |
| 65 | mpb.AppendDecorators( | |
| 66 | decor.EwmaETA(decor.ET_STYLE_HHMMSS, 1024*4, decor.WCSyncWidth), | |
| 67 | decor.AverageSpeed(decor.UnitKiB, "% .2f"), | |
| 68 | ), | |
| 69 | ) | |
| 70 | ||
| 71 | // create proxy reader | |
| 72 | reader := bar.ProxyReader(resp.Body) | |
| 73 | // and copy from reader | |
| 74 | _, err = io.Copy(dest, reader) | |
| 75 | ||
| 76 | if e := dest.Close(); err == nil { | |
| 77 | err = e | |
| 78 | } | |
| 79 | if err != nil { | |
| 80 | log.Printf("%s: %v", name, err) | |
| 81 | } | |
| 82 | } |
| 0 | package main | |
| 1 | ||
| 2 | import ( | |
| 3 | "fmt" | |
| 4 | "io" | |
| 5 | "net/http" | |
| 6 | "os" | |
| 7 | "path/filepath" | |
| 8 | "time" | |
| 9 | ||
| 10 | "github.com/vbauerster/mpb/v4" | |
| 11 | "github.com/vbauerster/mpb/v4/decor" | |
| 12 | ) | |
| 13 | ||
| 14 | func main() { | |
| 15 | url := "https://github.com/onivim/oni/releases/download/v0.3.4/Oni-0.3.4-amd64-linux.deb" | |
| 16 | ||
| 17 | resp, err := http.Get(url) | |
| 18 | if err != nil { | |
| 19 | panic(err) | |
| 20 | } | |
| 21 | defer resp.Body.Close() | |
| 22 | ||
| 23 | if resp.StatusCode != http.StatusOK { | |
| 24 | fmt.Printf("Server return non-200 status: %s\n", resp.Status) | |
| 25 | return | |
| 26 | } | |
| 27 | ||
| 28 | size := resp.ContentLength | |
| 29 | ||
| 30 | // create dest | |
| 31 | destName := filepath.Base(url) | |
| 32 | dest, err := os.Create(destName) | |
| 33 | if err != nil { | |
| 34 | fmt.Printf("Can't create %s: %v\n", destName, err) | |
| 35 | return | |
| 36 | } | |
| 37 | defer dest.Close() | |
| 38 | ||
| 39 | p := mpb.New( | |
| 40 | mpb.WithWidth(60), | |
| 41 | mpb.WithRefreshRate(180*time.Millisecond), | |
| 42 | ) | |
| 43 | ||
| 44 | bar := p.AddBar(size, mpb.BarStyle("[=>-|"), | |
| 45 | mpb.PrependDecorators( | |
| 46 | decor.CountersKibiByte("% 6.1f / % 6.1f"), | |
| 47 | ), | |
| 48 | mpb.AppendDecorators( | |
| 49 | decor.EwmaETA(decor.ET_STYLE_MMSS, float64(size)/2048), | |
| 50 | decor.Name(" ] "), | |
| 51 | decor.AverageSpeed(decor.UnitKiB, "% .2f"), | |
| 52 | ), | |
| 53 | ) | |
| 54 | ||
| 55 | // create proxy reader | |
| 56 | reader := bar.ProxyReader(resp.Body) | |
| 57 | ||
| 58 | // and copy from reader, ignoring errors | |
| 59 | io.Copy(dest, reader) | |
| 60 | ||
| 61 | p.Wait() | |
| 62 | } |