From 4f8601fd9e70c466ff9a6245d8407c4a208e3528 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niko=20K=C3=B6ser?= Date: Tue, 19 May 2020 18:15:04 +0200 Subject: [PATCH] refactor: update DownloadFile function The DownloadFile function now is a wrapper for hashicorp's getter.GetFile function which adds stability and opens up more options for downloading files from various platforms. --- pkg/helper/helper.go | 26 +++----------------------- 1 file changed, 3 insertions(+), 23 deletions(-) diff --git a/pkg/helper/helper.go b/pkg/helper/helper.go index d9680a89..44d759b0 100644 --- a/pkg/helper/helper.go +++ b/pkg/helper/helper.go @@ -2,12 +2,12 @@ package helper import ( "fmt" - "io" - "net/http" "os" "strconv" "strings" "testing" + + "github.com/hashicorp/go-getter" ) // DoesPathExist checks if a given path exists in the filesystem. @@ -75,27 +75,7 @@ func CreateFolderIfNotExists(path string) error { // DownloadFile downloads a file from an url to the local fs. func DownloadFile(src, dst string) error { - // Get the data - resp, err := http.Get(src) - if err != nil { - return err - } - if resp.StatusCode != 200 { - return fmt.Errorf("error: %s", resp.Status) - } - - defer resp.Body.Close() - - // Create the file - out, err := os.Create(dst) - if err != nil { - return err - } - defer out.Close() - - // Write the body to file - _, err = io.Copy(out, resp.Body) - return err + return getter.GetFile(dst, src) } // DownloadFileIfNotExists runs downloadFile() if the destination file doesn't already exist.