utils/convids/methods.go
2024-10-23 21:24:10 -05:00

65 lines
1.1 KiB
Go

package convids
import (
"errors"
"fmt"
"iter"
"regexp"
"slices"
"strings"
)
var ErrNoName = errors.New("missing name")
func (s *Show) Match(path string) (bool, error) {
if s.Pattern != "" {
return s.matchRegexp(path)
}
if s.Name == "" {
return false, ErrNoName
}
return strings.HasPrefix(path, s.Name), nil
}
func (s *Show) matchRegexp(path string) (f bool, err error) {
if s.re == nil {
p := s.Pattern
if !strings.HasPrefix(p, "^") {
p = "^" + p
}
s.re, err = regexp.Compile(p)
if err != nil {
return
}
}
return s.re.MatchString(path), nil
}
func (ss *Shows) All() iter.Seq[*Show] {
return slices.Values(*ss)
}
func (d *Data) AllShows(silent bool) iter.Seq[*Show] {
return func(yield func(*Show) bool) {
if d.Config == nil {
return
}
for _, show := range d.Config.Shows {
sh := (*d.Shows)[show]
if sh == nil || len(*sh) < 1 {
continue
}
if !silent {
fmt.Println("\nChecking", show, "shows\n")
}
for s := range sh.All() {
if s == nil {
continue
}
if !yield(s) {
return
}
}
}
}
}