grim/hgkeeper

Parents f3041eb60173
Children 803e8c53ff13
rework the ssh commands so there have a setup and teardown functions
--- a/hg/hg.go Mon Sep 09 23:11:01 2019 -0500
+++ b/hg/hg.go Tue Sep 10 01:37:08 2019 -0500
@@ -5,34 +5,46 @@
"os/exec"
)
-func common(cmd *exec.Cmd) *exec.Cmd {
- cmd.Env = append(cmd.Env, os.Environ()...)
- cmd.Env = append(cmd.Env, "HGRCPATH=/dev/null")
-
- return cmd
+type Command struct {
+ cmd *exec.Cmd
+ tmpHgrc string
+ writeable bool
}
-func Init(path string) *exec.Cmd {
- return common(exec.Command("hg", "init", path))
+func (c *Command) Setup() {
+ c.cmd.Env = append(os.Environ(), "HGRCPATH=/dev/null")
+}
+
+func (c *Command) Teardown() {
+
+}
+
+func (c *Command) Cmd() *exec.Cmd {
+ return c.cmd
}
-func Serve(path string, writeable bool) *exec.Cmd {
- args := []string{
- "-R", path,
- "serve",
- "--stdio",
+func Init(path string) *Command {
+ return &Command{
+ cmd: exec.Command("hg", "init", path),
}
-
- return common(exec.Command("hg", args...))
}
-func Add(path string, files ...string) *exec.Cmd {
+func Serve(path string, writeable bool) *Command {
+ return &Command{
+ cmd: exec.Command("hg", "-R", path, "serve", "--stdio"),
+ writeable: writeable,
+ }
+}
+
+func Add(path string, files ...string) *Command {
args := append([]string{"add", "--cwd", path}, files...)
- return common(exec.Command("hg", args...))
+ return &Command{
+ cmd: exec.Command("hg", args...),
+ }
}
-func Commit(path, username, message string) *exec.Cmd {
+func Commit(path, username, message string) *Command {
args := []string{
"commit",
"--cwd", path,
@@ -40,5 +52,7 @@
"-m", message,
}
- return common(exec.Command("hg", args...))
+ return &Command{
+ cmd: exec.Command("hg", args...),
+ }
}
--- a/setup/command.go Mon Sep 09 23:11:01 2019 -0500
+++ b/setup/command.go Tue Sep 10 01:37:08 2019 -0500
@@ -5,7 +5,6 @@
"fmt"
"io/ioutil"
"os"
- "os/exec"
"path/filepath"
"strings"
@@ -21,8 +20,8 @@
commitMessage = "initial revision"
)
-func runCmd(cmd *exec.Cmd) error {
- output, err := cmd.CombinedOutput()
+func runCmd(cmd *hg.Command) error {
+ output, err := cmd.Cmd().CombinedOutput()
if len(output) > 0 {
fmt.Printf("%s\n", output)
}
--- a/ssh/commands/run.go Mon Sep 09 23:11:01 2019 -0500
+++ b/ssh/commands/run.go Tue Sep 10 01:37:08 2019 -0500
@@ -2,18 +2,23 @@
import (
"io"
- "os/exec"
"strings"
"sync"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh"
+
+ "bitbucket.org/rw_grim/hgkeeper/hg"
)
-func run(cmd *exec.Cmd, conn ssh.Channel, serverConn *ssh.ServerConn, req *ssh.Request) error {
+func run(hgCmd *hg.Command, conn ssh.Channel, serverConn *ssh.ServerConn, req *ssh.Request) error {
+ cmd := hgCmd.Cmd()
+
teardown := func() {
conn.Close()
+ hgCmd.Teardown()
+
if err := cmd.Wait(); err != nil {
log.Warnf(
"%s command %q failed: %v",
@@ -68,6 +73,8 @@
once.Do(teardown)
}()
+ hgCmd.Setup()
+
if err := cmd.Start(); err != nil {
req.Reply(false, nil)
once.Do(teardown)