package task

import (
	"fmt"
	"os/exec"

	"github.com/juju/errors"
)

const downloadCmd = "wget --tries 20 --waitretry 60 --retry-connrefused --dns-timeout 60 " +
	"--connect-timeout 60 --read-timeout 60 --directory-prefix %s %s"

// DownloadFile will download a url and store it in local filepath.
func DownloadFile(url string, destPath string) error {
	if err := exec.Command("/bin/sh", "-c",
		fmt.Sprintf(downloadCmd, destPath, url)).Run(); err != nil {
		return errors.Annotatef(err, "fail to download %s", url)
	}
	return nil
}
