From a39fda5dd6fe6404f0c3266b57083645240922ee Mon Sep 17 00:00:00 2001 From: Rob Bayliss Date: Wed, 10 Feb 2016 10:06:04 -0500 Subject: [PATCH] Better error detection for nonexistent/nonrunning containers in ssh --- cmd/wrappers.go | 15 +++++++++------ vagabond.go | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cmd/wrappers.go b/cmd/wrappers.go index b423b9e..80d593f 100644 --- a/cmd/wrappers.go +++ b/cmd/wrappers.go @@ -1,6 +1,7 @@ package cmd import ( + "bytes" "fmt" "github.com/codegangsta/cli" "os" @@ -42,14 +43,16 @@ var CmdSsh = cli.Command{ Name: "ssh", Usage: "Shell into a running docker container", Action: func(ctx *cli.Context) { - args := []string{"docker", "exec", "-it"} - for _, i := range ctx.Args() { - args = append(args, fmt.Sprintf("$(docker-compose ps -q %s)", i)) + name := ctx.Args()[0] + contid, err := exec.Command("docker-compose", "ps", "-q", name).Output() + contid = bytes.TrimSpace(contid) + if err != nil || bytes.Equal(contid, []byte("")) { + fmt.Printf("Could not find container %s. Are you sure it's running?\n", name) + os.Exit(1) } - args = append(args, "/bin/bash") - fmt.Printf("running: %s\n", sliceToString(args)) - cmd := exec.Command("bash", "-c", sliceToString(args)) + fmt.Printf("running: docker exec -it $(docker-compose ps -q %s) /bin/bash \n", name) + cmd := exec.Command("docker", "exec", "-it", string(contid), "/bin/bash") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr cmd.Stdin = os.Stdin diff --git a/vagabond.go b/vagabond.go index b3ae8ac..fbfe877 100644 --- a/vagabond.go +++ b/vagabond.go @@ -19,6 +19,7 @@ func main() { cmd.CmdDiagnose, cmd.CmdSelfUpdate, + // Simple wrapper commands: cmd.CmdUp, cmd.CmdDestroy, cmd.CmdHalt,