diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-01-03 18:04:08 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-01-03 20:24:29 +0200 |
commit | 23a7f3baa33265519840609dc54e950615ec39b1 (patch) | |
tree | ff2737f76b63a2acf5f8a9fffd5c15e3eb4c46c8 /tools/updater/download.go | |
parent | WebProfile refactoring (diff) | |
download | smolbote-23a7f3baa33265519840609dc54e950615ec39b1.tar.xz |
Merge some QoL improvements from staging branch
- Build executable in top-level buildroot
- Use meson sourceset
- Pull in poi-crash and poi-update from staging
- Remove extraneous scripts in tools/
- Pull in configure scripts in scripts/
Diffstat (limited to 'tools/updater/download.go')
-rw-r--r-- | tools/updater/download.go | 88 |
1 files changed, 0 insertions, 88 deletions
diff --git a/tools/updater/download.go b/tools/updater/download.go deleted file mode 100644 index 8c9de9a..0000000 --- a/tools/updater/download.go +++ /dev/null @@ -1,88 +0,0 @@ -package main - -import ( - "fmt" - "io" - "net/http" - "os" - "path/filepath" - "strings" -) - -func Min(x, y int64) int64 { - if x < y { - return x - } - return y -} - -func Max(x, y int64) int64 { - if x > y { - return x - } - return y -} - -// WriteCounter counts the number of bytes written to it -type WriteCounter struct { - ContentLength int64 - BytesWritten uint64 - - Step int64 -} - -func NewWriteCounter(contentLength int64) *WriteCounter { - ptr := new(WriteCounter) - ptr.ContentLength = contentLength - ptr.Step = Max(contentLength/20, 1) - - return ptr -} - -func (wc *WriteCounter) Write(p []byte) (int, error) { - n := len(p) - wc.BytesWritten += 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)) - - // steps can be at most 20 -> get the minimum of (n_steps) and 20 - steps := int(Min(int64(wc.BytesWritten)/wc.Step, 20)) - - fmt.Printf("\r[%s%s] %d/%d bytes complete", strings.Repeat("#", steps), strings.Repeat(" ", 20-steps), wc.BytesWritten, wc.ContentLength) -} - -func downloadFile(path string, url string) error { - // make sure dir exists - os.MkdirAll(filepath.Dir(path), 0755) - - // 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 := NewWriteCounter(response.ContentLength) - _, err = io.Copy(output, io.TeeReader(response.Body, counter)) - if err != nil { - return err - } - - fmt.Printf("\n") - output.Close() - return os.Rename(path+".part", path) -} |