diff options
author | Andinus <andinus@nand.sh> | 2020-03-19 13:22:57 +0530 |
---|---|---|
committer | Andinus <andinus@nand.sh> | 2020-03-19 13:22:57 +0530 |
commit | 58122b41d2fc536e5f2ad30da70e33e59a18dc90 (patch) | |
tree | 81fd113d40b670c97b5ae85e5f52a91ad7d8a004 /pkg/background | |
parent | 5e6090c35f3d6105b20e529cc23c76a339ba944f (diff) | |
download | cetus-58122b41d2fc536e5f2ad30da70e33e59a18dc90.tar.gz |
Add download function
Diffstat (limited to 'pkg/background')
-rw-r--r-- | pkg/background/download.go | 34 | ||||
-rw-r--r-- | pkg/background/set.go (renamed from pkg/background/background.go) | 0 |
2 files changed, 34 insertions, 0 deletions
diff --git a/pkg/background/download.go b/pkg/background/download.go new file mode 100644 index 0000000..fd391a6 --- /dev/null +++ b/pkg/background/download.go @@ -0,0 +1,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) (err 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 +} diff --git a/pkg/background/background.go b/pkg/background/set.go index fa95f6f..fa95f6f 100644 --- a/pkg/background/background.go +++ b/pkg/background/set.go |