aboutsummaryrefslogtreecommitdiff
path: root/tools/updater
diff options
context:
space:
mode:
Diffstat (limited to 'tools/updater')
-rw-r--r--tools/updater/download.go88
-rw-r--r--tools/updater/main.go77
-rw-r--r--tools/updater/manifest.go41
3 files changed, 0 insertions, 206 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)
-}
diff --git a/tools/updater/main.go b/tools/updater/main.go
deleted file mode 100644
index baef1f1..0000000
--- a/tools/updater/main.go
+++ /dev/null
@@ -1,77 +0,0 @@
-package main
-
-import (
- "bufio"
- "flag"
- "fmt"
- "net/http"
- "os"
- "runtime"
- "strings"
-)
-
-func fail(err error) {
- fmt.Printf("An error has occurred:\n%s\n", err.Error())
- if runtime.GOOS == "windows" {
- fmt.Print("Press 'Enter' to continue...")
- fmt.Scanln()
- }
- os.Exit(-1)
-}
-
-func main() {
- helpFlag := flag.Bool("help", false, "Show help information")
- verboseFlag := flag.Bool("verbose", false, "Print more information")
- repoFlag := flag.String("repo", "https://neueland.iserlohn-fortress.net/smolbote/downloads", "Repository path")
- platformFlag := flag.String("platform", runtime.GOOS, "Platform")
- dryRunFlag := flag.Bool("dry-run", false, "Dry run: only check files, do not download")
-
- flag.Parse()
-
- manifestPath := fmt.Sprintf("%s/%s-sha512.txt", *repoFlag, *platformFlag)
- repoPath := fmt.Sprintf("%s/%s/", *repoFlag, *platformFlag)
-
- // help flag --> show usage and exit
- if *helpFlag {
- fmt.Println("Usage:")
- flag.PrintDefaults()
-
- fmt.Println("Paths:")
- fmt.Println(" manifest ", manifestPath)
- fmt.Println(" repository ", repoPath)
- return
- }
-
- response, err := http.Get(manifestPath)
- if err != nil {
- fail(err)
- } else if response.StatusCode != 200 {
- fmt.Printf("Could not get manifest: %s\n", response.Status)
- return
- }
- defer response.Body.Close()
-
- // read through manifest
- scanner := bufio.NewScanner(response.Body)
- for scanner.Scan() {
- s := strings.Split(scanner.Text(), " ")
-
- filepath := s[1]
- checksum := s[0]
-
- if same, err := checkFile(filepath, checksum); err != nil {
- fail(err)
- } else {
- if *verboseFlag {
- fmt.Printf("[%s]: %t\n", filepath, same)
- }
-
- if !same && !*dryRunFlag {
- if err := downloadFile(filepath, repoPath+filepath); err != nil {
- fail(err)
- }
- }
- }
- }
-
-}
diff --git a/tools/updater/manifest.go b/tools/updater/manifest.go
deleted file mode 100644
index 0d8bfae..0000000
--- a/tools/updater/manifest.go
+++ /dev/null
@@ -1,41 +0,0 @@
-package main
-
-import (
- "crypto/sha512"
- "fmt"
- "io"
- "os"
-)
-
-func checkFile(filepath string, checksum string) (bool, error) {
- if _, err := os.Stat(filepath); os.IsNotExist(err) {
- return false, nil
- }
-
- // file exists, check checksum
- lsum, err := hash(filepath)
- if err != nil {
- return false, err
- }
-
- if checksum != fmt.Sprintf("%x", lsum) {
- return false, nil
- }
-
- return true, nil
-}
-
-func hash(filepath string) ([]byte, error) {
- file, err := os.Open(filepath)
- if err != nil {
- return nil, err
- }
- defer file.Close()
-
- hasher := sha512.New()
- if _, err := io.Copy(hasher, file); err != nil {
- return nil, err
- }
-
- return hasher.Sum(nil), nil
-}