Skip to content

Commit

Permalink
refactor: configure go-getter
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt committed Nov 15, 2023
1 parent e269c6d commit 3940a38
Showing 1 changed file with 37 additions and 2 deletions.
39 changes: 37 additions & 2 deletions x/upgrade/plan/downloader.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package plan

import (
"context"
"errors"
"fmt"
neturl "net/url"
"os"
"path/filepath"
"strings"

"github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/go-getter"
)

Expand All @@ -24,8 +26,9 @@ import (
// NOTE: This functions does not check the provided url for validity.
func DownloadUpgrade(dstRoot, url, daemonName string) error {
target := filepath.Join(dstRoot, "bin", daemonName)

// First try to download it as a single file. If there's no error, it's okay and we're done.
if err := getter.GetFile(target, url); err != nil {
if err := getFile(url, target); err != nil {
// If it was a checksum error, no need to try as directory.
if _, ok := err.(*getter.ChecksumError); ok {
return err
Expand Down Expand Up @@ -109,7 +112,8 @@ func DownloadURL(url string) (string, error) {
}
defer os.RemoveAll(tempDir)
tempFile := filepath.Join(tempDir, "content")
if err = getter.GetFile(tempFile, url); err != nil {

if err := getFile(url, tempFile); err != nil {
return "", fmt.Errorf("could not download url \"%s\": %w", url, err)
}
tempFileBz, rerr := os.ReadFile(tempFile)
Expand All @@ -136,3 +140,34 @@ func ValidateURL(urlStr string, mustChecksum bool) error {

return nil
}

// getFile downloads the given url into the provided directory.
func getFile(url, dst string) error {
httpGetter := &getter.HttpGetter{
Client: cleanhttp.DefaultClient(),
XTerraformGetDisabled: true,
}

goGetterGetters := map[string]getter.Getter{
"file": new(getter.FileGetter),
"gcs": new(getter.GCSGetter),
"git": new(getter.GitGetter),
"hg": new(getter.HgGetter),
"s3": new(getter.S3Getter),
"http": httpGetter,
"https": httpGetter,
}

// https://github.com/hashicorp/go-getter#security-options
getterClient := &getter.Client{
Ctx: context.Background(),
DisableSymlinks: true,
Src: url,
Dst: dst,
Pwd: dst,
Mode: getter.ClientModeAny,
Getters: goGetterGetters,
}

return getterClient.Get()
}

0 comments on commit 3940a38

Please sign in to comment.