tool to sync save states for my games
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.5 KiB

// 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 <list|push|pull|link>"
func main() {
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1)
}
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)
var cmd *exec.Cmd
switch os.Args[1] {
case "pull", "push":
if os.Args[1] == "push" {
cmd = exec.Command("rsync", "-auv", "sj.mcquay.me:~/docs/saves", fmt.Sprintf("%s/", docs))
} else {
cmd = exec.Command("rsync", "-auv", fmt.Sprintf("%s/saves", docs), "sj.mcquay.me:~/docs/")
}
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)
}
case "link", "ln":
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)
}
}
case "list", "ls":
known := map[string]bool{}
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 {
status := "l"
dst := fmt.Sprintf("%s/%s", appSupport, dir.Name())
if _, err := os.Lstat(dst); os.IsNotExist(err) {
status = "m"
}
fmt.Printf("%s %s\n", status, dir.Name())
known[dir.Name()] = true
}
dirs, err = ioutil.ReadDir(appSupport)
if err != nil {
fmt.Fprintf(os.Stderr, "could not read saves dir: %+v\n", err)
os.Exit(1)
}
for _, dir := range dirs {
if dir.Mode()&os.ModeSymlink == os.ModeSymlink {
if _, ok := known[dir.Name()]; !ok {
fmt.Fprintf(os.Stderr, "? %s\n", dir.Name())
}
}
}
default:
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1)
}
}