diff --git a/cli/app/cmd.go b/cli/app/cmd.go index 12295b94..36cbf1da 100644 --- a/cli/app/cmd.go +++ b/cli/app/cmd.go @@ -32,6 +32,14 @@ var localCmdFlag = &cli.BoolFlag{ Destination: &localCmd, } +var remoteUser string +var remoteUserFlag = &cli.StringFlag{ + Name: "user, u", + Value: "", + Usage: "User to run command within a service context", + Destination: &remoteUser, +} + var appCmdCommand = cli.Command{ Name: "command", Aliases: []string{"cmd"}, @@ -52,18 +60,15 @@ Example: Flags: []cli.Flag{ internal.DebugFlag, localCmdFlag, + remoteUserFlag, }, BashComplete: autocomplete.AppNameComplete, Before: internal.SubCommandBefore, Action: func(c *cli.Context) error { app := internal.ValidateApp(c) - if len(c.Args()) <= 2 && !localCmd { - internal.ShowSubcommandHelpAndError(c, errors.New("missing /? did you mean to pass --local?")) - } - - if len(c.Args()) > 2 && localCmd { - internal.ShowSubcommandHelpAndError(c, errors.New("cannot specify and --local together")) + if localCmd && remoteUser != "" { + internal.ShowSubcommandHelpAndError(c, errors.New("cannot use --local & together")) } abraSh := path.Join(config.RECIPES_DIR, app.Recipe, "abra.sh") @@ -74,6 +79,20 @@ Example: logrus.Fatal(err) } + var parsedCmdArgs string + var cmdArgsIdx int + var hasCmdArgs bool + for idx, arg := range c.Args() { + if arg == "--" { + cmdArgsIdx = idx + hasCmdArgs = true + } + + if hasCmdArgs && idx > cmdArgsIdx { + parsedCmdArgs += fmt.Sprintf("%s ", c.Args().Get(idx)) + } + } + if localCmd { cmdName := c.Args().Get(1) if err := ensureCommand(abraSh, app.Recipe, cmdName); err != nil { @@ -82,8 +101,17 @@ Example: logrus.Debugf("--local detected, running %s on local work station", cmdName) - sourceAndExec := fmt.Sprintf("TARGET=local; APP_NAME=%s; . %s; %s", app.StackName(), abraSh, cmdName) + var sourceAndExec string + if hasCmdArgs { + logrus.Debugf("parsed following command arguments: %s", parsedCmdArgs) + sourceAndExec = fmt.Sprintf("TARGET=local; APP_NAME=%s; . %s; %s %s", app.StackName(), abraSh, cmdName, parsedCmdArgs) + } else { + logrus.Debug("did not detect any command arguments") + sourceAndExec = fmt.Sprintf("TARGET=local; APP_NAME=%s; . %s; %s", app.StackName(), abraSh, cmdName) + } + cmd := exec.Command("/bin/sh", "-c", sourceAndExec) + if err := internal.RunCmd(cmd); err != nil { logrus.Fatal(err) } @@ -113,20 +141,6 @@ Example: logrus.Debugf("running command %s within the context of %s_%s", cmdName, app.StackName(), targetServiceName) - var parsedCmdArgs string - var cmdArgsIdx int - var hasCmdArgs bool - for idx, arg := range c.Args() { - if arg == "--" { - cmdArgsIdx = idx - hasCmdArgs = true - } - - if hasCmdArgs && idx > cmdArgsIdx { - parsedCmdArgs += fmt.Sprintf("%s ", c.Args().Get(idx)) - } - } - if hasCmdArgs { logrus.Debugf("parsed following command arguments: %s", parsedCmdArgs) } else { @@ -200,6 +214,11 @@ func runCmdRemote(app config.App, abraSh, serviceName, cmdName, cmdArgs string) Tty: true, } + if remoteUser != "" { + logrus.Debugf("running command with user %s", remoteUser) + execCreateOpts.User = remoteUser + } + // FIXME: avoid instantiating a new CLI dcli, err := command.NewDockerCli() if err != nil {