1
0
mirror of https://github.com/go-task/task.git synced 2025-06-23 00:38:19 +02:00

feat: wildcard matching of task names

This commit is contained in:
Pete Davison
2024-01-27 15:51:43 +00:00
parent 1ef5cf71d0
commit 9a3d2bc3aa
3 changed files with 67 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package ast
import (
"fmt"
"regexp"
"strings"
"gopkg.in/yaml.v3"
@ -51,6 +53,30 @@ func (t *Task) Name() string {
return t.Task
}
// WildcardMatch will check if the given string matches the name of the Task and returns any wildcard values.
func (t *Task) WildcardMatch(name string) (bool, []string) {
// Convert the name into a regex string
regexStr := fmt.Sprintf("^%s$", strings.ReplaceAll(t.Task, "*", "(.*)"))
regex := regexp.MustCompile(regexStr)
wildcards := regex.FindStringSubmatch(name)
wildcardCount := strings.Count(t.Task, "*")
// If there are no wildcards, return false
if len(wildcards) == 0 {
return false, nil
}
// Remove the first match, which is the full string
wildcards = wildcards[1:]
// If there are more/less wildcards than matches, return false
if len(wildcards) != wildcardCount {
return false, wildcards
}
return true, wildcards
}
func (t *Task) UnmarshalYAML(node *yaml.Node) error {
switch node.Kind {