summary refs log tree commit diff stats
path: root/pkg/background/download.go
blob: 78ff135192a91f1c43d700ddea76cbb33a997cc1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package background

import (
	"fmt"
	"io"
	"net/http"
	"os"
)

// Download takes path and url as input and downloads the data to a
// file, returning an error if there is one
func Download(file string, url string) error {
	o, err := os.Create(file)
	if err != nil {
		return err
	}
	defer o.Close()

	res, err := http.Get(url)
	if err != nil {
		return err
	}
	defer res.Body.Close()

	if res.StatusCode != http.StatusOK {
		return fmt.Errorf("Unexpected Response: %s", res.Status)
	}

	_, err = io.Copy(o, res.Body)
	if err != nil {
		return err
	}
	return nil
}