Skip to content

Commit

Permalink
Merge pull request #125 from hashicorp/checksum_skip_dl
Browse files Browse the repository at this point in the history
don't get file when checksum of destination is valid
  • Loading branch information
vancluever authored Dec 13, 2018
2 parents bd1edc2 + bb46a02 commit be39683
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ is shown below:
The checksum query parameter is never sent to the backend protocol
implementation. It is used at a higher level by go-getter itself.

If the destination file exists and the checksums match: download
will be skipped.

### Unarchiving

go-getter will automatically unarchive files into a file or directory
Expand Down
21 changes: 15 additions & 6 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,15 +248,24 @@ func (c *Client) Get() error {
// If we're not downloading a directory, then just download the file
// and return.
if mode == ClientModeFile {
err := g.GetFile(dst, u)
if err != nil {
return err
}

getFile := true
if checksumHash != nil {
if err := checksum(dst, checksumHash, checksumValue); err != nil {
if err := checksum(dst, checksumHash, checksumValue); err == nil {
// don't get the file if the checksum of dst is correct
getFile = false
}
}
if getFile {
err := g.GetFile(dst, u)
if err != nil {
return err
}

if checksumHash != nil {
if err := checksum(dst, checksumHash, checksumValue); err != nil {
return err
}
}
}

if decompressor != nil {
Expand Down
36 changes: 36 additions & 0 deletions get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,39 @@ func TestGetFile_filename(t *testing.T) {
t.Fatalf("err: %s", err)
}
}

func TestGetFile_checksumSkip(t *testing.T) {
dst := tempFile(t)
u := testModule("basic-file/foo.txt") + "?checksum=md5:09f7e02f1290be211da707a266f153b3"

getter := &MockGetter{Proxy: new(FileGetter)}
client := &Client{
Src: u,
Dst: dst,
Dir: false,
Getters: map[string]Getter{
"file": getter,
},
}

// get the file
if err := client.Get(); err != nil {
t.Fatalf("err: %s", err)
}

if v := getter.GetFileURL.Query().Get("checksum"); v != "" {
t.Fatalf("bad: %s", v)
}

// remove proxy file getter and reset GetFileCalled so that we can re-test.
getter.Proxy = nil
getter.GetFileCalled = false

if err := client.Get(); err != nil {
t.Fatalf("err: %s", err)
}

if getter.GetFileCalled {
t.Fatalf("get should not have been called")
}
}

0 comments on commit be39683

Please sign in to comment.