aboutsummaryrefslogtreecommitdiff
path: root/tools/updater/download.go
diff options
context:
space:
mode:
Diffstat (limited to 'tools/updater/download.go')
-rw-r--r--tools/updater/download.go62
1 files changed, 62 insertions, 0 deletions
diff --git a/tools/updater/download.go b/tools/updater/download.go
new file mode 100644
index 0000000..b89703b
--- /dev/null
+++ b/tools/updater/download.go
@@ -0,0 +1,62 @@
+package main
+
+import (
+ "fmt"
+ "strings"
+ "os"
+ "io"
+ "net/http"
+ "path/filepath"
+)
+
+// WriteCounter counts the number of bytes written to it
+type WriteCounter struct {
+ Filepath string
+ Total uint64
+}
+
+func (wc *WriteCounter) Write(p []byte) (int, error) {
+ n := len(p)
+ wc.Total += uint64(n)
+ wc.PrintProgress()
+ return n, nil
+}
+
+func (wc *WriteCounter) PrintProgress() {
+ // clear the line by using a character return to go back to the start of the
+ // line and remove the remaining characters
+ fmt.Printf("\r%s", strings.Repeat(" ", 35))
+ fmt.Printf("\r[%s]... %d bytes complete", wc.Filepath, wc.Total)
+}
+
+func downloadFile(path string, url string) error {
+ // make sure dir exists
+ os.MkdirAll(filepath.Dir(path), 0777)
+
+ // create .part file
+ output, err := os.Create(path + ".part")
+ if err != nil {
+ return err
+ }
+ defer output.Close()
+
+ // get data
+ response, err := http.Get(url)
+ if err != nil {
+ return err
+ }
+ defer response.Body.Close()
+
+ // create progress reporter
+ counter := &WriteCounter{path, 0}
+ _, err = io.Copy(output, io.TeeReader(response.Body, counter))
+ if err != nil {
+ return err
+ }
+
+ fmt.Printf("\n")
+ //fmt.Printf("\r%s", strings.Repeat(" ", 40))
+ os.Rename(path + ".part", path)
+ //fmt.Printf("\r[%s] complete\n", path)
+ return nil
+}