Skip to content

Commit be39683

Browse files
authored
Merge pull request #125 from hashicorp/checksum_skip_dl
don't get file when checksum of destination is valid
2 parents bd1edc2 + bb46a02 commit be39683

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ is shown below:
169169
The checksum query parameter is never sent to the backend protocol
170170
implementation. It is used at a higher level by go-getter itself.
171171

172+
If the destination file exists and the checksums match: download
173+
will be skipped.
174+
172175
### Unarchiving
173176

174177
go-getter will automatically unarchive files into a file or directory

client.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,24 @@ func (c *Client) Get() error {
248248
// If we're not downloading a directory, then just download the file
249249
// and return.
250250
if mode == ClientModeFile {
251-
err := g.GetFile(dst, u)
252-
if err != nil {
253-
return err
254-
}
255-
251+
getFile := true
256252
if checksumHash != nil {
257-
if err := checksum(dst, checksumHash, checksumValue); err != nil {
253+
if err := checksum(dst, checksumHash, checksumValue); err == nil {
254+
// don't get the file if the checksum of dst is correct
255+
getFile = false
256+
}
257+
}
258+
if getFile {
259+
err := g.GetFile(dst, u)
260+
if err != nil {
258261
return err
259262
}
263+
264+
if checksumHash != nil {
265+
if err := checksum(dst, checksumHash, checksumValue); err != nil {
266+
return err
267+
}
268+
}
260269
}
261270

262271
if decompressor != nil {

get_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,3 +378,39 @@ func TestGetFile_filename(t *testing.T) {
378378
t.Fatalf("err: %s", err)
379379
}
380380
}
381+
382+
func TestGetFile_checksumSkip(t *testing.T) {
383+
dst := tempFile(t)
384+
u := testModule("basic-file/foo.txt") + "?checksum=md5:09f7e02f1290be211da707a266f153b3"
385+
386+
getter := &MockGetter{Proxy: new(FileGetter)}
387+
client := &Client{
388+
Src: u,
389+
Dst: dst,
390+
Dir: false,
391+
Getters: map[string]Getter{
392+
"file": getter,
393+
},
394+
}
395+
396+
// get the file
397+
if err := client.Get(); err != nil {
398+
t.Fatalf("err: %s", err)
399+
}
400+
401+
if v := getter.GetFileURL.Query().Get("checksum"); v != "" {
402+
t.Fatalf("bad: %s", v)
403+
}
404+
405+
// remove proxy file getter and reset GetFileCalled so that we can re-test.
406+
getter.Proxy = nil
407+
getter.GetFileCalled = false
408+
409+
if err := client.Get(); err != nil {
410+
t.Fatalf("err: %s", err)
411+
}
412+
413+
if getter.GetFileCalled {
414+
t.Fatalf("get should not have been called")
415+
}
416+
}

0 commit comments

Comments
 (0)