Skip to content

Commit

Permalink
add method to download remote repo with type specified
Browse files Browse the repository at this point in the history
  • Loading branch information
Yusuf Kanchwala committed Oct 2, 2020
1 parent 53db1a0 commit d834e2e
Showing 1 changed file with 66 additions and 21 deletions.
87 changes: 66 additions & 21 deletions pkg/downloader/download.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package downloader

import (
"fmt"
"path/filepath"
"strings"

Expand Down Expand Up @@ -62,57 +63,72 @@ func NewDownloader() *Downloader {
return &Downloader{}
}

// Download retrieves the package referenced in the given address
// into the installation path and then returns the full path to any subdir
// indicated in the address.
func (d Downloader) Download(addr, instPath string) (string, error) {
// list of errors
var (
ErrEmptyURLType = fmt.Errorf("empty remote url and type")
ErrEmptyURLDest = fmt.Errorf("remote url or destination dir path cannot be empty")
ErrEmptyURLTypeDest = fmt.Errorf("empty remote url or type or desitnation dir path")
)

// Download retrieves the remote repository referenced in the given remoteURL
// into the destination path and then returns the full path to any subdir
// indicated in the URL
func (d Downloader) Download(remoteURL, destPath string) (string, error) {

zap.S().Debugf("download with remote url: %q, destination dir: %q",
remoteURL, destPath)

// validations: remote url or destination dir path cannot be empty
if remoteURL == "" || destPath == "" {
zap.S().Error(ErrEmptyURLDest)
return "", ErrEmptyURLDest
}

// get subDir, if present
packageAddr, subDir := SplitAddrSubdir(addr)
zap.S().Debugf("downloading %q to %q", packageAddr, instPath)
repoURL, subDir := SplitAddrSubdir(remoteURL)
zap.S().Debugf("downloading %q to %q", repoURL, destPath)

// check if a detector is present for the given address
realAddr, err := getter.Detect(packageAddr, instPath, goGetterDetectors)
// check if a detector is present for the given url with type
URLWithType, err := getter.Detect(repoURL, destPath, goGetterDetectors)
if err != nil {
zap.S().Errorf("failed to detect resolved address for %q. error: '%v'", addr, err)
zap.S().Errorf("failed to detect url with type for %q. error: '%v'", remoteURL, err)
return "", err
}
zap.S().Debugf("resolved address: %q; provider address: %q", realAddr, addr)
zap.S().Debugf("remote URL: %q; url with type: %q", remoteURL, URLWithType)

// get actual subDir path
var realSubDir string
realAddr, realSubDir = SplitAddrSubdir(realAddr)
URLWithType, realSubDir := SplitAddrSubdir(URLWithType)
if realSubDir != "" {
subDir = filepath.Join(realSubDir, subDir)
}

if realAddr != packageAddr {
zap.S().Debugf("detector rewrote %q to %q", packageAddr, realAddr)
if URLWithType != repoURL {
zap.S().Debugf("detector rewrote %q to %q", repoURL, URLWithType)
}

// downloading from remote addr
client := getter.Client{
Src: realAddr,
Dst: instPath,
Pwd: instPath,
Src: URLWithType,
Dst: destPath,
Pwd: destPath,
Mode: getter.ClientModeDir,
Detectors: goGetterNoDetectors, // we already did detection above
Decompressors: goGetterDecompressors,
Getters: goGetterGetters,
}
err = client.Get()
if err != nil {
zap.S().Errorf("failed to download %q. error: '%v'", realAddr, err)
zap.S().Errorf("failed to download %q. error: '%v'", URLWithType, err)
return "", err
}

// Our subDir string can contain wildcards until this point, so that
// e.g. a subDir of * can expand to one top-level directory in a .tar.gz
// archive. Now that we've expanded the archive successfully we must
// resolve that into a concrete path.
finalDir := instPath
finalDir := destPath
if subDir != "" {
finalDir, err = getter.SubdirGlob(instPath, subDir)
finalDir, err = getter.SubdirGlob(destPath, subDir)
if err != nil {
zap.S().Errorf("failed to expand %q to %q", subDir, finalDir)
return "", err
Expand All @@ -125,6 +141,35 @@ func (d Downloader) Download(addr, instPath string) (string, error) {
return filepath.Clean(finalDir), nil
}

// DownloadWithType retrieves the remote repository referenced in the
// given remoteURL into the installation path and then returns the full path
// to any subdir indicated in the remoteURL
//
// DownloadWithType enforces download type on go-getter to get rid of any
// ambiguities in remoteURL
func (d Downloader) DownloadWithType(remoteType, remoteURL, destPath string) (string, error) {

zap.S().Debugf("download with remote type: %q, remote URL: %q, destination dir: %q",
remoteType, remoteURL, destPath)

// validations
// remoteURL and repoType cannot be empty
if remoteURL == "" && remoteType == "" {
zap.S().Error(ErrEmptyURLType)
return "", ErrEmptyURLType
}

// remoteURL, remoteType, destination path cannot be empty
if remoteURL == "" || remoteType == "" || destPath == "" {
zap.S().Error(ErrEmptyURLTypeDest)
return "", ErrEmptyURLDest
}
URLWithType := fmt.Sprintf("%s::%s", remoteType, remoteURL)

// Download
return d.Download(URLWithType, destPath)
}

// SplitAddrSubdir splits the given address (which is assumed to be a
// registry address or go-getter-style address) into a package portion
// and a sub-directory portion.
Expand All @@ -136,7 +181,7 @@ func (d Downloader) Download(addr, instPath string) (string, error) {
//
// The subDir portion will be returned as empty if no subdir separator
// ("//") is present in the address.
func SplitAddrSubdir(addr string) (packageAddr, subDir string) {
func SplitAddrSubdir(addr string) (repoURL, subDir string) {
return getter.SourceDirSubdir(addr)
}

Expand Down

0 comments on commit d834e2e

Please sign in to comment.