🚧 WalkFiles
This commit is contained in:
parent
c711ed6567
commit
19206a9bf5
6 changed files with 157 additions and 16 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1 +1,2 @@
|
|||
build/
|
||||
.task/
|
||||
|
|
|
|||
37
Taskfile.yml
Normal file
37
Taskfile.yml
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# https://taskfile.dev
|
||||
|
||||
version: '3'
|
||||
|
||||
tasks:
|
||||
fmt:
|
||||
desc: Format go files
|
||||
sources:
|
||||
- "**/*.go"
|
||||
cmds:
|
||||
- go fmt ./...
|
||||
cmd-build:
|
||||
internal: true
|
||||
cmds:
|
||||
- go build -o build/ ./cmd/{{.CMD}}
|
||||
build-convids:
|
||||
desc: Builds the convids command
|
||||
sources:
|
||||
- cmd/convids/**/*.go
|
||||
- convids/**/*.go
|
||||
- internal/cli/*.go
|
||||
generates:
|
||||
- build/convids
|
||||
cmds:
|
||||
- task: cmd-build
|
||||
vars:
|
||||
CMD: convids
|
||||
build-all:
|
||||
desc: Builds all available commands
|
||||
sources:
|
||||
- "**/*.go"
|
||||
generates:
|
||||
- build/convids
|
||||
cmds:
|
||||
- task: cmd-build
|
||||
vars:
|
||||
CMD: "*"
|
||||
|
|
@ -7,7 +7,6 @@ import (
|
|||
"codeberg.org/danjones000/utils/convids"
|
||||
"codeberg.org/danjones000/utils/internal/cli"
|
||||
"github.com/spf13/pflag"
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
const dataPath = "shows.yml"
|
||||
|
|
@ -26,12 +25,7 @@ func main() {
|
|||
fmt.Println("looping")
|
||||
}
|
||||
|
||||
f, err := os.Open(dataPath)
|
||||
cli.HandleErr(err)
|
||||
|
||||
ydec := yaml.NewDecoder(f)
|
||||
var data convids.Data
|
||||
err = ydec.Decode(&data)
|
||||
data, err := convids.NewData(dataPath)
|
||||
cli.HandleErr(err)
|
||||
|
||||
fmt.Printf("Data: %+v\n", data)
|
||||
|
|
@ -39,4 +33,5 @@ func main() {
|
|||
for s := range data.AllShows(false) {
|
||||
fmt.Printf("Show: %+v\n", s)
|
||||
}
|
||||
fmt.Println("\nDone!")
|
||||
}
|
||||
|
|
|
|||
63
convids/logic.go
Normal file
63
convids/logic.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package convids
|
||||
|
||||
import (
|
||||
"os"
|
||||
fp "path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func NewData(path string) (*Data, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ydec := yaml.NewDecoder(f)
|
||||
var data Data
|
||||
err = ydec.Decode(&data)
|
||||
return &data, err
|
||||
}
|
||||
|
||||
func ensureExtRe(c *Config) (err error) {
|
||||
if c.extRe != nil {
|
||||
return nil
|
||||
}
|
||||
c.extRe, err = regexp.Compile("(" + strings.Join(c.Extensions, "|") + ")$")
|
||||
return
|
||||
}
|
||||
|
||||
func WalkFiles(d *Data, stopOnError, silent bool, cb func(s *Show, path string) error) (err error) {
|
||||
err = ensureExtRe(d.Config)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var files []os.DirEntry
|
||||
files, err = os.ReadDir(d.Config.Source)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for s := range d.AllShows(silent) {
|
||||
var found bool
|
||||
for _, file := range files {
|
||||
if file.IsDir() {
|
||||
continue
|
||||
}
|
||||
if !d.Config.extRe.MatchString(file.Name()) {
|
||||
continue
|
||||
}
|
||||
found, err = s.Match(file.Name())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
if found {
|
||||
err = cb(s, fp.Join(d.Config.Source, file.Name()))
|
||||
if err != nil && stopOnError {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -1,22 +1,61 @@
|
|||
package convids
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"iter"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (s Shows) All() iter.Seq[Show] {
|
||||
return slices.Values(s)
|
||||
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 (d Data) AllShows(silent bool) iter.Seq[Show] {
|
||||
return func(yield func(Show) bool) {
|
||||
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 d.Shows[show].All() {
|
||||
for s := range sh.All() {
|
||||
if s == nil {
|
||||
continue
|
||||
}
|
||||
if !yield(s) {
|
||||
return
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,23 +1,29 @@
|
|||
package convids
|
||||
|
||||
import "regexp"
|
||||
|
||||
type Data struct {
|
||||
Config Config
|
||||
Shows ShowMap
|
||||
Config *Config
|
||||
Shows *ShowMap
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Source string
|
||||
Extensions []string
|
||||
Shows []string
|
||||
|
||||
extRe *regexp.Regexp
|
||||
}
|
||||
|
||||
type ShowMap map[string]Shows
|
||||
type ShowMap map[string]*Shows
|
||||
|
||||
type Shows []Show
|
||||
type Shows []*Show
|
||||
|
||||
type Show struct {
|
||||
Folder string
|
||||
Pattern string
|
||||
Name string
|
||||
Anime bool
|
||||
|
||||
re *regexp.Regexp
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue