From 9bed7f7a9be99a191ea704d32331a39ace48ec0b Mon Sep 17 00:00:00 2001 From: Kevin Ard Date: Fri, 13 Nov 2020 15:27:03 -0500 Subject: [PATCH] feat (help): allow cli option to list tasks with no desc added an add'l cli option that lists all tasks, with or without description. orig. behavior: task -l lists tasks with desc field new behaviour: task -la or task -a will list all tasks. if task has desc, it will be included. BREAKING CHANGES: none, that I know of. NOTES/Concerns: - This is wip. - Haven't checked how it interacts with bash completion. - The new Executor.TaskNames func does not use e.CompiledTask(taskfile.Call{Task: task.Task}) --- cmd/task/task.go | 6 ++++-- help.go | 24 +++++++++++++++++++++--- task.go | 4 +++- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/cmd/task/task.go b/cmd/task/task.go index ec394ca6..79be2334 100644 --- a/cmd/task/task.go +++ b/cmd/task/task.go @@ -57,6 +57,7 @@ func main() { helpFlag bool init bool list bool + listAll bool status bool force bool watch bool @@ -75,6 +76,7 @@ func main() { pflag.BoolVarP(&helpFlag, "help", "h", false, "shows Task usage") pflag.BoolVarP(&init, "init", "i", false, "creates a new Taskfile.yml in the current folder") pflag.BoolVarP(&list, "list", "l", false, "lists tasks with description of current Taskfile") + pflag.BoolVarP(&listAll, "list-all", "a", false, "list tasks with or without a description") pflag.BoolVar(&status, "status", false, "exits with non-zero exit code if any of the given tasks is not up-to-date") pflag.BoolVarP(&force, "force", "f", false, "forces execution even when the task is up-to-date") pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task") @@ -148,8 +150,8 @@ func main() { return } - if list { - e.PrintTasksHelp() + if list || listAll { + e.PrintTasksHelp(listAll) return } diff --git a/help.go b/help.go index af12ddcb..f43530e0 100644 --- a/help.go +++ b/help.go @@ -9,10 +9,19 @@ import ( "github.com/go-task/task/v3/taskfile" ) -// PrintTasksHelp prints help os tasks that have a description -func (e *Executor) PrintTasksHelp() { - tasks := e.tasksWithDesc() +// PrintTasksHelp prints tasks' help. +// Behavior is governed by listAll. When false, only tasks with descriptions are reported. +// When true, all tasks are reported with descriptions shown where available. +func (e *Executor) PrintTasksHelp(listAll bool) { + var tasks []*taskfile.Task + if listAll == true { + tasks = e.taskNames() + } else { + tasks = e.tasksWithDesc() + } + if len(tasks) == 0 { + // TODO: This message should be more informative. Maybe a hint to try -la for showing all? e.Logger.Outf(logger.Yellow, "task: No tasks with description available") return } @@ -26,6 +35,15 @@ func (e *Executor) PrintTasksHelp() { w.Flush() } +func (e *Executor) taskNames() (tasks []*taskfile.Task) { + tasks = make([]*taskfile.Task, 0, len(e.Taskfile.Tasks)) + for _, task := range e.Taskfile.Tasks { + tasks = append(tasks, task) + } + sort.Slice(tasks, func(i, j int) bool { return tasks[i].Task < tasks[j].Task }) + return +} + func (e *Executor) tasksWithDesc() (tasks []*taskfile.Task) { tasks = make([]*taskfile.Task, 0, len(e.Taskfile.Tasks)) for _, task := range e.Taskfile.Tasks { diff --git a/task.go b/task.go index 2709ecdb..d8945be0 100644 --- a/task.go +++ b/task.go @@ -64,7 +64,9 @@ func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error { for _, c := range calls { if _, ok := e.Taskfile.Tasks[c.Task]; !ok { // FIXME: move to the main package - e.PrintTasksHelp() + // FIXME: (ard.kevin.84@gmail.com) changed the PrintTasksHelp signature to support show all/some. + // False preserves original behavior, but should be reviewed. + e.PrintTasksHelp(false) return &taskNotFoundError{taskName: c.Task} } }