Skip to content

Commit

Permalink
feat: add CLI_ARGS placeholder in list output (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
notnmeyer authored Sep 11, 2024
1 parent 1e876ae commit 7e13e7d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/tsk/tsk.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func main() {
}

// cfg is the parsed task file
cfg, err := task.NewTaskConfig(opts.taskFile, opts.cliArgs)
cfg, err := task.NewTaskConfig(opts.taskFile, opts.cliArgs, opts.listTasks)
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func filterTasks(tasks *map[string]Task, regex *regexp.Regexp) map[string]Task {
return filtered
}

func NewTaskConfig(taskFile, cliArgs string) (*Config, error) {
func NewTaskConfig(taskFile, cliArgs string, listTasks bool) (*Config, error) {
var err error
if taskFile == "" {
dir, _ := os.Getwd()
Expand All @@ -260,7 +260,7 @@ func NewTaskConfig(taskFile, cliArgs string) (*Config, error) {
}

// render the task file as a template
rendered, err := render(taskFile, cliArgs)
rendered, err := render(taskFile, cliArgs, listTasks)
if err != nil {
return nil, err
}
Expand Down
23 changes: 21 additions & 2 deletions internal/task/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestFindTaskFile(t *testing.T) {
func TestDotEnv(t *testing.T) {
var taskFile, cliArgs string

config, err := NewTaskConfig(taskFile, cliArgs)
config, err := NewTaskConfig(taskFile, cliArgs, false)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -301,7 +301,7 @@ func TestTemplates(t *testing.T) {
expected := regexp.MustCompile(cliArgs)
wd, _ := os.Getwd()
path, _ := findTaskFile(wd, "tasks.toml")
config, _ := NewTaskConfig(path, cliArgs)
config, _ := NewTaskConfig(path, cliArgs, false)
out := new(bytes.Buffer)
exec := Executor{
Stdout: out,
Expand All @@ -312,3 +312,22 @@ func TestTemplates(t *testing.T) {
t.Errorf("Expected '%s' to match '%s'", cliArgs, out.String())
}
}

// when building --list output for tasks that use CLI_ARGS test that placeholder
// text is inserted when CLI_ARGS arent provided
func TestTemplatesWithPlaceholders(t *testing.T) {
placeholder := "{{.CLI_ARGS}}"
expected := regexp.MustCompile(placeholder)
wd, _ := os.Getwd()
path, _ := findTaskFile(wd, "tasks.toml")
config, _ := NewTaskConfig(path, "", true)
out := new(bytes.Buffer)
exec := Executor{
Stdout: out,
}

exec.RunTasks(config, &[]string{"template"})
if !expected.Match(out.Bytes()) {
t.Errorf("Expected '%s' to match '%s'", placeholder, out.String())
}
}
7 changes: 6 additions & 1 deletion internal/task/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,17 @@ func appendDotEnvToEnv(env []string, dotenv string) ([]string, error) {
return env, nil
}

func render(file, cliArgs string) (*bytes.Buffer, error) {
func render(file, cliArgs string, cliArgsPlaceholder bool) (*bytes.Buffer, error) {
tmpl, err := template.ParseFiles(file)
if err != nil {
return nil, err
}

// insert a placeholder value for cliArgs for display purposes
if cliArgsPlaceholder && cliArgs == "" {
cliArgs = "{{.CLI_ARGS}}"
}

var renderedBuffer bytes.Buffer
if err := tmpl.Execute(&renderedBuffer, &Vals{CLI_ARGS: cliArgs}); err != nil {
return nil, err
Expand Down

0 comments on commit 7e13e7d

Please sign in to comment.