mirror of
https://github.com/go-task/task.git
synced 2025-08-10 22:42:19 +02:00
refactor with support for --list and --list-all
This commit is contained in:
@@ -158,10 +158,12 @@ func main() {
|
|||||||
|
|
||||||
OutputStyle: output,
|
OutputStyle: output,
|
||||||
}
|
}
|
||||||
if list && silent {
|
|
||||||
e.PrintTaskNames()
|
if (list || listAll) && silent {
|
||||||
|
e.ListTaskNames(listAll)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := e.Setup(); err != nil {
|
if err := e.Setup(); err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
37
help.go
37
help.go
@@ -74,32 +74,29 @@ func (e *Executor) tasksWithDesc() (tasks []*taskfile.Task) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// PrintTaskNames prints only the task names in a taskfile.
|
// PrintTaskNames prints only the task names in a Taskfile.
|
||||||
func (e *Executor) PrintTaskNames() error {
|
// 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) {
|
||||||
// if called from cmd/task.go, e.Taskfile has not yet been parsed
|
// if called from cmd/task.go, e.Taskfile has not yet been parsed
|
||||||
if nil == e.Taskfile {
|
if nil == e.Taskfile && e.readTaskfile() != nil {
|
||||||
err := e.readTaskfile()
|
return
|
||||||
if nil != err {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// use stdout if no output defined
|
||||||
var w io.Writer = os.Stdout
|
var w io.Writer = os.Stdout
|
||||||
if nil != e.Stdout {
|
if e.Stdout != nil {
|
||||||
w = e.Stdout
|
w = e.Stdout
|
||||||
}
|
}
|
||||||
|
// create a string slice from all map values (*taskfile.Task)
|
||||||
// create a slice from all map values
|
s := make([]string, 0, len(e.Taskfile.Tasks))
|
||||||
task := make([]*taskfile.Task, 0, len(e.Taskfile.Tasks))
|
|
||||||
for _, t := range e.Taskfile.Tasks {
|
for _, t := range e.Taskfile.Tasks {
|
||||||
task = append(task, t)
|
if allTasks || t.Desc != "" {
|
||||||
|
s = append(s, strings.TrimRight(t.Task, ":"))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// sort and print all task names
|
||||||
sort.Slice(task,
|
sort.Strings(s)
|
||||||
func(i, j int) bool {
|
for _, t := range s {
|
||||||
return task[i].Task < task[j].Task
|
fmt.Fprintln(w, t)
|
||||||
})
|
|
||||||
for _, t := range task {
|
|
||||||
fmt.Fprintf(w, "%s\n", strings.TrimSuffix(t.Task, ":"))
|
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user