in httputil/httputil.go [172:234]
func DownloadBinary(originURL, destDir, destFile string) (string, error) {
err := os.MkdirAll(destDir, 0755)
if err != nil {
return "", fmt.Errorf("could not create directory %s: %v", destDir, err)
}
destinationPath := filepath.Join(destDir, destFile)
if _, err := os.Stat(destinationPath); err != nil {
tmpfile, err := ioutil.TempFile(destDir, "download")
if err != nil {
return "", fmt.Errorf("could not create temporary file: %v", err)
}
defer func() {
err := tmpfile.Close()
if err == nil {
os.Remove(tmpfile.Name())
}
}()
u, err := url.Parse(originURL)
if err != nil {
// originURL supposed to be valid
return "", err
}
log.Printf("Downloading %s...", originURL)
var auth string = ""
t, err := tryFindNetrcFileCreds(u.Host)
if err == nil {
// successfully parsed netrc for given host
auth = t
}
resp, err := get(originURL, auth)
if err != nil {
return "", fmt.Errorf("HTTP GET %s failed: %v", originURL, err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("HTTP GET %s failed with error %v", originURL, resp.StatusCode)
}
_, err = io.Copy(tmpfile, resp.Body)
if err != nil {
return "", fmt.Errorf("could not copy from %s to %s: %v", originURL, tmpfile.Name(), err)
}
err = os.Chmod(tmpfile.Name(), 0755)
if err != nil {
return "", fmt.Errorf("could not chmod file %s: %v", tmpfile.Name(), err)
}
tmpfile.Close()
err = os.Rename(tmpfile.Name(), destinationPath)
if err != nil {
return "", fmt.Errorf("could not move %s to %s: %v", tmpfile.Name(), destinationPath, err)
}
}
return destinationPath, nil
}