utils/convids/methods.go

66 lines
1.1 KiB
Go
Raw Normal View History

2024-10-23 15:56:56 -05:00
package convids
import (
2024-10-23 21:24:10 -05:00
"errors"
2024-10-23 15:56:56 -05:00
"fmt"
"iter"
2024-10-23 21:24:10 -05:00
"regexp"
2024-10-23 15:56:56 -05:00
"slices"
2024-10-23 21:24:10 -05:00
"strings"
2024-10-23 15:56:56 -05:00
)
2024-10-23 21:24:10 -05:00
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
2024-10-23 15:56:56 -05:00
}
2024-10-23 21:24:10 -05:00
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
}
2024-10-23 15:56:56 -05:00
for _, show := range d.Config.Shows {
2024-10-23 21:24:10 -05:00
sh := (*d.Shows)[show]
if sh == nil || len(*sh) < 1 {
continue
}
2024-10-23 15:56:56 -05:00
if !silent {
fmt.Println("\nChecking", show, "shows\n")
}
2024-10-23 21:24:10 -05:00
for s := range sh.All() {
if s == nil {
continue
}
2024-10-23 15:56:56 -05:00
if !yield(s) {
return
}
}
}
}
}