Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add process bar during dtm upgrade #1149

Merged
merged 2 commits into from
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 2 additions & 29 deletions internal/pkg/pluginmanager/pbdownloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ package pluginmanager

import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"time"

"github.com/cheggaaa/pb"
"github.com/devstream-io/devstream/pkg/util/downloader"

"github.com/devstream-io/devstream/pkg/util/log"
)
Expand Down Expand Up @@ -60,7 +58,7 @@ func (pd *PbDownloadClient) download(pluginsDir, pluginFilename, version string)

defer downFile.Close()
// create progress bar when reading response body
errSetup := setUpProgressBar(resp, downFile)
_, errSetup := downloader.SetUpProgressBar(resp, downFile)
if errSetup != nil {
log.Error(errSetup)
return errSetup
Expand All @@ -81,31 +79,6 @@ func (pd *PbDownloadClient) download(pluginsDir, pluginFilename, version string)
return nil
}

// setUpProgressBar create bar and setup
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
}

func createPathIfNotExists(path string) error {
_, err := os.Stat(path)
if !os.IsNotExist(err) {
Expand Down
51 changes: 45 additions & 6 deletions pkg/util/downloader/downloader.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package downloader

import (
"bytes"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strconv"
"time"

"github.com/cheggaaa/pb"

"github.com/devstream-io/devstream/pkg/util/log"
)
Expand Down Expand Up @@ -39,11 +42,7 @@ func Download(url, filename, targetDir string) (int64, error) {
log.Debugf("download create file failed: %s", err)
}
}()
content, err := FetchContentFromURL(url)
if err != nil {
return 0, err
}
return io.Copy(f, bytes.NewBuffer(content))
return FetchContentFromURLAndSetUpProgressBar(url, f)
}

func FetchContentFromURL(url string) ([]byte, error) {
Expand All @@ -67,3 +66,43 @@ func FetchContentFromURL(url string) ([]byte, error) {

return io.ReadAll(resp.Body)
}

// setUpProgressBar create bar and setup
func SetUpProgressBar(resp *http.Response, downFile io.Writer) (int64, 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()
defer bar.Finish()
writer := io.MultiWriter(downFile, bar)
return io.Copy(writer, source)
}

func FetchContentFromURLAndSetUpProgressBar(url string, dst io.Writer) (int64, error) {
0zyt marked this conversation as resolved.
Show resolved Hide resolved
resp, err := http.Get(url)

// check response error
if err != nil {
log.Debugf("Download from url failed: %s", err)
return 0, err
}
defer func(body io.ReadCloser) {
err := body.Close()
if err != nil {
log.Errorf("Close response body failed: %s", err)
}
}(resp.Body)

if resp.StatusCode != http.StatusOK {
return 0, fmt.Errorf("Download file from url failed: %+v", resp)
}
return SetUpProgressBar(resp, dst)
}