Skip to content

Commit

Permalink
Merge pull request #70 from nao1215/add-progress-bar
Browse files Browse the repository at this point in the history
Add download progress bar
  • Loading branch information
nao1215 authored Feb 20, 2023
2 parents 7a89e15 + 9eb4f6d commit 360e9b8
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion cmd/update-go.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,10 @@ func fetchGolangTarball(tarballName string) error {
}
defer tarball.Close()

_, err = io.Copy(tarball, resp.Body)
progress := NewProgress(resp.ContentLength)
defer progress.Finish()

_, err = io.Copy(tarball, io.TeeReader(resp.Body, progress))
if err != nil {
return err
}
Expand Down Expand Up @@ -309,3 +312,33 @@ func recovery(targetPath, backupPath string) error {
}
return nil
}

type Progress struct {
Total int64
Current int64
}

func NewProgress(total int64) *Progress {
return &Progress{
Total: total,
Current: 0,
}
}

func (p *Progress) Write(data []byte) (n int, err error) {
n = len(data)
p.Current += int64(n)
p.Show()
return
}

func (p *Progress) Show() {
if p.Total == 0 {
return
}
fmt.Printf("\rDownloading... %d/%d kB (%d%%)", p.Current/1000, p.Total/1000, (p.Current*100)/p.Total)
}

func (p *Progress) Finish() {
fmt.Println()
}

0 comments on commit 360e9b8

Please sign in to comment.