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.
What Would You Like to Add? Why Is This Needed?
devstream/internal/pkg/upgrade/upgrade.go
Lines 88 to 92 in e07a621
Design
We could just refactor this function:
devstream/pkg/util/scm/github/download.go
Line 13 in e07a621
Maybe you could focus on these code:
devstream/pkg/util/scm/github/download.go
Lines 49 to 68 in e07a621
In line 63, now we use
downloader.Downloadwhich 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.
devstream/internal/pkg/pluginmanager/pbdownloader.go
Lines 85 to 107 in e07a621
Anything else
Please be brave to do some refactors. e.g. Maybe you will use
setUpProgressBarwhich is inpluginmanagerpkg and it is an unexported function. So, it is ok to change its place and export it.