diff --git a/command/command_test.go b/command/command_test.go index c0b9e77..d41e5b5 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -24,16 +24,13 @@ func TestGetCommand(t *testing.T) { flags.Parse(arguments) app := cli.NewApp() context := cli.NewContext(app, &flags, nil) - command, extraArgs := getCommand(context, follow) + command := getCommand(context, follow) if !command.IsAdHoc() { t.Errorf("Command name should be ad-hoc, but got %s", command.Name) } if command.Command != commandText { t.Errorf("Command should be %s, but got %s", commandText, command.Command) } - if extraArgs != "" { - t.Errorf("Extra args should be empty, but got %s", extraArgs) - } } } @@ -54,18 +51,15 @@ func TestGetCommandExtraArgs(t *testing.T) { flags.Parse(arguments) app := cli.NewApp() context := cli.NewContext(app, &flags, nil) - command, extraArgs := getCommand(context, follow) + command := getCommand(context, follow) expectedCommand := core.Command{ Name: "logs-extra", - Command: "tail -f -n 10 logs/%s", + Command: "tail -f -n 10 logs/" + extraText, WorkingDir: "", } if command != expectedCommand { t.Errorf("Command should be %v, but got %v", expectedCommand, command) } - if extraArgs != extraText { - t.Errorf("Extra args should %s, but got %s", extraText, extraArgs) - } } } diff --git a/command/run.go b/command/run.go index cd11412..2ba600c 100644 --- a/command/run.go +++ b/command/run.go @@ -31,7 +31,7 @@ type execOutput struct { func CmdRun(c *cli.Context) { CheckUpdate(c) follow := ContainsFollow(c) - command, extraArgs := getCommand(c, follow) + command := getCommand(c, follow) hosts := getHosts(c, follow) var confirmation string if command.RequiresConfirmation { @@ -44,7 +44,7 @@ func CmdRun(c *cli.Context) { } cmd := command.Command if command.WorkingDir != "" { - cmd = strings.TrimSpace(fmt.Sprintf("cd %s && %s", command.WorkingDir, fmt.Sprintf(cmd, extraArgs))) + cmd = strings.TrimSpace(fmt.Sprintf("cd %s && %s", command.WorkingDir, cmd)) } fmt.Printf("Running command: %s from %s on %v\n", command.Command, command.WorkingDir, hosts) sshConfig := readSSHConfig(config.DefaultConfig) @@ -77,7 +77,7 @@ func CmdRun(c *cli.Context) { fmt.Println(output.output) } } -func getCommand(c *cli.Context, follow bool) (core.Command, string) { +func getCommand(c *cli.Context, follow bool) core.Command { args := c.Args() actualCommandNameArgsIndex := getActualArgsIndex(commandNameArgsIndex, follow) commandName := strings.TrimSpace(args.Get(actualCommandNameArgsIndex)) @@ -101,7 +101,10 @@ func getCommand(c *cli.Context, follow bool) (core.Command, string) { } extraArgs = strings.Join(args.Tail()[extraArgsIndex:], " ") } - return command, extraArgs + if extraArgs != "" { + command.Command = fmt.Sprintf(command.Command, extraArgs) + } + return command } func getActualArgsIndex(argsIndex int, follow bool) int { actualArgsIndex := argsIndex