summary refs log blame commit diff stats
path: root/pkg/background/download.go
blob: 78ff135192a91f1c43d700ddea76cbb33a997cc1 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11










                                                                   
                                              





















                                                                        
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
}