Skip to content

Commit

Permalink
#58 Add test for having the extra args substitution in the middle of …
Browse files Browse the repository at this point in the history
…the command

Also updated the doc.
  • Loading branch information
zshamrock committed May 10, 2018
1 parent ed5fc55 commit 0d6b555
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,17 @@ Confirm to run "redeploy" command on [host-name] - yes/no or y/n:

#### Pass extra arguments to the command

Any extra arguments passed to the the `run` command will be then passed to the actual command to run. Here the example:
Any extra arguments passed to the the `run` command will be then passed to the actual command to run replacing the
substitution `%s` in the command name from the config file.

Here is the example:

```
# commands file
...
[logs]
workingdir=/opt/app/logs
command=tail -n 10
command=tail -n 10 %s
...
```

Expand All @@ -163,7 +166,8 @@ vmx run host-name logs -f rest.log
```

it will be interpreted as `tail -n 10 -f rest.log` (i.e. all extra arguments are passed to the `command` defined in the
`commands` file).
`commands` file, more precisely replacing the `%s` placeholder, so it can be in any place of the command, not only in
the end).

#### Running the ad-hoc command

Expand Down
48 changes: 27 additions & 21 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,34 @@ func TestGetCommand(t *testing.T) {
func TestGetCommandExtraArgs(t *testing.T) {
config.Init("")
followFlags := []string{"", "-f", "--follow"}
commandsData := []map[string]string{
{"name": "logs-extra1", "extra": "rest.log", "command": "tail -f -n 10 logs/rest.log"},
{"name": "logs-extra2", "extra": "5", "command": "tail -f -n 5 logs/app.log"},
}
for _, followFlag := range followFlags {
commandText := "logs-extra"
extraText := "rest.log"
arguments := []string{"dev", commandText, extraText}
flags := flag.FlagSet{}
follow := false
if followFlag != "" {
flags.Bool("follow", false, "")
follow = true
arguments = append([]string{"--", followFlag}, arguments...)
}
flags.Parse(arguments)
app := cli.NewApp()
context := cli.NewContext(app, &flags, nil)
command := getCommand(context, follow)
expectedCommand := core.Command{
Name: "logs-extra",
Command: "tail -f -n 10 logs/" + extraText,
WorkingDir: "",
}
if command != expectedCommand {
t.Errorf("Command should be %v, but got %v", expectedCommand, command)
for _, commandData := range commandsData {
commandText := commandData["name"]
extraText := commandData["extra"]
arguments := []string{"dev", commandText, extraText}
flags := flag.FlagSet{}
follow := false
if followFlag != "" {
flags.Bool("follow", false, "")
follow = true
arguments = append([]string{"--", followFlag}, arguments...)
}
flags.Parse(arguments)
app := cli.NewApp()
context := cli.NewContext(app, &flags, nil)
command := getCommand(context, follow)
expectedCommand := core.Command{
Name: commandData["name"],
Command: commandData["command"],
WorkingDir: "",
}
if command != expectedCommand {
t.Errorf("Command should be %v, but got %v", expectedCommand, command)
}
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,16 @@ func TestReadConfig(t *testing.T) {
Command: "df -h",
WorkingDir: "",
},
"logs-extra": {
Name: "logs-extra",
"logs-extra1": {
Name: "logs-extra1",
Command: "tail -f -n 10 logs/%s",
WorkingDir: "",
},
"logs-extra2": {
Name: "logs-extra2",
Command: "tail -f -n %s logs/app.log",
WorkingDir: "",
},
}
if diff := deep.Equal(commands, expected); diff != nil {
t.Error(diff)
Expand Down
5 changes: 4 additions & 1 deletion test/config/commands
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ command=./redeploy.sh
[disk-space]
command=df -h

[logs-extra]
[logs-extra1]
command=tail -f -n 10 logs/%s

[logs-extra2]
command=tail -f -n %s logs/app.log

0 comments on commit 0d6b555

Please sign in to comment.