Skip to content

Commit

Permalink
#58 Return already built command with the applied extra args
Browse files Browse the repository at this point in the history
  • Loading branch information
zshamrock committed May 10, 2018
1 parent 0cb37eb commit ed5fc55
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
12 changes: 3 additions & 9 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

Expand All @@ -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)
}
}
}

Expand Down
11 changes: 7 additions & 4 deletions command/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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))
Expand All @@ -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
Expand Down

0 comments on commit ed5fc55

Please sign in to comment.