ListTasks prints a list of tasks. Tasks that match the given filters will be excluded from the list. The function returns a boolean indicating whether tasks were found and an error if one was encountered while preparing the output.
(o ListOptions)
| 60 | // The function returns a boolean indicating whether tasks were found |
| 61 | // and an error if one was encountered while preparing the output. |
| 62 | func (e *Executor) ListTasks(o ListOptions) (bool, error) { |
| 63 | tasks, err := e.GetTaskList(o.Filters()...) |
| 64 | if err != nil { |
| 65 | return false, err |
| 66 | } |
| 67 | if o.FormatTaskListAsJSON { |
| 68 | output, err := e.ToEditorOutput(tasks, o.NoStatus, o.Nested) |
| 69 | if err != nil { |
| 70 | return false, err |
| 71 | } |
| 72 | |
| 73 | encoder := json.NewEncoder(e.Stdout) |
| 74 | encoder.SetIndent("", " ") |
| 75 | if err := encoder.Encode(output); err != nil { |
| 76 | return false, err |
| 77 | } |
| 78 | |
| 79 | return len(tasks) > 0, nil |
| 80 | } |
| 81 | if len(tasks) == 0 { |
| 82 | if o.ListOnlyTasksWithDescriptions { |
| 83 | e.Logger.Outf(logger.Yellow, "task: No tasks with description available. Try --list-all to list all tasks\n") |
| 84 | } else if o.ListAllTasks { |
| 85 | e.Logger.Outf(logger.Yellow, "task: No tasks available\n") |
| 86 | } |
| 87 | return false, nil |
| 88 | } |
| 89 | e.Logger.Outf(logger.Default, "task: Available tasks for this project:\n") |
| 90 | |
| 91 | // Format in tab-separated columns with a tab stop of 8. |
| 92 | w := tabwriter.NewWriter(e.Stdout, 0, 8, 6, ' ', 0) |
| 93 | for _, task := range tasks { |
| 94 | e.Logger.FOutf(w, logger.Yellow, "* ") |
| 95 | e.Logger.FOutf(w, logger.Green, task.Task) |
| 96 | desc := strings.ReplaceAll(task.Desc, "\n", " ") |
| 97 | e.Logger.FOutf(w, logger.Default, ": \t%s", desc) |
| 98 | if len(task.Aliases) > 0 { |
| 99 | e.Logger.FOutf(w, logger.Cyan, "\t(aliases: %s)", strings.Join(task.Aliases, ", ")) |
| 100 | } |
| 101 | _, _ = fmt.Fprint(w, "\n") |
| 102 | } |
| 103 | if err := w.Flush(); err != nil { |
| 104 | return false, err |
| 105 | } |
| 106 | return true, nil |
| 107 | } |
| 108 | |
| 109 | // ListTaskNames prints only the task names in a Taskfile. |
| 110 | // Only tasks with a non-empty description are printed if allTasks is false. |