refactor: implement task list filtering #890
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR refactors the code used to fetch the list of tasks when outputting them to the CLI. This is used for the
--list
and--list-all
flags as well as when any errors are displayed to the user.Why?
This code has a lot of duplication in it and isn't very extensible. i.e. it requires a lot of custom code and bloat to add new features.
Shiny things
I've added a
GetTaskList
function to the executor (this nicely matches the newGetTask
function in #879). This function will return a sorted list of tasks from the task map and filter any tasks according to the list of given filters. These filters can be any function that matches the newFilterFunc
type.I have implemented the two filter functions that are currently required in the codebase:
FilterNoDesc
- Removes tasks with no descriptionFilterInternal
- Removes tasks that are marked as internalTo add a new filter, all you'd need to do is create a function that takes a single task, decides if it should be filtered or not and return true/false accordingly. This function should then be wrapped with the
Filter
helper function which does all the actual filtering for you.Note: The
Filter
function uses an in-place, zero-allocation method for filtering. This is much more efficient than creating a new slice for each filter and usingappend
many times. See this StackOverflow post for details.Sorting
This PR also happens to fix #806. The duplication of the task list sorting was what led me down this path in the first place. The sort function has been replaced with something that will put tasks without a
:
first.It's worth noting that #806 requested that the tasks in the root Taskfile get sorted at the top of the list. This will not necessarily happen as if you create a task called
foo:bar
in your root Taskfile, it will see it as a namespace. However, I think this behaviour is fine (good, even).Other
Worth noting that the error handling for there being no tasks has moved from
help.go
into thecmd/task.go
file. This is because functions aren't comparable and thereforeListTasks
can't check if theFilterNoDesc
function was given or not and decide which error to show.This has the knock-on effect that the
task: No tasks with description available. Try --list-all to list all tasks
error message will no longer display when an error occurs and there are no tasks with descriptions. I personally don't mind this as it felt like a side-effect anyway, but I wanted to gather thoughts from others.