From 6c5da563f352c02ee247456ed160ce7dafba5d08 Mon Sep 17 00:00:00 2001 From: Rob Bayliss Date: Wed, 10 Feb 2016 09:53:45 -0500 Subject: [PATCH] Start adding wrapper commands --- cmd/selfupdate.go | 6 ++-- cmd/wrappers.go | 78 +++++++++++++++++++++++++++++++++++++++++++++++ vagabond.go | 5 +++ 3 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 cmd/wrappers.go diff --git a/cmd/selfupdate.go b/cmd/selfupdate.go index 3d5408a..d6d76bf 100644 --- a/cmd/selfupdate.go +++ b/cmd/selfupdate.go @@ -8,8 +8,8 @@ import ( "log" "net/http" "os" - "runtime" "path/filepath" + "runtime" ) const UpdateUrl = "https://api.github.com/repos/LastCallMedia/vagabond/releases" @@ -26,7 +26,7 @@ func runSelfUpdate(ctx *cli.Context) { log.Fatalf("Unable to fetch release data") } filename := "vagabond_" + runtime.GOOS + "_" + runtime.GOARCH - asset, found := searchAssets(release.Assets, filename) + asset, found := assetSearch(release.Assets, filename) if !found { log.Fatal("Unable to find a release asset for this OS and architecture") } @@ -54,7 +54,7 @@ func getRelease(version string) (release *github.RepositoryRelease, err error) { return } -func searchAssets(assets []github.ReleaseAsset, filename string) (asset github.ReleaseAsset, found bool) { +func assetSearch(assets []github.ReleaseAsset, filename string) (asset github.ReleaseAsset, found bool) { found = false for _, potentialAsset := range assets { if *potentialAsset.Name == filename { diff --git a/cmd/wrappers.go b/cmd/wrappers.go new file mode 100644 index 0000000..b423b9e --- /dev/null +++ b/cmd/wrappers.go @@ -0,0 +1,78 @@ +package cmd + +import ( + "fmt" + "github.com/codegangsta/cli" + "os" + "os/exec" +) + +var CmdUp = cli.Command{ + Name: "up", + Usage: "Start one or more docker containers", + Action: func(ctx *cli.Context) { + args := []string{"up", "-d"} + args = append(args, ctx.Args()...) + + _ = pipeCommand("docker-compose", args...) + }, +} + +var CmdDestroy = cli.Command{ + Name: "destroy", + Usage: "Remove one or more docker containers", + Action: func(ctx *cli.Context) { + args := []string{"rm"} + args = append(args, ctx.Args()...) + _ = pipeCommand("docker-compose", args...) + }, +} + +var CmdHalt = cli.Command{ + Name: "halt", + Usage: "Stop one or more docker containers", + Action: func(ctx *cli.Context) { + args := []string{"stop"} + args = append(args, ctx.Args()...) + _ = pipeCommand("docker-compose", args...) + }, +} + +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)) + } + args = append(args, "/bin/bash") + + fmt.Printf("running: %s\n", sliceToString(args)) + cmd := exec.Command("bash", "-c", sliceToString(args)) + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Stdin = os.Stdin + cmd.Run() + }, +} + +func sliceToString(slice []string) string { + parts := "" + for _, i := range slice { + parts += fmt.Sprintf(" %s", i) + } + return parts +} + +func pipeCommand(name string, arg ...string) error { + parts := name + sliceToString(arg) + fmt.Printf("running %s\n", parts) + + cmd := exec.Command(name, arg...) + cmd.Stdout = os.Stdout + cmd.Stdin = os.Stdin + cmd.Stderr = os.Stderr + err := cmd.Run() + return err +} diff --git a/vagabond.go b/vagabond.go index aed5d1d..b3ae8ac 100644 --- a/vagabond.go +++ b/vagabond.go @@ -18,6 +18,11 @@ func main() { cmd.CmdSetup, cmd.CmdDiagnose, cmd.CmdSelfUpdate, + + cmd.CmdUp, + cmd.CmdDestroy, + cmd.CmdHalt, + cmd.CmdSsh, } app.Run(os.Args) }