func main() { flag.Parse() err := os.Chdir(*dataFolder) if err != nil { cmdln.PrintStatus(-1, err.Error()) os.Exit(1) } generateBinHash() cmdline, exit := cmdln.NewCommandline("RES > ") cmdline.PopulateDefaults() cmdline.AddCommand("hash", "access binary-hash functions", hashFunction) go cmdline.ListenAndServe() /* exit := make(chan bool) cmds := cmdln.NewCommandMap() cmds.AddCommand("hash", "access binary-hash functions", hashFunction) cmdline := cmdln.New(cmdln.PROMPT, cmds, exit) cmdline.SetPrompt("RES >") cmdline.PopulateDefaults() go cmdline.ListenAndServe() */ go listenWithBreak(supplyHash, exit) select { case <-exit: } }
func generateBinHash() { bytes, err := fileToByteSlice(*binFileName) if err != nil { cmdln.PrintStatus(cmdln.ERROR, err.Error()) os.Exit(1) } binHash = hash(bytes) }
func cd(folder *string) error { err := os.Chdir(*folder) if err != nil { os.Mkdir(*folder, os.ModePerm) cmdln.PrintStatus(cmdln.FAULT, "Attempting to create "+*folder) return os.Chdir(*folder) } return nil }
func listenWithBreak(handleFn func(net.Conn), brk chan bool) { addr, err := net.ResolveTCPAddr("tcp", *remoteAddr) if err != nil { cmdln.PrintStatus(cmdln.ERROR, err.Error()) brk <- true } ln, err := net.ListenTCP("tcp", addr) if err != nil { cmdln.PrintStatus(cmdln.ERROR, err.Error()) brk <- true } for { conn, err := ln.AcceptTCP() if err != nil { // handle error continue } hashRequests++ go handleFn(conn) } }
func main() { flag.Parse() fmt.Println(NAME) fmt.Printf("Version: %s\n", VERSION) err := cd(dataFolder) if err != nil { cmdln.PrintStatus(cmdln.ERROR, err.Error()) return } cmdln.PrintStatus(cmdln.SUCCESS, "~/.metaparticle found.") conf, err := getConfMap(*confName) if err != nil { cmdln.PrintStatus(cmdln.ERROR, err.Error()) return } different, err := updateExists(conf["host"], conf["bin"]) if err != nil { cmdln.PrintStatus(cmdln.ERROR, err.Error()) return } if different { cmdln.PrintStatus(cmdln.FAULT, "New version available.") if !cmdln.AskYN("Do you want to download the latest version now?") { cmdln.PrintStatus(cmdln.ERROR, "To play, you must have the latest version.") return } err := updateFile(conf["host"], conf["bin"]) if err != nil { cmdln.PrintStatus(cmdln.ERROR, err.Error()) return } } cmdln.PrintStatus(cmdln.SUCCESS, conf["bin"]+" is up to date.") cmd := exec.Command(conf["bin"]) cmd.Run() }
func printHashReqs() { cmdln.PrintStatus(hashRequests, fmt.Sprintf("So far %d requests.", hashRequests)) }