aboutsummaryrefslogtreecommitdiff
path: root/tools/updater/main.go
blob: baef1f1697ecdcb57e5c48e099e602ae74ae6808 (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
66
67
68
69
70
71
72
73
74
75
76
77
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)
				}
			}
		}
	}

}