Skip to content

Commit c6ff641

Browse files
committed
Merge branch 'list-task-names' of https://github.com/ardnew/task into ardnew-list-task-names
2 parents 350f74a + 9897f4b commit c6ff641

3 files changed

Lines changed: 49 additions & 2 deletions

File tree

cmd/task/task.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ func main() {
158158

159159
OutputStyle: output,
160160
}
161+
162+
if (list || listAll) && silent {
163+
e.ListTaskNames(listAll)
164+
return
165+
}
166+
161167
if err := e.Setup(); err != nil {
162168
log.Fatal(err)
163169
}

help.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package task
22

33
import (
44
"fmt"
5+
"io"
6+
"os"
57
"sort"
8+
"strings"
69
"text/tabwriter"
710

811
"github.com/go-task/task/v3/internal/logger"
@@ -70,3 +73,30 @@ func (e *Executor) tasksWithDesc() (tasks []*taskfile.Task) {
7073
sort.Slice(tasks, func(i, j int) bool { return tasks[i].Task < tasks[j].Task })
7174
return
7275
}
76+
77+
// PrintTaskNames prints only the task names in a Taskfile.
78+
// Only tasks with a non-empty description are printed if allTasks is false.
79+
// Otherwise, all task names are printed.
80+
func (e *Executor) ListTaskNames(allTasks bool) {
81+
// if called from cmd/task.go, e.Taskfile has not yet been parsed
82+
if nil == e.Taskfile && e.readTaskfile() != nil {
83+
return
84+
}
85+
// use stdout if no output defined
86+
var w io.Writer = os.Stdout
87+
if e.Stdout != nil {
88+
w = e.Stdout
89+
}
90+
// create a string slice from all map values (*taskfile.Task)
91+
s := make([]string, 0, len(e.Taskfile.Tasks))
92+
for _, t := range e.Taskfile.Tasks {
93+
if allTasks || t.Desc != "" {
94+
s = append(s, strings.TrimRight(t.Task, ":"))
95+
}
96+
}
97+
// sort and print all task names
98+
sort.Strings(s)
99+
for _, t := range s {
100+
fmt.Fprintln(w, t)
101+
}
102+
}

task.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,15 +104,26 @@ func (e *Executor) Run(ctx context.Context, calls ...taskfile.Call) error {
104104
return g.Wait()
105105
}
106106

107-
// Setup setups Executor's internal state
108-
func (e *Executor) Setup() error {
107+
// readTaskfile selects and parses the entrypoint.
108+
func (e *Executor) readTaskfile() error {
109+
// select the default entrypoint if not provided
110+
if e.Entrypoint == "" {
111+
e.Entrypoint = "Taskfile.yml"
112+
}
113+
109114
var err error
110115
e.Taskfile, err = read.Taskfile(&read.ReaderNode{
111116
Dir: e.Dir,
112117
Entrypoint: e.Entrypoint,
113118
Parent: nil,
114119
Optional: false,
115120
})
121+
return err
122+
}
123+
124+
// Setup setups Executor's internal state
125+
func (e *Executor) Setup() error {
126+
err := e.readTaskfile()
116127
if err != nil {
117128
return err
118129
}

0 commit comments

Comments
 (0)