Skip to content

Commit

Permalink
Merge pull request #14 from orchardup/add-run-command
Browse files Browse the repository at this point in the history
Add "orchard run" command
  • Loading branch information
aanand committed Jul 11, 2014
2 parents 4b66d1b + 428398f commit b36f358
Showing 1 changed file with 38 additions and 3 deletions.
41 changes: 38 additions & 3 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ func (c *Command) Usage() {

func (c *Command) UsageError(format string, args ...interface{}) error {
fmt.Fprintf(os.Stderr, format, args...)
fmt.Fprintf(os.Stderr, "\nUsage: %s\n", c.UsageLine)
fmt.Fprintf(os.Stderr, "\nUsage: %s\n\n", c.UsageLine)
fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(c.Long))
os.Exit(2)
return fmt.Errorf(format, args...)
}

var All = []*Command{
Hosts,
Docker,
Proxy,
Hosts,
IP,
Proxy,
Run,
}

var HostSubcommands = []*Command{
Expand All @@ -70,6 +72,7 @@ func init() {
Docker.Run = RunDocker
Proxy.Run = RunProxy
IP.Run = RunIP
Run.Run = RunRun
}

var Hosts = &Command{
Expand Down Expand Up @@ -164,6 +167,23 @@ host (named 'default') will be assumed.
`,
}

var Run = &Command{
UsageLine: "run [-H HOST] COMMAND [ARGS...]",
Short: "Run a command with the DOCKER_HOST envvar set",
Long: `Start a proxy to an Orchard host and run a command locally
with the DOCKER_HOST environment variable set.
For example:
$ orchard run fig up
You can optionally specify which host - if you don't, the default
host (named 'default') will be assumed.
`,
}

var flRunHost = Run.Flag.String("H", "", "")

func RunHosts(cmd *Command, args []string) error {
list := len(args) == 0 || (len(args) == 1 && args[0] == "ls")

Expand Down Expand Up @@ -330,6 +350,21 @@ func RunIP(cmd *Command, args []string) error {
return nil
}

func RunRun(cmd *Command, args []string) error {
if len(args) < 1 {
return cmd.UsageError("`orchard run` expects at least 1 argument")
}
return WithDockerProxy("", *flRunHost, func(listenURL string) error {
os.Setenv("DOCKER_HOST", listenURL)

cmd := exec.Command(args[0], args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
})
}

func WithDockerProxy(listenURL, hostName string, callback func(string) error) error {
if hostName == "" {
hostName = "default"
Expand Down

0 comments on commit b36f358

Please sign in to comment.