From a3d5c49e061d7b022271ea2be374e929ff385c60 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 11 Sep 2018 02:06:31 +0200 Subject: Update golang updater tool --- tools/updater/main.go | 133 ++++++++++++++++++++------------------------------ 1 file changed, 54 insertions(+), 79 deletions(-) (limited to 'tools/updater/main.go') diff --git a/tools/updater/main.go b/tools/updater/main.go index eda229b..f4feb62 100644 --- a/tools/updater/main.go +++ b/tools/updater/main.go @@ -1,88 +1,63 @@ package main import ( - "flag" - "fmt" - "io/ioutil" - "net/http" - "os" - "os/exec" - "strings" + "flag" + "fmt" + "os" + "net/http" + "bufio" + "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) - } + 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") + //dryRunFlag := flag.Bool("dry-run", false, "Dry run") + + flag.Parse() + + // help flag --> show usage and exit + if *helpFlag { + fmt.Println("Usage:") + flag.PrintDefaults() + os.Exit(0) + } + + manifestPath := *repoFlag + "/windows-sha512.txt" + repoPath := *repoFlag + "/windows/" + + if *verboseFlag { + fmt.Println("repoFlag =", *repoFlag) + fmt.Println("manifest =", manifestPath) + fmt.Println("repoPath =", repoPath) + } + + response, err := http.Get(manifestPath) + if err != nil { + panic(err) + } + defer response.Body.Close() + + scanner := bufio.NewScanner(response.Body) + for scanner.Scan() { + s := strings.Split(scanner.Text(), " ") + + filepath := s[1] + checksum := s[0] + + state, err := checkFile(filepath, checksum) + if err != nil { + panic(err) + } + + if(state) { + fmt.Printf("[%s] passed\n", filepath) + } else { + fmt.Printf("[%s] missing or mismatching\n", filepath) + downloadFile(filepath, repoPath + filepath) + } + } - for branch, commit := range branches { - fmt.Println("-", branch, ":", commit[0:12]) - } } -- cgit v1.2.1