diff --git a/command/command_test.go b/command/command_test.go index da904c8..a108b72 100644 --- a/command/command_test.go +++ b/command/command_test.go @@ -4,6 +4,8 @@ import ( "flag" "testing" + "github.com/zshamrock/vmx/config" + "github.com/zshamrock/vmx/core" "gopkg.in/urfave/cli.v1" ) @@ -30,24 +32,33 @@ func TestGetCommand(t *testing.T) { } func TestGetCommandExtraArgs(t *testing.T) { - t.SkipNow() + config.Init("") followFlags := []string{"", "-f", "--follow"} for _, followFlag := range followFlags { + commandText := "logs-extra" + extraText := "rest.log" flags := flag.FlagSet{} - flags.Bool("follow", false, "") - commandText := "tail -f -n 10 logs/rest.log" - flags.Parse([]string{"--", followFlag, "dev", commandText}) + follow := false + arguments := []string{"dev", commandText, extraText} + if followFlag != "" { + follow = true + flags.Bool("follow", false, "") + arguments = append([]string{"--", followFlag}, arguments...) + } + flags.Parse(arguments) app := cli.NewApp() context := cli.NewContext(app, &flags, nil) - command, extraArgs := getCommand(context, true) - if !command.IsAdHoc() { - t.Errorf("Command name should be ad-hoc, but got %s", command.Name) + command, extraArgs := getCommand(context, follow) + expectedCommand := core.Command{ + Name: "logs-extra", + Command: "tail -f -n 10 logs/%s", + WorkingDir: "", } - if command.Command != commandText { - t.Errorf("Command should be %s, but got %s", commandText, command.Command) + if command != expectedCommand { + t.Errorf("Command should be %v, but got %v", expectedCommand, command) } - if extraArgs != "" { - t.Errorf("Extra args should be empty, but got %s", extraArgs) + 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 1b7ec11..cd11412 100644 --- a/command/run.go +++ b/command/run.go @@ -44,7 +44,7 @@ func CmdRun(c *cli.Context) { } cmd := command.Command if command.WorkingDir != "" { - cmd = strings.TrimSpace(fmt.Sprintf("cd %s && %s %s", command.WorkingDir, cmd, extraArgs)) + cmd = strings.TrimSpace(fmt.Sprintf("cd %s && %s", command.WorkingDir, fmt.Sprintf(cmd, extraArgs))) } fmt.Printf("Running command: %s from %s on %v\n", command.Command, command.WorkingDir, hosts) sshConfig := readSSHConfig(config.DefaultConfig) diff --git a/config/config_test.go b/config/config_test.go index cdd38e0..d0b23d7 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -39,6 +39,11 @@ func TestReadConfig(t *testing.T) { Command: "df -h", WorkingDir: "", }, + "logs-extra": { + Name: "logs-extra", + Command: "tail -f -n 10 logs/%s", + WorkingDir: "", + }, } if diff := deep.Equal(commands, expected); diff != nil { t.Error(diff) diff --git a/test/config/commands b/test/config/commands index bc03d1b..9d87a73 100644 --- a/test/config/commands +++ b/test/config/commands @@ -14,3 +14,6 @@ command=./redeploy.sh [disk-space] command=df -h + +[logs-extra] +command=tail -f -n 10 logs/%s