Files
task/help.go

106 lines
2.5 KiB
Go
Raw Normal View History

package task
import (
"fmt"
"io"
"os"
"sort"
"strings"
"text/tabwriter"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/taskfile"
)
// ListTasksWithDesc reports tasks that have a description spec.
func (e *Executor) ListTasksWithDesc() {
2022-01-04 17:19:38 -03:00
e.printTasks(false)
return
}
// ListAllTasks reports all tasks, with or without a description spec.
func (e *Executor) ListAllTasks() {
2022-01-04 17:19:38 -03:00
e.printTasks(true)
return
}
2022-01-04 17:19:38 -03:00
func (e *Executor) printTasks(listAll bool) {
var tasks []*taskfile.Task
if listAll {
tasks = e.allTaskNames()
} else {
tasks = e.tasksWithDesc()
}
if len(tasks) == 0 {
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")
}
return
}
2019-05-26 18:36:39 -03:00
e.Logger.Outf(logger.Default, "task: Available tasks for this project:")
// Format in tab-separated columns with a tab stop of 8.
w := tabwriter.NewWriter(e.Stdout, 0, 8, 0, '\t', 0)
for _, task := range tasks {
fmt.Fprintf(w, "* %s: \t%s\n", task.Name(), task.Desc)
}
w.Flush()
}
func (e *Executor) allTaskNames() (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 {
if task.Desc != "" {
compiledTask, err := e.FastCompiledTask(taskfile.Call{Task: task.Task})
if err == nil {
task = compiledTask
}
tasks = append(tasks, task)
}
}
sort.Slice(tasks, func(i, j int) bool { return tasks[i].Task < tasks[j].Task })
return
}
// PrintTaskNames prints only the task names in a taskfile.
func (e *Executor) PrintTaskNames() error {
// if called from cmd/task.go, e.Taskfile has not yet been parsed
if nil == e.Taskfile {
err := e.readTaskfile()
if nil != err {
return err
}
}
var w io.Writer = os.Stdout
if nil != e.Stdout {
w = e.Stdout
}
// create a slice from all map values
task := make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
for _, t := range e.Taskfile.Tasks {
task = append(task, t)
}
sort.Slice(task,
func(i, j int) bool {
return task[i].Task < task[j].Task
})
for _, t := range task {
fmt.Fprintf(w, "%s\n", strings.TrimSuffix(t.Task, ":"))
}
return nil
}