// command saves pushes and pulls save files from my server package main import ( "bytes" "fmt" "io/ioutil" "log" "os" "os/exec" "os/user" ) const usage = "saves " type link struct { src string dst string } func main() { u, err := user.Current() if err != nil { fmt.Fprintf(os.Stderr, "cannot calculate home dir: %v", err) os.Exit(1) } docs := fmt.Sprintf("%s/Documents/", u.HomeDir) saves := fmt.Sprintf("%s/Documents/saves", u.HomeDir) appSupport := fmt.Sprintf("%s/Library/Application Support", u.HomeDir) if len(os.Args) != 2 { fmt.Fprintf(os.Stderr, "%s\n", usage) os.Exit(1) } var cmd *exec.Cmd switch os.Args[1] { case "pull": cmd = exec.Command("rsync", "-auv", "sj.mcquay.me:~/docs/saves", docs) case "push": cmd = exec.Command("rsync", "-auv", fmt.Sprintf("%ssaves", docs), "sj.mcquay.me:~/docs/") case "link": dirs, err := ioutil.ReadDir(saves) if err != nil { fmt.Fprintf(os.Stderr, "could not read saves dir: %+v\n", err) os.Exit(1) } for _, dir := range dirs { src := fmt.Sprintf("%s/%s", saves, dir.Name()) dst := fmt.Sprintf("%s/%s", appSupport, dir.Name()) if err := os.RemoveAll(dst); err != nil { fmt.Fprintf(os.Stderr, "could not make way for link:%+v\n", err) os.Exit(1) } if err := os.Symlink(src, dst); err != nil { fmt.Fprintf(os.Stderr, "could not link:%+v\n", err) os.Exit(1) } } return default: fmt.Fprintf(os.Stderr, "%s\n", usage) os.Exit(1) } if cmd == nil { fmt.Fprintf(os.Stderr, "failed to populate command\n") os.Exit(1) } var so, se bytes.Buffer cmd.Stdout = &so cmd.Stderr = &se if rc := cmd.Run(); rc != nil { log.Printf("cmd: %+v", cmd) log.Printf("so: %s", so.Bytes()) log.Printf("se: %s", se.Bytes()) log.Printf("rc: %+v", rc) } }