From 978a6e5ecb1ab7a8a224d1c97424066faf533568 Mon Sep 17 00:00:00 2001 From: ardnew Date: Thu, 1 Jul 2021 16:05:46 -0500 Subject: [PATCH 1/2] quickly print task names only with flags --silent and --list --- cmd/task/task.go | 4 ++++ help.go | 33 +++++++++++++++++++++++++++++++++ task.go | 15 +++++++++++++-- 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index 8eff5db5..a972cc2f 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -158,6 +158,10 @@ func main() { OutputStyle: output, } + if list && silent { + e.PrintTaskNames() + return + } if err := e.Setup(); err != nil { log.Fatal(err) } diff --git a/help.go b/help.go index ca0d7841..61a57610 100644 --- a/help.go +++ b/help.go @@ -2,7 +2,10 @@ package task import ( "fmt" + "io" + "os" "sort" + "strings" "text/tabwriter" "github.com/go-task/task/v3/internal/logger" @@ -70,3 +73,33 @@ func (e *Executor) tasksWithDesc() (tasks []*taskfile.Task) { sort.Slice(tasks, func(i, j int) bool { return tasks[i].Task < tasks[j].Task }) return } + +// PrintTaskNames prints only the task names in a taskfile. +func (e *Executor) PrintTaskNames() error { + // if called from cmd/task.go, e.Taskfile has not yet been parsed + if nil == e.Taskfile { + err := e.readTaskfile() + if nil != err { + return err + } + } + var w io.Writer = os.Stdout + if nil != e.Stdout { + w = e.Stdout + } + + // create a slice from all map values + task := make([]*taskfile.Task, 0, len(e.Taskfile.Tasks)) + for _, t := range e.Taskfile.Tasks { + task = append(task, t) + } + + sort.Slice(task, + func(i, j int) bool { + return task[i].Task < task[j].Task + }) + for _, t := range task { + fmt.Fprintf(w, "%s\n", strings.TrimSuffix(t.Task, ":")) + } + return nil +} diff --git a/task.go b/task.go index ecd3ed35..f0a82446 100644 --- a/task.go +++ b/task.go @@ -104,10 +104,21 @@ func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error { return g.Wait() } -// Setup setups Executor's internal state -func (e *Executor) Setup() error { +// readTaskfile selects and parses the entrypoint. +func (e *Executor) readTaskfile() error { + // select the default entrypoint if not provided + if e.Entrypoint == "" { + e.Entrypoint = "Taskfile.yml" + } + var err error e.Taskfile, err = read.Taskfile(e.Dir, e.Entrypoint) + return err +} + +// Setup setups Executor's internal state +func (e *Executor) Setup() error { + err := e.readTaskfile() if err != nil { return err } From 9897f4b52774ad935198241d8fb853e383e65501 Mon Sep 17 00:00:00 2001 From: ardnew Date: Mon, 21 Mar 2022 12:59:25 -0500 Subject: [PATCH 2/2] refactor with support for --list and --list-all --- cmd/task/task.go | 6 ++++-- help.go | 37 +++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index a972cc2f..20e4d612 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -158,10 +158,12 @@ func main() { OutputStyle: output, } - if list && silent { - e.PrintTaskNames() + + if (list || listAll) && silent { + e.ListTaskNames(listAll) return } + if err := e.Setup(); err != nil { log.Fatal(err) } diff --git a/help.go b/help.go index 61a57610..315daaae 100644 --- a/help.go +++ b/help.go @@ -74,32 +74,29 @@ func (e *Executor) tasksWithDesc() (tasks []*taskfile.Task) { return } -// PrintTaskNames prints only the task names in a taskfile. -func (e *Executor) PrintTaskNames() error { +// PrintTaskNames prints only the task names in a Taskfile. +// Only tasks with a non-empty description are printed if allTasks is false. +// Otherwise, all task names are printed. +func (e *Executor) ListTaskNames(allTasks bool) { // if called from cmd/task.go, e.Taskfile has not yet been parsed - if nil == e.Taskfile { - err := e.readTaskfile() - if nil != err { - return err - } + if nil == e.Taskfile && e.readTaskfile() != nil { + return } + // use stdout if no output defined var w io.Writer = os.Stdout - if nil != e.Stdout { + if e.Stdout != nil { w = e.Stdout } - - // create a slice from all map values - task := make([]*taskfile.Task, 0, len(e.Taskfile.Tasks)) + // create a string slice from all map values (*taskfile.Task) + s := make([]string, 0, len(e.Taskfile.Tasks)) for _, t := range e.Taskfile.Tasks { - task = append(task, t) + if allTasks || t.Desc != "" { + s = append(s, strings.TrimRight(t.Task, ":")) + } } - - sort.Slice(task, - func(i, j int) bool { - return task[i].Task < task[j].Task - }) - for _, t := range task { - fmt.Fprintf(w, "%s\n", strings.TrimSuffix(t.Task, ":")) + // sort and print all task names + sort.Strings(s) + for _, t := range s { + fmt.Fprintln(w, t) } - return nil }