2017-03-19 20:18:18 +02:00
|
|
|
package task
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-07-01 23:05:46 +02:00
|
|
|
"io"
|
2022-04-01 02:37:06 +02:00
|
|
|
"log"
|
2021-07-01 23:05:46 +02:00
|
|
|
"os"
|
2017-03-19 20:18:18 +02:00
|
|
|
"sort"
|
2021-07-01 23:05:46 +02:00
|
|
|
"strings"
|
2017-03-19 20:18:18 +02:00
|
|
|
"text/tabwriter"
|
2018-02-17 18:22:18 +02:00
|
|
|
|
2020-08-16 20:48:19 +02:00
|
|
|
"github.com/go-task/task/v3/internal/logger"
|
2020-08-19 10:59:58 +02:00
|
|
|
"github.com/go-task/task/v3/taskfile"
|
2017-03-19 20:18:18 +02:00
|
|
|
)
|
|
|
|
|
2022-01-04 22:16:21 +02:00
|
|
|
// ListTasksWithDesc reports tasks that have a description spec.
|
|
|
|
func (e *Executor) ListTasksWithDesc() {
|
2022-01-04 22:19:38 +02:00
|
|
|
e.printTasks(false)
|
2022-01-04 22:16:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListAllTasks reports all tasks, with or without a description spec.
|
|
|
|
func (e *Executor) ListAllTasks() {
|
2022-01-04 22:19:38 +02:00
|
|
|
e.printTasks(true)
|
2022-01-04 22:16:21 +02:00
|
|
|
}
|
|
|
|
|
2022-01-04 22:19:38 +02:00
|
|
|
func (e *Executor) printTasks(listAll bool) {
|
2020-11-13 22:27:03 +02:00
|
|
|
var tasks []*taskfile.Task
|
2022-01-04 22:16:21 +02:00
|
|
|
if listAll {
|
2022-01-03 19:12:18 +02:00
|
|
|
tasks = e.allTaskNames()
|
2020-11-13 22:27:03 +02:00
|
|
|
} else {
|
|
|
|
tasks = e.tasksWithDesc()
|
|
|
|
}
|
|
|
|
|
2017-03-19 20:18:18 +02:00
|
|
|
if len(tasks) == 0 {
|
2022-01-04 22:16:21 +02:00
|
|
|
if listAll {
|
|
|
|
e.Logger.Outf(logger.Yellow, "task: No tasks available")
|
|
|
|
} else {
|
|
|
|
e.Logger.Outf(logger.Yellow, "task: No tasks with description available. Try --list-all to list all tasks")
|
|
|
|
}
|
2017-03-19 20:18:18 +02:00
|
|
|
return
|
|
|
|
}
|
2019-05-26 23:36:39 +02:00
|
|
|
e.Logger.Outf(logger.Default, "task: Available tasks for this project:")
|
2017-03-19 20:18:18 +02:00
|
|
|
|
|
|
|
// Format in tab-separated columns with a tab stop of 8.
|
2022-09-29 19:13:12 +02:00
|
|
|
w := tabwriter.NewWriter(e.Stdout, 0, 8, 6, ' ', 0)
|
2017-03-19 20:18:18 +02:00
|
|
|
for _, task := range tasks {
|
2022-09-29 19:13:12 +02:00
|
|
|
e.Logger.FOutf(w, logger.Yellow, "* ")
|
2022-10-15 00:28:05 +02:00
|
|
|
e.Logger.FOutf(w, logger.Green, task.Task)
|
2022-09-29 19:13:12 +02:00
|
|
|
e.Logger.FOutf(w, logger.Default, ": \t%s", task.Desc)
|
2022-10-15 00:28:05 +02:00
|
|
|
if len(task.Aliases) > 0 {
|
|
|
|
e.Logger.FOutf(w, logger.Cyan, "\t(aliases: %s)", strings.Join(task.Aliases, ", "))
|
|
|
|
}
|
2022-09-29 19:13:12 +02:00
|
|
|
fmt.Fprint(w, "\n")
|
2017-03-19 20:18:18 +02:00
|
|
|
}
|
|
|
|
w.Flush()
|
|
|
|
}
|
|
|
|
|
2022-01-03 19:12:18 +02:00
|
|
|
func (e *Executor) allTaskNames() (tasks []*taskfile.Task) {
|
2020-11-13 22:27:03 +02:00
|
|
|
tasks = make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
|
|
|
|
for _, task := range e.Taskfile.Tasks {
|
2022-07-22 04:15:35 +02:00
|
|
|
if !task.Internal {
|
|
|
|
tasks = append(tasks, task)
|
|
|
|
}
|
2020-11-13 22:27:03 +02:00
|
|
|
}
|
|
|
|
sort.Slice(tasks, func(i, j int) bool { return tasks[i].Task < tasks[j].Task })
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-17 18:22:18 +02:00
|
|
|
func (e *Executor) tasksWithDesc() (tasks []*taskfile.Task) {
|
|
|
|
tasks = make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
|
2017-12-29 22:27:32 +02:00
|
|
|
for _, task := range e.Taskfile.Tasks {
|
2022-07-22 04:15:35 +02:00
|
|
|
if !task.Internal && task.Desc != "" {
|
2021-01-12 17:03:04 +02:00
|
|
|
compiledTask, err := e.FastCompiledTask(taskfile.Call{Task: task.Task})
|
2020-01-20 15:38:18 +02:00
|
|
|
if err == nil {
|
|
|
|
task = compiledTask
|
|
|
|
}
|
2017-12-29 22:27:32 +02:00
|
|
|
tasks = append(tasks, task)
|
2017-03-19 20:18:18 +02:00
|
|
|
}
|
|
|
|
}
|
2017-12-29 22:27:32 +02:00
|
|
|
sort.Slice(tasks, func(i, j int) bool { return tasks[i].Task < tasks[j].Task })
|
2017-03-19 20:18:18 +02:00
|
|
|
return
|
|
|
|
}
|
2021-07-01 23:05:46 +02:00
|
|
|
|
2022-10-02 17:49:38 +02:00
|
|
|
// ListTaskNames prints only the task names in a Taskfile.
|
2022-03-21 19:59:25 +02:00
|
|
|
// 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) {
|
2021-07-01 23:05:46 +02:00
|
|
|
// if called from cmd/task.go, e.Taskfile has not yet been parsed
|
2022-04-01 02:37:06 +02:00
|
|
|
if e.Taskfile == nil {
|
|
|
|
if err := e.readTaskfile(); err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
2021-07-01 23:05:46 +02:00
|
|
|
}
|
2022-03-21 19:59:25 +02:00
|
|
|
// use stdout if no output defined
|
2021-07-01 23:05:46 +02:00
|
|
|
var w io.Writer = os.Stdout
|
2022-03-21 19:59:25 +02:00
|
|
|
if e.Stdout != nil {
|
2021-07-01 23:05:46 +02:00
|
|
|
w = e.Stdout
|
|
|
|
}
|
2022-03-21 19:59:25 +02:00
|
|
|
// create a string slice from all map values (*taskfile.Task)
|
|
|
|
s := make([]string, 0, len(e.Taskfile.Tasks))
|
2021-07-01 23:05:46 +02:00
|
|
|
for _, t := range e.Taskfile.Tasks {
|
2022-07-24 17:54:23 +02:00
|
|
|
if (allTasks || t.Desc != "") && !t.Internal {
|
2022-03-21 19:59:25 +02:00
|
|
|
s = append(s, strings.TrimRight(t.Task, ":"))
|
|
|
|
}
|
2021-07-01 23:05:46 +02:00
|
|
|
}
|
2022-03-21 19:59:25 +02:00
|
|
|
// sort and print all task names
|
|
|
|
sort.Strings(s)
|
|
|
|
for _, t := range s {
|
|
|
|
fmt.Fprintln(w, t)
|
2021-07-01 23:05:46 +02:00
|
|
|
}
|
|
|
|
}
|