Skip to content

Commit

Permalink
feat: add progress notification to download
Browse files Browse the repository at this point in the history
  • Loading branch information
dpastoor committed Jun 2, 2022
1 parent aaa3e89 commit 81c962f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 6 deletions.
8 changes: 6 additions & 2 deletions cmd/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/dpastoor/qvm/internal/pipeline"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

type installCmd struct {
Expand All @@ -20,6 +21,7 @@ type installCmd struct {
}

type installOpts struct {
progress bool
}

func newInstall(installOpts installOpts, release string) error {
Expand All @@ -41,7 +43,7 @@ func newInstall(installOpts installOpts, release string) error {
return nil
}
log.Info("attempting to install quarto version: ", release)
res, err := pipeline.DownloadReleaseVersion(release, runtime.GOOS)
res, err := pipeline.DownloadReleaseVersion(release, runtime.GOOS, installOpts.progress)
if err != nil {
return err
}
Expand All @@ -51,7 +53,7 @@ func newInstall(installOpts installOpts, release string) error {
}

func setInstallOpts(installOpts *installOpts) {

installOpts.progress = !viper.GetBool("no-progress")
}

func (opts *installOpts) Validate() error {
Expand Down Expand Up @@ -100,6 +102,8 @@ func newInstallCmd() *installCmd {
return nil
},
}
cmd.Flags().BoolP("no-progress", "", false, "do not print download progress")
viper.BindPFlag("no-progress", cmd.Flags().Lookup("no-progress"))
root.cmd = cmd
return root
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.18

require (
github.com/adrg/xdg v0.4.0
github.com/dustin/go-humanize v1.0.0
github.com/google/go-github/v44 v44.1.0
github.com/mholt/archiver/v4 v4.0.0-alpha.6.0.20220421032531-8a97d87612e9
github.com/muesli/mango-cobra v1.1.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dsnet/compress v0.0.1 h1:PlZu0n3Tuv04TzpfPbrnI0HW/YwodEXDS+oPKahKF0Q=
github.com/dsnet/compress v0.0.1/go.mod h1:Aw8dCMJ7RioblQeTqt88akK31OvO8Dhf5JflhBbQEHo=
github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY=
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4=
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
Expand Down
26 changes: 24 additions & 2 deletions internal/gh/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"
"time"

"github.com/dustin/go-humanize"
"github.com/google/go-github/v44/github"
log "github.com/sirupsen/logrus"
)
Expand Down Expand Up @@ -49,10 +50,30 @@ func (o osAssetSuffix) String() string {
}
}

type WriteCounter struct {
Written int
Total int
Label string
nWrites int
progress bool
}

func (wc *WriteCounter) Write(p []byte) (int, error) {
n := len(p)
wc.Written += n
wc.nWrites++
if wc.progress && wc.nWrites%200 == 0 {
log.Infof("downloading quarto version %s ... (%s/%s)", wc.Label,
humanize.Bytes(uint64(wc.Written)),
humanize.Bytes(uint64(wc.Total)))
}
return n, nil
}

// DownloadReleaseAsset downloads the release asset for a given platform to a temp
// file and returns the path to the written file.
// targetOs should be "windows", "darwin", "linux"
func DownloadReleaseAsset(client *github.Client, tag string, targetOs string) (string, error) {
func DownloadReleaseAsset(client *github.Client, tag string, targetOs string, progress bool) (string, error) {
switch targetOs {
case "windows", "darwin", "linux":
break
Expand Down Expand Up @@ -83,7 +104,8 @@ func DownloadReleaseAsset(client *github.Client, tag string, targetOs string) (s
defer tmpFile.Close()
log.Tracef("starting to copy release asset to %s\n", tmpFile.Name())
start = time.Now()
wb, err := io.Copy(tmpFile, rc)
counter := &WriteCounter{Total: asset.GetSize(), Label: tag, progress: progress}
wb, err := io.Copy(tmpFile, io.TeeReader(rc, counter))
log.Tracef("done copying release asset in %s\n", time.Since(start))
if err != nil {
return tmpFile.Name(), err
Expand Down
4 changes: 2 additions & 2 deletions internal/pipeline/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (

// DownloadReleaseVersion downloads the appropriate tarball for a given platform
// platform should be windows | linux | darwin
func DownloadReleaseVersion(release string, platform string) (string, error) {
func DownloadReleaseVersion(release string, platform string, progress bool) (string, error) {
client := gh.NewClient(os.Getenv("GITHUB_PAT"))
dlPath, err := gh.DownloadReleaseAsset(client, release, platform)
dlPath, err := gh.DownloadReleaseAsset(client, release, platform, progress)
if err != nil {
return "", err
}
Expand Down

0 comments on commit 81c962f

Please sign in to comment.