Skip to content

Commit 00a90d1

Browse files
committed
Merge branch 'f/list-all' of https://github.com/therealkevinard/task into therealkevinard-f/list-all
2 parents d6c1855 + 42702e8 commit 00a90d1

File tree

5 files changed

+102
-6
lines changed

5 files changed

+102
-6
lines changed

cmd/task/task.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ func main() {
6060
helpFlag bool
6161
init bool
6262
list bool
63+
listAll bool
6364
status bool
6465
force bool
6566
watch bool
@@ -79,6 +80,7 @@ func main() {
7980
pflag.BoolVarP(&helpFlag, "help", "h", false, "shows Task usage")
8081
pflag.BoolVarP(&init, "init", "i", false, "creates a new Taskfile.yaml in the current folder")
8182
pflag.BoolVarP(&list, "list", "l", false, "lists tasks with description of current Taskfile")
83+
pflag.BoolVarP(&listAll, "list-all", "a", false, "list tasks with or without a description")
8284
pflag.BoolVar(&status, "status", false, "exits with non-zero exit code if any of the given tasks is not up-to-date")
8385
pflag.BoolVarP(&force, "force", "f", false, "forces execution even when the task is up-to-date")
8486
pflag.BoolVarP(&watch, "watch", "w", false, "enables watch of the given task")
@@ -153,7 +155,11 @@ func main() {
153155
}
154156

155157
if list {
156-
e.PrintTasksHelp()
158+
e.ListTasksWithDesc()
159+
}
160+
161+
if listAll {
162+
e.ListAllTasks()
157163
return
158164
}
159165

help.go

Lines changed: 33 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.allTaskNames()
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) allTaskNames() (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 {
@@ -40,3 +58,15 @@ func (e *Executor) tasksWithDesc() (tasks []*taskfile.Task) {
4058
sort.Slice(tasks, func(i, j int) bool { return tasks[i].Task < tasks[j].Task })
4159
return
4260
}
61+
62+
// ListTasksWithDesc reports tasks that have a description spec.
63+
func (e *Executor) ListTasksWithDesc() {
64+
e.PrintTasksHelp(false)
65+
return
66+
}
67+
68+
// ListAllTasks reports all tasks, with or without a description spec.
69+
func (e *Executor) ListAllTasks() {
70+
e.PrintTasksHelp(true)
71+
return
72+
}

task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
6868
for _, c := range calls {
6969
if _, ok := e.Taskfile.Tasks[c.Task]; !ok {
7070
// FIXME: move to the main package
71-
e.PrintTasksHelp()
71+
e.ListTasksWithDesc()
7272
return &taskNotFoundError{taskName: c.Task}
7373
}
7474
}

task_test.go

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,10 +518,58 @@ func TestLabelInList(t *testing.T) {
518518
Stderr: &buff,
519519
}
520520
assert.NoError(t, e.Setup())
521-
e.PrintTasksHelp()
521+
e.PrintTasksHelp(false)
522522
assert.Contains(t, buff.String(), "foobar")
523523
}
524524

525+
// task -al case 1: listAll list all tasks
526+
func TestListAllShowsNoDesc(t *testing.T) {
527+
const dir = "testdata/list_mixed_desc"
528+
529+
var buff bytes.Buffer
530+
e := task.Executor{
531+
Dir: dir,
532+
Stdout: &buff,
533+
Stderr: &buff,
534+
}
535+
536+
assert.NoError(t, e.Setup())
537+
538+
var title string
539+
e.ListAllTasks()
540+
for _, title = range []string{
541+
"foo",
542+
"voo",
543+
"doo",
544+
} {
545+
assert.Contains(t, buff.String(), title)
546+
}
547+
}
548+
549+
// task -al case 2: !listAll list some tasks (only those with desc)
550+
func TestListCanListDescOnly(t *testing.T) {
551+
const dir = "testdata/list_mixed_desc"
552+
553+
var buff bytes.Buffer
554+
e := task.Executor{
555+
Dir: dir,
556+
Stdout: &buff,
557+
Stderr: &buff,
558+
}
559+
560+
assert.NoError(t, e.Setup())
561+
e.ListTasksWithDesc()
562+
563+
var title string
564+
assert.Contains(t, buff.String(), "foo")
565+
for _, title = range []string{
566+
"voo",
567+
"doo",
568+
} {
569+
assert.NotContains(t, buff.String(), title)
570+
}
571+
}
572+
525573
func TestStatusVariables(t *testing.T) {
526574
const dir = "testdata/status_vars"
527575

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
version: '3'
2+
3+
tasks:
4+
foo:
5+
label: "foobar"
6+
desc: "foo has desc and label"
7+
8+
voo:
9+
label: "voo has no desc"
10+
11+
doo:
12+
label: "doo has desc, no label"

0 commit comments

Comments
 (0)