From 58122b41d2fc536e5f2ad30da70e33e59a18dc90 Mon Sep 17 00:00:00 2001 From: Andinus Date: Thu, 19 Mar 2020 13:22:57 +0530 Subject: Add download function --- pkg/background/background.go | 13 ------------- pkg/background/download.go | 34 ++++++++++++++++++++++++++++++++++ pkg/background/set.go | 13 +++++++++++++ 3 files changed, 47 insertions(+), 13 deletions(-) delete mode 100644 pkg/background/background.go create mode 100644 pkg/background/download.go create mode 100644 pkg/background/set.go diff --git a/pkg/background/background.go b/pkg/background/background.go deleted file mode 100644 index fa95f6f..0000000 --- a/pkg/background/background.go +++ /dev/null @@ -1,13 +0,0 @@ -package background - -import "os/exec" - -// Set calls feh to set the background -func Set(path string) error { - feh, err := exec.LookPath("feh") - if err != nil { - return err - } - err = exec.Command(feh, "--bg-fill", path).Run() - return err -} 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/set.go b/pkg/background/set.go new file mode 100644 index 0000000..fa95f6f --- /dev/null +++ b/pkg/background/set.go @@ -0,0 +1,13 @@ +package background + +import "os/exec" + +// Set calls feh to set the background +func Set(path string) error { + feh, err := exec.LookPath("feh") + if err != nil { + return err + } + err = exec.Command(feh, "--bg-fill", path).Run() + return err +} -- cgit 1.4.1-2-gfad0