diff options
Diffstat (limited to 'tools/updater')
-rw-r--r-- | tools/updater/main.go | 83 | ||||
-rw-r--r-- | tools/updater/updater.go | 145 |
2 files changed, 83 insertions, 145 deletions
diff --git a/tools/updater/main.go b/tools/updater/main.go new file mode 100644 index 0000000..e5e377c --- /dev/null +++ b/tools/updater/main.go @@ -0,0 +1,83 @@ +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, err error) { + cmd := exec.Command(executable, "--version") + + // wait for complete + err = cmd.Wait() + + // return output + output, err := cmd.Output() + v := strings.Split(string(output), "-") + version = v[len(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 --version") + poi_version, err := poi(*execFlag) + if err != nil { + fmt.Println("error:", err) + } + fmt.Println(poi_version) + + // get branchmap + fmt.Println("Getting branchmap...") + if *verboseFlag { + fmt.Println("for repository", *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:len(poi_version)-1]) + } + +} diff --git a/tools/updater/updater.go b/tools/updater/updater.go deleted file mode 100644 index 07be93a..0000000 --- a/tools/updater/updater.go +++ /dev/null @@ -1,145 +0,0 @@ -package main - -import ( - "fmt" - "strings" - "flag" - "errors" - "net/http" - "os" - "io" - "io/ioutil" - "encoding/json" -) - -type VersionMeta struct { - Poi VersionData `json:"poi"` -} - -type VersionData struct { - Version string `json:"version"` - Patch int `json:"patch"` - Commit string `json:"commit"` -} - -func parseJson(name string) (ver VersionMeta, err error) { - var reader io.Reader - - if strings.HasPrefix(name, "https://") { - // Grabbing stuff over https - resp, e := http.Get(name) - if resp.StatusCode != 200 { - e = errors.New("HTTP response not OK") - } - if e != nil { - fmt.Println("| Error encountered getting version.json: ", e) - } - defer resp.Body.Close() - reader = resp.Body - err = e - - } else { - // FIXME Assuming we're grabbing a local file - r, e := os.Open(name) - if e != nil { - fmt.Println("| Error opening file: ", e) - } - defer r.Close() - reader = r - err = e - } - - if err == nil { - body, err := ioutil.ReadAll(reader) - if err != nil { - fmt.Println("| Error encountered reading body") - } - - err = json.Unmarshal(body, &ver) - } - - return -} - -// left > right r -> 1 -// left = right r -> 0 -// left < right r -> -1 -func compareVersion(left, right string) (ret int) { - if strings.Compare(left, right) == 0 { - ret = 0 - return - } - - l := strings.Split(left, ".") - r := strings.Split(right, ".") - - for i := 0; i < 3; i++ { - if l[i] > r[i] { - ret = 1 - break - } else if l[i] < r[i] { - ret = -1 - break - } - } - - return -} - -func main() { - fmt.Println("[ smolbote updater ]") - - // read local version.json - localFlag := flag.Bool("local", true, "Read local version.json") - // read remote version.json - remoteFlag := flag.Bool("remote", true, "Read remote version.json") - // download update - downloadFlag := flag.Bool("download", false, "Download update") - // install update - installFlag := flag.Bool("install", false, "Install update") - - flag.Parse() - - var local VersionMeta - var remote VersionMeta - var err error - - if *localFlag { - fmt.Println("+ Reading local version.json") - local, err = parseJson("version.json") - if err == nil { - fmt.Println(fmt.Sprintf("| Got poi [%s-%d-%s]", local.Poi.Version, local.Poi.Patch, local.Poi.Commit)) - } else { - // there has been an error reading the local version.json - *localFlag = false - } - fmt.Println("\\ done") - } - - if *remoteFlag { - fmt.Println("+ Reading remote version.json") - remote, err = parseJson("https://neueland.iserlohn-fortress.net/smolbote/version") - if err == nil { - fmt.Println(fmt.Sprintf("| Got poi [%s-%d-%s]", remote.Poi.Version, remote.Poi.Patch, remote.Poi.Commit)) - } - - comp := compareVersion(local.Poi.Version, remote.Poi.Version) - if comp >= 0 { - fmt.Println("| poi up to poi") - } else { // comp < 0 - fmt.Println("| poi out of poi") - } - - fmt.Println("\\ done") - } - - if *downloadFlag { - fmt.Println("+ Downloading update") - fmt.Println("\\ done") - } - - if *installFlag { - fmt.Println("+ Installing update") - fmt.Println("\\ done") - } -} |