Skip to content

Commit

Permalink
Start adding wrapper commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rbayliss committed Feb 10, 2016
1 parent d8d1c08 commit 6c5da56
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 3 deletions.
6 changes: 3 additions & 3 deletions cmd/selfupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"log"
"net/http"
"os"
"runtime"
"path/filepath"
"runtime"
)

const UpdateUrl = "https://api.github.com/repos/LastCallMedia/vagabond/releases"
Expand All @@ -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")
}
Expand Down Expand Up @@ -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 {
Expand Down
78 changes: 78 additions & 0 deletions cmd/wrappers.go
Original file line number Diff line number Diff line change
@@ -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
}
5 changes: 5 additions & 0 deletions vagabond.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ func main() {
cmd.CmdSetup,
cmd.CmdDiagnose,
cmd.CmdSelfUpdate,

cmd.CmdUp,
cmd.CmdDestroy,
cmd.CmdHalt,
cmd.CmdSsh,
}
app.Run(os.Args)
}

0 comments on commit 6c5da56

Please sign in to comment.