From 74c88f3ef6c79e83bc9f60c60dc21490f5dbb0dc Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Fri, 7 Mar 2014 00:37:06 +0400 Subject: [PATCH] Progress when downloading single files and when parsing remote mirrors. --- aptly/interfaces.go | 4 ++++ console/progress.go | 7 +++++++ debian/remote.go | 8 ++++++++ http/download.go | 13 +++++++++++++ http/fake.go | 5 +++++ 5 files changed, 37 insertions(+) diff --git a/aptly/interfaces.go b/aptly/interfaces.go index a639bcad..b6535dbf 100644 --- a/aptly/interfaces.go +++ b/aptly/interfaces.go @@ -54,6 +54,8 @@ type Progress interface { ShutdownBar() // AddBar increments progress for progress bar AddBar(count int) + // SetBar sets current position for progress bar + SetBar(count int) // Printf does printf but in safe manner: not overwriting progress bar Printf(msg string, a ...interface{}) // ColoredPrintf does printf in colored way + newline @@ -73,4 +75,6 @@ type Downloader interface { // Shutdown stops downloader after current tasks are finished, // but doesn't process rest of queue Shutdown() + // GetProgress returns Progress object + GetProgress() Progress } diff --git a/console/progress.go b/console/progress.go index 35ca0b61..bdbc3553 100644 --- a/console/progress.go +++ b/console/progress.go @@ -101,6 +101,13 @@ func (p *Progress) AddBar(count int) { } } +// SetBar sets current position for progress bar +func (p *Progress) SetBar(count int) { + if p.bar != nil { + p.bar.Set(count) + } +} + // Printf does printf but in safe manner: not overwriting progress bar func (p *Progress) Printf(msg string, a ...interface{}) { p.queue <- printTask{code: codePrint, message: fmt.Sprintf(msg, a...)} diff --git a/debian/remote.go b/debian/remote.go index 38de801e..f1a131c8 100644 --- a/debian/remote.go +++ b/debian/remote.go @@ -342,6 +342,9 @@ func (repo *RemoteRepo) Download(progress aptly.Progress, d aptly.Downloader, pa } defer packagesFile.Close() + stat, _ := packagesFile.Stat() + progress.InitBar(stat.Size(), true) + sreader := NewControlFileReader(packagesReader) for { @@ -353,6 +356,9 @@ func (repo *RemoteRepo) Download(progress aptly.Progress, d aptly.Downloader, pa break } + off, _ := packagesFile.Seek(0, 1) + progress.SetBar(int(off)) + var p *Package if kind == "binary" { @@ -373,6 +379,8 @@ func (repo *RemoteRepo) Download(progress aptly.Progress, d aptly.Downloader, pa return err } } + + progress.ShutdownBar() } progress.Printf("Building download queue...\n") diff --git a/http/download.go b/http/download.go index b31f78e9..6a20f64b 100644 --- a/http/download.go +++ b/http/download.go @@ -85,6 +85,11 @@ func (downloader *downloaderImpl) Resume() { } } +// GetProgress returns Progress object +func (downloader *downloaderImpl) GetProgress() aptly.Progress { + return downloader.progress +} + // Download starts new download task func (downloader *downloaderImpl) Download(url string, destination string, result chan<- error) { downloader.DownloadWithChecksum(url, destination, result, utils.ChecksumInfo{Size: -1}, false) @@ -211,6 +216,10 @@ func DownloadTempWithChecksum(downloader aptly.Downloader, url string, expected tempfile := filepath.Join(tempdir, "buffer") + if expected.Size != -1 && downloader.GetProgress() != nil { + downloader.GetProgress().InitBar(expected.Size, true) + } + ch := make(chan error, 1) downloader.DownloadWithChecksum(url, tempfile, ch, expected, ignoreMismatch) @@ -219,6 +228,10 @@ func DownloadTempWithChecksum(downloader aptly.Downloader, url string, expected return nil, err } + if expected.Size != -1 && downloader.GetProgress() != nil { + downloader.GetProgress().ShutdownBar() + } + file, err := os.Open(tempfile) if err != nil { return nil, err diff --git a/http/fake.go b/http/fake.go index d325d389..4d256a3f 100644 --- a/http/fake.go +++ b/http/fake.go @@ -130,3 +130,8 @@ func (f *FakeDownloader) Pause() { // Resume does nothing func (f *FakeDownloader) Resume() { } + +// GetProgress returns Progress object +func (f *FakeDownloader) GetProgress() aptly.Progress { + return nil +}