Skip to content

Commit

Permalink
refactor: update DownloadFile function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nikoksr committed May 19, 2020
1 parent 349df7c commit 4f8601f
Showing 1 changed file with 3 additions and 23 deletions.
26 changes: 3 additions & 23 deletions pkg/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 4f8601f

Please sign in to comment.