From 7e13e7d4ec8523dbca10b8420af7ed9f58e2f70f Mon Sep 17 00:00:00 2001 From: Nate Meyer <672246+notnmeyer@users.noreply.github.com> Date: Tue, 10 Sep 2024 21:10:33 -0700 Subject: [PATCH] feat: add CLI_ARGS placeholder in list output (#86) --- cmd/tsk/tsk.go | 2 +- internal/task/task.go | 4 ++-- internal/task/task_test.go | 23 +++++++++++++++++++++-- internal/task/util.go | 7 ++++++- 4 files changed, 30 insertions(+), 6 deletions(-) diff --git a/cmd/tsk/tsk.go b/cmd/tsk/tsk.go index f0698fc..06cefc8 100644 --- a/cmd/tsk/tsk.go +++ b/cmd/tsk/tsk.go @@ -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) } diff --git a/internal/task/task.go b/internal/task/task.go index 0e81907..3b9f4c9 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -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() @@ -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 } diff --git a/internal/task/task_test.go b/internal/task/task_test.go index 957972b..ead8f37 100644 --- a/internal/task/task_test.go +++ b/internal/task/task_test.go @@ -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) } @@ -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, @@ -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()) + } +} diff --git a/internal/task/util.go b/internal/task/util.go index c40d568..2953fd3 100644 --- a/internal/task/util.go +++ b/internal/task/util.go @@ -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