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) } } } } }