-
-
Notifications
You must be signed in to change notification settings - Fork 896
Expand file tree
/
Copy pathhelp.go
More file actions
317 lines (287 loc) · 9.22 KB
/
Copy pathhelp.go
File metadata and controls
317 lines (287 loc) · 9.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
package task
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"strings"
"github.com/Ladicle/tabwriter"
"golang.org/x/sync/errgroup"
"github.com/go-task/task/v3/internal/editors"
"github.com/go-task/task/v3/internal/fingerprint"
"github.com/go-task/task/v3/internal/listing"
"github.com/go-task/task/v3/internal/logger"
"github.com/go-task/task/v3/internal/sort"
"github.com/go-task/task/v3/taskfile/ast"
)
// ListOptions collects list-related options
type ListOptions struct {
ListOnlyTasksWithDescriptions bool
ListAllTasks bool
FormatTaskListAsJSON bool
NoStatus bool
Nested bool
Long bool
Tree bool
Filter string
SortMode string
}
// NewListOptions creates a new ListOptions instance
func NewListOptions(list, listAll, listAsJson, noStatus, nested, long, tree bool, filter, sortMode string) ListOptions {
return ListOptions{
ListOnlyTasksWithDescriptions: list,
ListAllTasks: listAll,
FormatTaskListAsJSON: listAsJson,
NoStatus: noStatus,
Nested: nested,
Long: long,
Tree: tree,
Filter: filter,
SortMode: sortMode,
}
}
// ShouldListTasks returns true if one of the options to list tasks has been set to true
func (o ListOptions) ShouldListTasks() bool {
return o.ListOnlyTasksWithDescriptions || o.ListAllTasks
}
// Filters returns the slice of FilterFunc which filters a list
// of ast.Task according to the given ListOptions
func (o ListOptions) Filters() []FilterFunc {
filters := []FilterFunc{FilterOutInternal}
if o.ListOnlyTasksWithDescriptions {
filters = append(filters, FilterOutNoDesc)
}
return filters
}
// 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.
func (e *Executor) ListTasks(o ListOptions) (bool, error) {
tasks, err := e.GetTaskList(o.Filters()...)
if err != nil {
return false, err
}
if o.Filter != "" {
tasks = listing.FilterTasks(tasks, o.Filter)
}
if o.FormatTaskListAsJSON {
output, err := e.ToEditorOutput(tasks, o.NoStatus, o.Nested, o.Long)
if err != nil {
return false, err
}
encoder := json.NewEncoder(e.Stdout)
encoder.SetIndent("", " ")
if err := encoder.Encode(output); err != nil {
return false, err
}
return len(tasks) > 0, nil
}
if len(tasks) == 0 {
if o.Filter != "" {
e.Logger.Outf(logger.Yellow, "task: No tasks matching %q\n", o.Filter)
} else if o.ListOnlyTasksWithDescriptions {
e.Logger.Outf(logger.Yellow, "task: No tasks with description available. Try --list-all to list all tasks\n")
} else if o.ListAllTasks {
e.Logger.Outf(logger.Yellow, "task: No tasks available\n")
}
return false, nil
}
if o.Tree {
return e.listTasksTree(tasks, o)
}
e.Logger.Outf(logger.Default, "task: Available tasks for this project:\n")
// Format in tab-separated columns with a tab stop of 8.
w := tabwriter.NewWriter(e.Stdout, 0, 8, 6, ' ', 0)
for _, task := range tasks {
e.Logger.FOutf(w, logger.Yellow, "* ")
e.writeHighlighted(w, logger.Green, task.Task, o.Filter)
desc := strings.ReplaceAll(task.Desc, "\n", " ")
e.Logger.FOutf(w, logger.Default, ": \t%s", desc)
if len(task.Aliases) > 0 {
e.Logger.FOutf(w, logger.Cyan, "\t(aliases: %s)", strings.Join(task.Aliases, ", "))
}
_, _ = fmt.Fprint(w, "\n")
e.writeTaskDetails(w, task, " \t", o.Long)
}
if err := w.Flush(); err != nil {
return false, err
}
return true, nil
}
func (e *Executor) writeHighlighted(w io.Writer, baseColor logger.Color, text, filter string) {
if filter == "" || listing.IsGlobPattern(filter) {
e.Logger.FOutf(w, baseColor, "%s", text)
return
}
idx := strings.Index(strings.ToLower(text), strings.ToLower(filter))
if idx == -1 {
e.Logger.FOutf(w, baseColor, "%s", text)
return
}
e.Logger.FOutf(w, baseColor, "%s", text[:idx])
e.Logger.FOutf(w, logger.Bold, "%s", text[idx:idx+len(filter)])
e.Logger.FOutf(w, baseColor, "%s", text[idx+len(filter):])
}
func (e *Executor) writeTaskDetails(w io.Writer, task *ast.Task, indent string, long bool) {
if listing.HasRequires(task) {
e.Logger.FOutf(w, logger.Default, indent)
e.Logger.FOutf(w, logger.Yellow, "requires:")
e.Logger.FOutf(w, logger.Default, " %s\n", listing.FormatRequires(task.Requires))
}
if long {
if deps := listing.FormatDeps(task.Deps); deps != "" {
e.Logger.FOutf(w, logger.Default, indent)
e.Logger.FOutf(w, logger.Yellow, "deps:")
e.Logger.FOutf(w, logger.Default, " %s\n", deps)
}
if task.Summary != "" {
summary := strings.TrimSpace(strings.ReplaceAll(task.Summary, "\n", " "))
e.Logger.FOutf(w, logger.Default, indent)
e.Logger.FOutf(w, logger.Yellow, "summary:")
e.Logger.FOutf(w, logger.Default, " %s\n", summary)
}
}
}
// ListTaskNames prints only the task names in a Taskfile.
// 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) error {
// use stdout if no output defined
var w io.Writer = os.Stdout
if e.Stdout != nil {
w = e.Stdout
}
// Sort the tasks
if e.TaskSorter == nil {
e.TaskSorter = sort.AlphaNumericWithRootTasksFirst
}
// Create a list of task names
taskNames := make([]string, 0, e.Taskfile.Tasks.Len())
for task := range e.Taskfile.Tasks.Values(e.TaskSorter) {
if (allTasks || task.Desc != "") && !task.Internal {
taskNames = append(taskNames, strings.TrimRight(task.Task, ":"))
for _, alias := range task.Aliases {
taskNames = append(taskNames, strings.TrimRight(alias, ":"))
}
}
}
for _, t := range taskNames {
fmt.Fprintln(w, t)
}
return nil
}
func (e *Executor) listTasksTree(tasks []*ast.Task, o ListOptions) (bool, error) {
e.Logger.Outf(logger.Default, "task: Available tasks for this project:\n")
groups := listing.GroupByNamespace(tasks)
hasNamespaced := listing.HasNamespacedGroups(groups)
hasRoot := listing.HasRootGroup(groups)
showSeparator := hasNamespaced && hasRoot && (o.SortMode == "" || o.SortMode == "default")
// Move root group to end so namespaced groups appear first
if showSeparator {
for i, g := range groups {
if g.Namespace == "" && i < len(groups)-1 {
rootGroup := groups[i]
groups = append(groups[:i], groups[i+1:]...)
groups = append(groups, rootGroup)
break
}
}
}
w := tabwriter.NewWriter(e.Stdout, 0, 8, 6, ' ', 0)
firstGroup := true
for _, group := range groups {
isRoot := group.Namespace == ""
if !firstGroup {
_, _ = fmt.Fprint(w, "\n")
}
firstGroup = false
if isRoot && showSeparator {
e.Logger.FOutf(w, logger.Dim, " ─────\n\n")
}
if !isRoot {
e.Logger.FOutf(w, logger.Dim, " %s\n", group.Namespace)
}
for _, task := range group.Tasks {
name := group.LocalName(task)
desc := strings.ReplaceAll(task.Desc, "\n", " ")
indent := " "
if !isRoot {
indent = " "
}
nameColor := logger.Green
if task.Internal {
nameColor = logger.Dim
}
e.Logger.FOutf(w, nameColor, "%s", indent)
e.writeHighlighted(w, nameColor, name, o.Filter)
e.Logger.FOutf(w, logger.Default, ":\t%s", desc)
if len(task.Aliases) > 0 {
e.Logger.FOutf(w, logger.Cyan, "\t(aliases: %s)", strings.Join(task.Aliases, ", "))
}
_, _ = fmt.Fprint(w, "\n")
e.writeTaskDetails(w, task, indent+" \t", o.Long)
}
}
return true, w.Flush()
}
func (e *Executor) ToEditorOutput(tasks []*ast.Task, noStatus bool, nested bool, long bool) (*editors.Namespace, error) {
var g errgroup.Group
editorTasks := make([]editors.Task, len(tasks))
// Look over each task in parallel and turn it into an editor task
for i := range tasks {
g.Go(func() error {
var editorTask editors.Task
if long {
editorTask = editors.NewTaskLong(tasks[i])
} else {
editorTask = editors.NewTask(tasks[i])
}
if noStatus {
editorTasks[i] = editorTask
return nil
}
// Get the fingerprinting method to use
method := e.Taskfile.Method
if tasks[i].Method != "" {
method = tasks[i].Method
}
upToDate, err := fingerprint.IsTaskUpToDate(context.Background(), tasks[i],
fingerprint.WithMethod(method),
fingerprint.WithTempDir(e.TempDir.Fingerprint),
fingerprint.WithDry(e.Dry),
fingerprint.WithLogger(e.Logger),
)
if err != nil {
return err
}
editorTask.UpToDate = &upToDate
editorTasks[i] = editorTask
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
// Create the root namespace
var tasksLen int
if !nested {
tasksLen = len(editorTasks)
}
rootNamespace := &editors.Namespace{
Tasks: make([]editors.Task, tasksLen),
Location: e.Taskfile.Location,
}
// Recursively add namespaces to the root namespace or if nesting is
// disabled add them all to the root namespace
for i, task := range editorTasks {
taskNamespacePath := strings.Split(task.Task, ast.NamespaceSeparator)
if nested {
rootNamespace.AddNamespace(taskNamespacePath, task)
} else {
rootNamespace.Tasks[i] = task
}
}
return rootNamespace, g.Wait()
}