Skip to content

Commit

Permalink
feat(display): add progress
Browse files Browse the repository at this point in the history
  • Loading branch information
mar-heaven committed Aug 23, 2022
1 parent 371f74e commit 33ff4c1
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions utils/wget.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,20 @@ import (
"strings"
)

type ProgressReader struct {
io.Reader
Total int64
Current int64
}

func (r *ProgressReader) Read(p []byte) (n int, err error) {
n, err = r.Reader.Read(p)

r.Current += int64(n)
fmt.Printf("\rdownloading percent %.2f%%", float64(r.Current*10000/r.Total)/100)
return
}

func WgetBinary(url string, downloadTo string, headers map[string]string) error {
out, err := os.Create(downloadTo)
if err != nil {
Expand Down Expand Up @@ -43,8 +57,12 @@ func WgetBinary(url string, downloadTo string, headers map[string]string) error
if len(resp.Header["Content-Type"]) > 0 && strings.Contains(resp.Header["Content-Type"][0], "text") {
return errors.New("target is not a binary")
}

_, err = io.Copy(out, resp.Body)
reader := &ProgressReader{
Reader: resp.Body,
Total: resp.ContentLength,
}
_, err = io.Copy(out, reader)
fmt.Printf("\n")
if err != nil {
return err
}
Expand Down

0 comments on commit 33ff4c1

Please sign in to comment.