Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/gob"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
Expand Down Expand Up @@ -47,6 +48,7 @@ type CliOptions struct {
RunTags []string
RunTagSet mapset.Set
ExecuteOnlyMatchedTags bool
Args []string
}

// OptionsConfig is the set of values to be applied to all tasks or affect general behavior
Expand Down Expand Up @@ -295,9 +297,22 @@ func readTimeCache() {
}
}

// replaceArguments replaces the command line arguments in the given string
func replaceArguments(source string) string {
replaced := source
for i, arg := range config.Cli.Args {
replaced = strings.Replace(replaced, fmt.Sprintf("$%v", i+1), arg, -1)
}
replaced = strings.Replace(replaced, "$*", strings.Join(config.Cli.Args, " "), -1)
return replaced
}

func (taskConfig *TaskConfig) inflate() (tasks []TaskConfig) {
taskConfig.CmdString = replaceArguments(taskConfig.CmdString)
if taskConfig.Name == "" {
taskConfig.Name = taskConfig.CmdString
} else {
taskConfig.Name = replaceArguments(taskConfig.Name)
}

if len(taskConfig.ForEach) > 0 {
Expand Down
31 changes: 31 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,37 @@ func TestRemoveOneValue(t *testing.T) {

}

func TestCommandArguments(t *testing.T) {
yamlStr := `
tasks:
- cmd: command-with-args $1 $2
name: Arg1=$1 Arg2=$2
- cmd: all-args $*
name: Args=$*
`

config.Cli.Args = []string{"First", "Second"}
parseRunYaml([]byte(yamlStr))
tasks := CreateTasks()
if len(tasks) != 2 {
t.Error("Expected two tasks. Got: ", len(tasks))
}

if tasks[0].Config.CmdString != "command-with-args First Second" {
t.Error("Expected arguments to be replaced. Got: ", tasks[0].Config.CmdString)
}
if tasks[0].Config.Name != "Arg1=First Arg2=Second" {
t.Error("Expected arguments to be replaced in task name. Got: ", tasks[0].Config.Name)
}

if tasks[1].Config.CmdString != "all-args First Second" {
t.Error("Expected all arguments to be replaced. Got: ", tasks[1].Config.CmdString)
}
if tasks[1].Config.Name != "Args=First Second" {
t.Error("Expected all arguments to be replaced in task name. Got: ", tasks[1].Config.Name)
}
}

func TestCreateTasks_SuccessfulParse(t *testing.T) {
var expStr, actStr string
var expOpt, actOpt bool
Expand Down
4 changes: 1 addition & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,10 @@ func main() {
Action: func(cliCtx *cli.Context) error {
if cliCtx.NArg() < 1 {
exitWithErrorMessage("Must provide the path to a bashful yaml file")
} else if cliCtx.NArg() > 1 {
exitWithErrorMessage("Only one bashful yaml file can be provided at a time")
}

userYamlPath := cliCtx.Args().Get(0)
config.Cli.Args = cliCtx.Args().Tail()

if cliCtx.String("tags") != "" && cliCtx.String("only-tags") != "" {
exitWithErrorMessage("Options 'tags' and 'only-tags' are mutually exclusive.")
Expand All @@ -395,7 +394,6 @@ func main() {
config.Cli.RunTags = append(config.Cli.RunTags, value)
}
}
//config.Cli.RunTags = []string{"some-app1"}

run(userYamlPath)

Expand Down