4 Commits
0.1.0 ... 0.2.0

View File

@ -23,6 +23,7 @@ type client struct {
ReadTimeout time.Duration ReadTimeout time.Duration
Retries int Retries int
RetryWait time.Duration RetryWait time.Duration
NoProxy bool
} }
func main() { func main() {
@ -31,13 +32,14 @@ func main() {
var showVersion bool var showVersion bool
flag.StringVar(&c.User, "http-user", "", "HTTP auth username") flag.StringVar(&c.User, "http-user", "", "HTTP auth username")
flag.StringVar(&c.Password, "http-password", "", "HTTP auth password") flag.StringVar(&c.Password, "http-passwd", "", "HTTP auth password")
flag.IntVar(&c.Retries, "retries", 1, "Number of retries in case of download errors.") flag.IntVar(&c.Retries, "retries", 1, "Number of retries in case of download errors.")
flag.DurationVar(&c.RetryWait, "retry-wait", 2*time.Second, "Time to wait before retrying download.") flag.DurationVar(&c.RetryWait, "retry-wait", 2*time.Second, "Time to wait before retrying download.")
flag.DurationVar(&c.ConnectTimeout, "connect-timeout", 5*time.Second, "TCP connections that take longer to establish will be aborted") flag.DurationVar(&c.ConnectTimeout, "connect-timeout", 5*time.Second, "TCP connections that take longer to establish will be aborted")
flag.DurationVar(&c.ReadTimeout, "read-timeout", 10*time.Second, "TCP connections that that are longer idle will be aborted") flag.DurationVar(&c.ReadTimeout, "read-timeout", 10*time.Second, "TCP connections that that are longer idle will be aborted")
flag.BoolVar(&c.Insecure, "insecure", false, "Disable TLS certificate verification.") flag.BoolVar(&c.Insecure, "insecure", false, "Disable TLS certificate verification.")
flag.BoolVar(&showVersion, "version", false, "Show version and exit.") flag.BoolVar(&showVersion, "version", false, "Show version and exit.")
flag.BoolVar(&c.NoProxy, "no-proxy", false, "Ignore *_proxy environment variables.")
flag.StringVar(&filename, "filename", "", "Filename / path where to write downloaded file to. Defaults to filename of the URL.") flag.StringVar(&filename, "filename", "", "Filename / path where to write downloaded file to. Defaults to filename of the URL.")
flag.Parse() flag.Parse()
@ -69,12 +71,15 @@ func main() {
} }
} }
if err != nil { if err != nil {
if err := os.Remove(filename); err != nil {
println(err)
}
os.Exit(1) os.Exit(1)
} }
} }
func (c *client) DownloadFile(filename string, url string) (err error) { func (c *client) DownloadFile(filename string, url string) (err error) {
println("Downloading %s to file %s\n", url, filename) fmt.Fprintf(os.Stderr, "downloading %s to file %s\n", url, filename)
// Create the target file // Create the target file
out, err := os.Create(filename) out, err := os.Create(filename)
if err != nil { if err != nil {
@ -95,10 +100,21 @@ func (c *client) DownloadFile(filename string, url string) (err error) {
}, },
} }
if c.NoProxy {
tr.Proxy = nil
} else {
tr.Proxy = http.ProxyFromEnvironment
}
client := &http.Client{Transport: tr} client := &http.Client{Transport: tr}
// Get the data // Get the data
resp, err := client.Get(url) req, err := http.NewRequest(http.MethodGet, url, nil)
if err != nil {
return err
}
req.SetBasicAuth(c.User, c.Password)
resp, err := client.Do(req)
if err != nil { if err != nil {
return err return err
} }