Skip to content

Commit

Permalink
Added second argument for custom-formatted progress-bar to NewDownloa…
Browse files Browse the repository at this point in the history
…dClient in common/download_test.go. This second parameter was added as a result of commit f0bd901 which lets you customize the progress-bar format.
  • Loading branch information
arizvisa committed Dec 21, 2017
1 parent 745d62f commit 058b399
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 12 deletions.
7 changes: 6 additions & 1 deletion common/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ func HashForType(t string) hash.Hash {
func NewDownloadClient(c *DownloadConfig, bar *pb.ProgressBar) *DownloadClient {
const mtu = 1500 /* ethernet */ - 20 /* ipv4 */ - 20 /* tcp */

// If no custom progress-bar was specified, then create a default one.
if bar == nil {
bar = pb.New64(0)
}

// Create downloader map if it hasn't been specified already.
if c.DownloaderMap == nil {
// Create downloader map
c.DownloaderMap = map[string]Downloader{
"file": &FileDownloader{progress: bar, bufferSize: nil},
"ftp": &FTPDownloader{progress: bar, userInfo: url.UserPassword("anonymous", "anonymous@"), mtu: mtu},
Expand Down
20 changes: 10 additions & 10 deletions common/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestDownloadClientVerifyChecksum(t *testing.T) {
Checksum: checksum,
}

d := NewDownloadClient(config)
d := NewDownloadClient(config, nil)
result, err := d.VerifyChecksum(tf.Name())
if err != nil {
t.Fatalf("Verify err: %s", err)
Expand All @@ -59,7 +59,7 @@ func TestDownloadClient_basic(t *testing.T) {
Url: ts.URL + "/basic.txt",
TargetPath: tf.Name(),
CopyFile: true,
})
}, nil)

path, err := client.Get()
if err != nil {
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestDownloadClient_checksumBad(t *testing.T) {
Hash: HashForType("md5"),
Checksum: checksum,
CopyFile: true,
})
}, nil)
if _, err := client.Get(); err == nil {
t.Fatal("should error")
}
Expand All @@ -120,7 +120,7 @@ func TestDownloadClient_checksumGood(t *testing.T) {
Hash: HashForType("md5"),
Checksum: checksum,
CopyFile: true,
})
}, nil)
path, err := client.Get()
if err != nil {
t.Fatalf("err: %s", err)
Expand Down Expand Up @@ -151,7 +151,7 @@ func TestDownloadClient_checksumNoDownload(t *testing.T) {
Hash: HashForType("md5"),
Checksum: checksum,
CopyFile: true,
})
}, nil)
path, err := client.Get()
if err != nil {
t.Fatalf("err: %s", err)
Expand Down Expand Up @@ -190,7 +190,7 @@ func TestDownloadClient_resume(t *testing.T) {
Url: ts.URL,
TargetPath: tf.Name(),
CopyFile: true,
})
}, nil)
path, err := client.Get()
if err != nil {
t.Fatalf("err: %s", err)
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestDownloadClient_usesDefaultUserAgent(t *testing.T) {
CopyFile: true,
}

client := NewDownloadClient(config)
client := NewDownloadClient(config, nil)
_, err = client.Get()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -282,7 +282,7 @@ func TestDownloadClient_setsUserAgent(t *testing.T) {
CopyFile: true,
}

client := NewDownloadClient(config)
client := NewDownloadClient(config, nil)
_, err = client.Get()
if err != nil {
t.Fatal(err)
Expand Down Expand Up @@ -381,7 +381,7 @@ func TestDownloadFileUrl(t *testing.T) {
CopyFile: false,
}

client := NewDownloadClient(config)
client := NewDownloadClient(config, nil)

// Verify that we fail to match the checksum
_, err = client.Get()
Expand Down Expand Up @@ -412,7 +412,7 @@ func SimulateFileUriDownload(t *testing.T, uri string) (string, error) {
}

// go go go
client := NewDownloadClient(config)
client := NewDownloadClient(config, nil)
path, err := client.Get()

// ignore any non-important checksum errors if it's not a unc path
Expand Down
2 changes: 1 addition & 1 deletion common/step_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (s *StepDownload) Run(state multistep.StateBag) multistep.StepAction {
}
downloadConfigs[i] = config

if match, _ := NewDownloadClient(config).VerifyChecksum(config.TargetPath); match {
if match, _ := NewDownloadClient(config, nil).VerifyChecksum(config.TargetPath); match {
ui.Message(fmt.Sprintf("Found already downloaded, initial checksum matched, no download needed: %s", url))
finalPath = config.TargetPath
break
Expand Down

0 comments on commit 058b399

Please sign in to comment.