diff --git a/README.md b/README.md index 86ea5e0f8..c77ec5786 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/client.go b/client.go index 300301c2e..47c4ba337 100644 --- a/client.go +++ b/client.go @@ -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 { diff --git a/get_test.go b/get_test.go index 123c6a314..f7065adc2 100644 --- a/get_test.go +++ b/get_test.go @@ -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") + } +}