From 0d6b555e2631ac6d63a60129dacbeaf863410930 Mon Sep 17 00:00:00 2001 From: Aliaksandr Kazlou Date: Thu, 10 May 2018 10:21:42 +0300 Subject: [PATCH] #58 Add test for having the extra args substitution in the middle of the command Also updated the doc. --- README.md | 10 ++++++--- command/command_test.go | 48 +++++++++++++++++++++++------------------ config/config_test.go | 9 ++++++-- test/config/commands | 5 ++++- 4 files changed, 45 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 0cfc2ce..e12ec04 100644 --- a/README.md +++ b/README.md @@ -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 ... ``` @@ -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 diff --git a/command/command_test.go b/command/command_test.go index d41e5b5..810e141 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -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) + } } } } diff --git a/config/config_test.go b/config/config_test.go index d0b23d7..3e0a276 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -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) diff --git a/test/config/commands b/test/config/commands index 9d87a73..2fd6511 100644 --- a/test/config/commands +++ b/test/config/commands @@ -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