added list subcommand

also cleaned up stray code. Reorganized code to remove awkward handling of
push/pull at bottom.
This commit is contained in:
Stephen McQuay 2016-01-10 09:23:22 -08:00
parent 0f2b6d7987
commit 429eb02d64
1 changed files with 41 additions and 30 deletions

71
main.go
View File

@ -11,14 +11,14 @@ import (
"os/user"
)
const usage = "saves <push|pull|link>"
type link struct {
src string
dst string
}
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)
@ -27,18 +27,30 @@ func main() {
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", fmt.Sprintf("%s/", docs))
case "push":
cmd = exec.Command("rsync", "-auv", fmt.Sprintf("%s/saves", docs), "sj.mcquay.me:~/docs/")
case "link":
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)
@ -57,23 +69,22 @@ func main() {
os.Exit(1)
}
}
return
case "list", "ls":
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())
}
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)
}
}