Skip to content

🍀 Proposal: Add process bar during dtm upgrade #1146

Closed

Description

@aFlyBird0

What Would You Like to Add? Why Is This Needed?

// TODO(hxcGit): add download progress bar
if err = ghClient.DownloadAsset(ltstReleaseTagName, assetName, dtmTmpFileName); err != nil {
log.Debugf("Failed to download dtm: %v-%v.", ltstReleaseTagName, assetName)
return err
}

Design

We could just refactor this function:

func (c *Client) DownloadAsset(tagName, assetName, fileName string) error {

Maybe you could focus on these code:

var downloadUrl string
for _, a := range assets {
if a.GetName() == assetName {
downloadUrl = a.GetBrowserDownloadURL()
log.Debugf("Download url: %s.", downloadUrl)
break
}
}
if downloadUrl == "" {
log.Debugf("Failed to got the download url for %s, maybe it not exists.", assetName)
return fmt.Errorf("failed to got the download url for %s, maybe it not exists", assetName)
}
// 4. download
n, err := downloader.Download(downloadUrl, fileName, c.WorkPath)
if err != nil {
log.Debugf("Failed to download asset from %s.", downloadUrl)
return err
}
log.Debugf("Downloaded <%d> bytes.", n)

In line 63, now we use downloader.Download which doesn't support process bar.
Therefore, you should refactor the function or create a new function to support process bar.

I guess this function can help you.

func setUpProgressBar(resp *http.Response, downFile *os.File) error {
//get size
i, _ := strconv.Atoi(resp.Header.Get("Content-Length"))
sourceSiz := int64(i)
source := resp.Body
//create a bar and set param
bar := pb.New(int(sourceSiz)).SetRefreshRate(time.Millisecond * 10).SetUnits(pb.U_BYTES).SetWidth(100)
bar.ShowSpeed = true
bar.ShowTimeLeft = true
bar.ShowFinalTime = true
bar.SetWidth(80)
bar.Start()
writer := io.MultiWriter(downFile, bar)
_, err := io.Copy(writer, source)
if err != nil {
log.Error(err)
return err
}
bar.Finish()
return nil
}

Anything else

Please be brave to do some refactors. e.g. Maybe you will use setUpProgressBar which is in pluginmanager pkg and it is an unexported function. So, it is ok to change its place and export it.

Activity

  1. CrazyCollin commented on Oct 2, 2022

    @CrazyCollin

    Maybe I can have a try.

  2. daniel-hutao commented on Oct 6, 2022

    @daniel-hutao
    Member

    Maybe I can have a try.

    Hi, @CrazyCollin, the issues marked with the "good first issue" label are intended for first-time contributors. Let's save the remaining "good first issue" items for other new contributors.

  3. CrazyCollin commented on Oct 6, 2022

    @CrazyCollin

    Feel so sorry, I'll pay attention next time.

  4. 0zyt commented on Oct 7, 2022

    @0zyt

    I want to work on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions