aboutsummaryrefslogtreecommitdiff
path: root/tools/updater/main.go
blob: deaad871bcd6744e382edfa4d6f3dacaabab8011 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main

import (
	"bufio"
	"flag"
	"fmt"
	"net/http"
	"runtime"
	"strings"
)

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 {
		panic(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 {
			panic(err)
		} else {
			if *verboseFlag {
				fmt.Printf("[%s]: %t\n", filepath, same)
			}

			if !same && !*dryRunFlag {
				downloadFile(filepath, repoPath+filepath)
			}
		}
	}

}