package main import ( "os" "os/exec" "strings" ) // runInDir runs a command with inherited stdio, optionally in dir. func runInDir(dir, name string, args ...string) error { c := exec.Command(name, args...) if dir != "" { c.Dir = dir } c.Stdin = os.Stdin c.Stdout = os.Stdout c.Stderr = os.Stderr return c.Run() } func git(dir string, args ...string) error { return runInDir(dir, "git", args...) } // gitOK runs git and, on failure, prints a red summary. Returns success. func gitOK(dir string, args ...string) bool { if err := git(dir, args...); err != nil { errorln("git " + strings.Join(args, " ") + " failed") return false } return true } func gitCapture(dir string, args ...string) (string, error) { c := exec.Command("git", args...) if dir != "" { c.Dir = dir } out, err := c.Output() return string(out), err } // ssh runs a single remote command over ssh with inherited stdio. func ssh(remote string) error { return runInDir("", "ssh", "-p", cfg.GitPort, cfg.GitUser+"@"+cfg.GitHost, remote) } // sshOK runs a remote command and, on failure, prints a red summary. func sshOK(remote string) bool { if err := ssh(remote); err != nil { errorln("ssh failed: " + remote) return false } return true } // sshOut runs a remote command and returns its stdout split into lines. func sshOut(remote string) ([]string, error) { c := exec.Command("ssh", "-p", cfg.GitPort, cfg.GitUser+"@"+cfg.GitHost, remote) c.Stderr = os.Stderr out, err := c.Output() lines := strings.Split(string(out), "\n") for len(lines) > 0 && lines[len(lines)-1] == "" { lines = lines[:len(lines)-1] } return lines, err } // forwardShell runs a line via /bin/sh -c in the active project directory. func forwardShell(line string) { runInDir(DIR, "/bin/sh", "-c", line) }