Skip to content

Commit 9bed7f7

Browse files
author
Kevin Ard
committed
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})
1 parent b136166 commit 9bed7f7

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

cmd/task/task.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func main() {
5757
helpFlag bool
5858
init bool
5959
list bool
60+
listAll bool
6061
status bool
6162
force bool
6263
watch bool
@@ -75,6 +76,7 @@ func main() {
7576
pflag.BoolVarP(&helpFlag, "help", "h", false, "shows Task usage")
7677
pflag.BoolVarP(&init, "init", "i", false, "creates a new Taskfile.yml in the current folder")
7778
pflag.BoolVarP(&list, "list", "l", false, "lists tasks with description of current Taskfile")
79+
pflag.BoolVarP(&listAll, "list-all", "a", false, "list tasks with or without a description")
7880
pflag.BoolVar(&status, "status", false, "exits with non-zero exit code if any of the given tasks is not up-to-date")
7981
pflag.BoolVarP(&force, "force", "f", false, "forces execution even when the task is up-to-date")
8082
pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task")
@@ -148,8 +150,8 @@ func main() {
148150
return
149151
}
150152

151-
if list {
152-
e.PrintTasksHelp()
153+
if list || listAll {
154+
e.PrintTasksHelp(listAll)
153155
return
154156
}
155157

help.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,19 @@ import (
99
"github.com/go-task/task/v3/taskfile"
1010
)
1111

12-
// PrintTasksHelp prints help os tasks that have a description
13-
func (e *Executor) PrintTasksHelp() {
14-
tasks := e.tasksWithDesc()
12+
// PrintTasksHelp prints tasks' help.
13+
// Behavior is governed by listAll. When false, only tasks with descriptions are reported.
14+
// When true, all tasks are reported with descriptions shown where available.
15+
func (e *Executor) PrintTasksHelp(listAll bool) {
16+
var tasks []*taskfile.Task
17+
if listAll == true {
18+
tasks = e.taskNames()
19+
} else {
20+
tasks = e.tasksWithDesc()
21+
}
22+
1523
if len(tasks) == 0 {
24+
// TODO: This message should be more informative. Maybe a hint to try -la for showing all?
1625
e.Logger.Outf(logger.Yellow, "task: No tasks with description available")
1726
return
1827
}
@@ -26,6 +35,15 @@ func (e *Executor) PrintTasksHelp() {
2635
w.Flush()
2736
}
2837

38+
func (e *Executor) taskNames() (tasks []*taskfile.Task) {
39+
tasks = make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
40+
for _, task := range e.Taskfile.Tasks {
41+
tasks = append(tasks, task)
42+
}
43+
sort.Slice(tasks, func(i, j int) bool { return tasks[i].Task < tasks[j].Task })
44+
return
45+
}
46+
2947
func (e *Executor) tasksWithDesc() (tasks []*taskfile.Task) {
3048
tasks = make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
3149
for _, task := range e.Taskfile.Tasks {

task.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
6464
for _, c := range calls {
6565
if _, ok := e.Taskfile.Tasks[c.Task]; !ok {
6666
// FIXME: move to the main package
67-
e.PrintTasksHelp()
67+
// FIXME: ([email protected]) changed the PrintTasksHelp signature to support show all/some.
68+
// False preserves original behavior, but should be reviewed.
69+
e.PrintTasksHelp(false)
6870
return &taskNotFoundError{taskName: c.Task}
6971
}
7072
}

0 commit comments

Comments
 (0)