From 92debfd3a3f9cbf62a986953ef19e023565d5907 Mon Sep 17 00:00:00 2001 From: stephen mcquay Date: Sat, 9 Jan 2016 11:06:51 -0800 Subject: [PATCH] added support for making symlinks the assumption here is that every file in the ~/Documents/saves directory should exist as a symlink into ~/Library/Application Support/. --- main.go | 40 ++++++++++++++++++++++++++++++++++++---- readme.md | 2 +- 2 files changed, 37 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 9d36997..2209cae 100644 --- a/main.go +++ b/main.go @@ -1,16 +1,22 @@ -// command sk pushes and pulls save files from my server +// 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 = "sk " +const usage = "saves " + +type link struct { + src string + dst string +} func main() { u, err := user.Current() @@ -19,6 +25,8 @@ func main() { 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) @@ -27,14 +35,38 @@ func main() { var cmd *exec.Cmd switch os.Args[1] { case "pull": - cmd = exec.Command("rsync", "-auv", "sj.mcquay.me:~/docs/ycg", docs) + cmd = exec.Command("rsync", "-auv", "sj.mcquay.me:~/docs/saves", docs) case "push": - cmd = exec.Command("rsync", "-auv", fmt.Sprintf("%sycg", docs), "sj.mcquay.me:~/docs/") + 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 diff --git a/readme.md b/readme.md index d8bc4d5..c8c35ae 100644 --- a/readme.md +++ b/readme.md @@ -1,3 +1,3 @@ -# sk +# saves push and pull save files from my server