Skip to content

Commit fc979e2

Browse files
nacxwagoodman
authored andcommitted
Add basic support for passing command-line arguments to commands (#38)
1 parent c456116 commit fc979e2

File tree

3 files changed

+47
-3
lines changed

3 files changed

+47
-3
lines changed

config.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"encoding/gob"
55
"errors"
6+
"fmt"
67
"io/ioutil"
78
"os"
89
"path"
@@ -47,6 +48,7 @@ type CliOptions struct {
4748
RunTags []string
4849
RunTagSet mapset.Set
4950
ExecuteOnlyMatchedTags bool
51+
Args []string
5052
}
5153

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

300+
// replaceArguments replaces the command line arguments in the given string
301+
func replaceArguments(source string) string {
302+
replaced := source
303+
for i, arg := range config.Cli.Args {
304+
replaced = strings.Replace(replaced, fmt.Sprintf("$%v", i+1), arg, -1)
305+
}
306+
replaced = strings.Replace(replaced, "$*", strings.Join(config.Cli.Args, " "), -1)
307+
return replaced
308+
}
309+
298310
func (taskConfig *TaskConfig) inflate() (tasks []TaskConfig) {
311+
taskConfig.CmdString = replaceArguments(taskConfig.CmdString)
299312
if taskConfig.Name == "" {
300313
taskConfig.Name = taskConfig.CmdString
314+
} else {
315+
taskConfig.Name = replaceArguments(taskConfig.Name)
301316
}
302317

303318
if len(taskConfig.ForEach) > 0 {

config_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,37 @@ func TestRemoveOneValue(t *testing.T) {
6969

7070
}
7171

72+
func TestCommandArguments(t *testing.T) {
73+
yamlStr := `
74+
tasks:
75+
- cmd: command-with-args $1 $2
76+
name: Arg1=$1 Arg2=$2
77+
- cmd: all-args $*
78+
name: Args=$*
79+
`
80+
81+
config.Cli.Args = []string{"First", "Second"}
82+
parseRunYaml([]byte(yamlStr))
83+
tasks := CreateTasks()
84+
if len(tasks) != 2 {
85+
t.Error("Expected two tasks. Got: ", len(tasks))
86+
}
87+
88+
if tasks[0].Config.CmdString != "command-with-args First Second" {
89+
t.Error("Expected arguments to be replaced. Got: ", tasks[0].Config.CmdString)
90+
}
91+
if tasks[0].Config.Name != "Arg1=First Arg2=Second" {
92+
t.Error("Expected arguments to be replaced in task name. Got: ", tasks[0].Config.Name)
93+
}
94+
95+
if tasks[1].Config.CmdString != "all-args First Second" {
96+
t.Error("Expected all arguments to be replaced. Got: ", tasks[1].Config.CmdString)
97+
}
98+
if tasks[1].Config.Name != "Args=First Second" {
99+
t.Error("Expected all arguments to be replaced in task name. Got: ", tasks[1].Config.Name)
100+
}
101+
}
102+
72103
func TestCreateTasks_SuccessfulParse(t *testing.T) {
73104
var expStr, actStr string
74105
var expOpt, actOpt bool

main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,11 +373,10 @@ func main() {
373373
Action: func(cliCtx *cli.Context) error {
374374
if cliCtx.NArg() < 1 {
375375
exitWithErrorMessage("Must provide the path to a bashful yaml file")
376-
} else if cliCtx.NArg() > 1 {
377-
exitWithErrorMessage("Only one bashful yaml file can be provided at a time")
378376
}
379377

380378
userYamlPath := cliCtx.Args().Get(0)
379+
config.Cli.Args = cliCtx.Args().Tail()
381380

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

400398
run(userYamlPath)
401399

0 commit comments

Comments
 (0)