aboutsummaryrefslogtreecommitdiff
path: root/tools/updater/main.go
blob: eda229b1908994e28fb8d99119b94e6df417ccc0 (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
78
79
80
81
82
83
84
85
86
87
88
package main

import (
	"flag"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"
	"strings"
)

// get branchmap of mercurial repository served over http
func branchmap(repository string) (branches map[string]string, err error) {
	resp, err := http.Get(repository + "/?cmd=branchmap")

	if err != nil {
		return
	}

	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)

	branches = make(map[string]string)
	for _, line := range strings.Split(string(body), "\n") {
		a := strings.Split(line, " ")
		branches[a[0]] = a[1]
	}

	return
}

func poi(executable string) (version string, commit string, err error) {
	cmd := exec.Command(executable, "--build")

	// wait for complete
	err = cmd.Wait()

	// return output
	output, err := cmd.Output()
	v := strings.Split(string(output), ":")
	version = v[0]
	commit = v[1]
	return
}

func main() {
	helpFlag := flag.Bool("help", false, "Show help information")
	verboseFlag := flag.Bool("verbose", false, "Print more information")
	execFlag := flag.String("exec", "poi", "Executable path")
	repoFlag := flag.String("repo", "https://neueland.iserlohn-fortress.net/smolbote.hg", "Repository path")
	flag.Parse()

	// help flag --> show usage and exit
	if *helpFlag {
		fmt.Println("Usage:")
		flag.PrintDefaults()
		os.Exit(0)
	}

	fmt.Println("Getting poi build...")
	if *verboseFlag {
		fmt.Println("[exec =", *execFlag, "]")
	}

	poi_branch, poi_commit, err := poi(*execFlag)
	if err != nil {
		fmt.Println("error:", err)
	}
	fmt.Println("-", poi_branch, ":", poi_commit)

	// get branchmap
	fmt.Println("Getting branchmap...")
	if *verboseFlag {
		fmt.Println("[repo =", *repoFlag, "]")
	}

	branches, err := branchmap(*repoFlag)
	if err != nil {
		fmt.Println("Error getting branchmap:", err)
		os.Exit(1)
	}

	for branch, commit := range branches {
		fmt.Println("-", branch, ":", commit[0:12])
	}

}