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/.
This commit is contained in:
Stephen McQuay 2016-01-09 11:06:51 -08:00
parent f90e033b71
commit 92debfd3a3
2 changed files with 37 additions and 5 deletions

40
main.go
View File

@ -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 package main
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"os/exec" "os/exec"
"os/user" "os/user"
) )
const usage = "sk <push|pull>" const usage = "saves <push|pull|link>"
type link struct {
src string
dst string
}
func main() { func main() {
u, err := user.Current() u, err := user.Current()
@ -19,6 +25,8 @@ func main() {
os.Exit(1) os.Exit(1)
} }
docs := fmt.Sprintf("%s/Documents/", u.HomeDir) 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 { if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "%s\n", usage) fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1) os.Exit(1)
@ -27,14 +35,38 @@ func main() {
var cmd *exec.Cmd var cmd *exec.Cmd
switch os.Args[1] { switch os.Args[1] {
case "pull": 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": 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: default:
fmt.Fprintf(os.Stderr, "%s\n", usage) fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1) os.Exit(1)
} }
if cmd == nil {
fmt.Fprintf(os.Stderr, "failed to populate command\n")
os.Exit(1)
}
var so, se bytes.Buffer var so, se bytes.Buffer
cmd.Stdout = &so cmd.Stdout = &so
cmd.Stderr = &se cmd.Stderr = &se

View File

@ -1,3 +1,3 @@
# sk # saves
push and pull save files from my server push and pull save files from my server