-
-
Notifications
You must be signed in to change notification settings - Fork 958
/
fetch.go
49 lines (40 loc) · 1.25 KB
/
fetch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package util
import (
"context"
"net/http"
"os"
"time"
"github.com/gruntwork-io/go-commons/errors"
"github.com/hashicorp/go-getter/v2"
)
// FetchFile downloads the file from the given `downloadURL` into the specified `saveToFile` file.
func FetchFile(ctx context.Context, downloadURL, saveToFile string) error {
req, err := http.NewRequestWithContext(ctx, http.MethodGet, downloadURL, nil)
if err != nil {
return errors.WithStackTrace(err)
}
resp, err := (&http.Client{
Timeout: time.Minute * 1,
}).Do(req)
if err != nil {
return errors.WithStackTrace(err)
}
defer resp.Body.Close() //nolint:errcheck
if resp.StatusCode != http.StatusOK {
return errors.Errorf("failed to fetch %s, server response %q", downloadURL, http.StatusText(resp.StatusCode))
}
out, err := os.Create(saveToFile)
if err != nil {
return errors.WithStackTrace(err)
}
defer out.Close() //nolint:errcheck
if written, err := getter.Copy(ctx, out, resp.Body); err != nil {
return errors.WithStackTrace(err)
} else if written != resp.ContentLength {
return errors.Errorf("failed to fetch %s, original size %d but fetched size %d", downloadURL, resp.ContentLength, written)
}
if err := out.Sync(); err != nil {
return errors.WithStackTrace(err)
}
return nil
}