Compare commits
	
		
			No commits in common. "ec3db89d1a8046a64df5c078ecef91cde28c5da0" and "c711ed65673dc43c70a9d875f6528997d18e917d" have entirely different histories.
		
	
	
		
			
				ec3db89d1a
			
			...
			
				c711ed6567
			
		
	
		
					 6 changed files with 21 additions and 175 deletions
				
			
		
							
								
								
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							
							
						
						
									
										1
									
								
								.gitignore
									
										
									
									
										vendored
									
									
								
							| 
						 | 
					@ -1,2 +1 @@
 | 
				
			||||||
build/
 | 
					build/
 | 
				
			||||||
.task/
 | 
					 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										37
									
								
								Taskfile.yml
									
										
									
									
									
								
							
							
						
						
									
										37
									
								
								Taskfile.yml
									
										
									
									
									
								
							| 
						 | 
					@ -1,37 +0,0 @@
 | 
				
			||||||
# 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,6 +7,7 @@ import (
 | 
				
			||||||
	"codeberg.org/danjones000/utils/convids"
 | 
						"codeberg.org/danjones000/utils/convids"
 | 
				
			||||||
	"codeberg.org/danjones000/utils/internal/cli"
 | 
						"codeberg.org/danjones000/utils/internal/cli"
 | 
				
			||||||
	"github.com/spf13/pflag"
 | 
						"github.com/spf13/pflag"
 | 
				
			||||||
 | 
						"gopkg.in/yaml.v3"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const dataPath = "shows.yml"
 | 
					const dataPath = "shows.yml"
 | 
				
			||||||
| 
						 | 
					@ -25,12 +26,17 @@ func main() {
 | 
				
			||||||
		fmt.Println("looping")
 | 
							fmt.Println("looping")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	data, err := convids.NewData(dataPath)
 | 
						f, err := os.Open(dataPath)
 | 
				
			||||||
	cli.HandleErr(err)
 | 
						cli.HandleErr(err)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	convids.WalkFiles(data, false, false, func(s *convids.Show, path string) error {
 | 
						ydec := yaml.NewDecoder(f)
 | 
				
			||||||
		fmt.Printf("%s for %+v\n", path, s)
 | 
						var data convids.Data
 | 
				
			||||||
		return nil
 | 
						err = ydec.Decode(&data)
 | 
				
			||||||
	})
 | 
						cli.HandleErr(err)
 | 
				
			||||||
	fmt.Println("\nDone!")
 | 
					
 | 
				
			||||||
 | 
						fmt.Printf("Data: %+v\n", data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for s := range data.AllShows(false) {
 | 
				
			||||||
 | 
							fmt.Printf("Show: %+v\n", s)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,63 +0,0 @@
 | 
				
			||||||
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,75 +1,22 @@
 | 
				
			||||||
package convids
 | 
					package convids
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
					 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"iter"
 | 
						"iter"
 | 
				
			||||||
	"regexp"
 | 
					 | 
				
			||||||
	"slices"
 | 
						"slices"
 | 
				
			||||||
	"strings"
 | 
					 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var ErrNoName = errors.New("missing name")
 | 
					func (s Shows) All() iter.Seq[Show] {
 | 
				
			||||||
 | 
						return slices.Values(s)
 | 
				
			||||||
var animePattern = `^\[.+\] `
 | 
					 | 
				
			||||||
var animeRegexp = regexp.MustCompile(animePattern)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
func (s *Show) Match(path string) (bool, error) {
 | 
					 | 
				
			||||||
	if s.Pattern != "" {
 | 
					 | 
				
			||||||
		return s.matchRegexp(path)
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if s.Name == "" {
 | 
					 | 
				
			||||||
		return false, ErrNoName
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	if s.Anime {
 | 
					 | 
				
			||||||
		if !animeRegexp.MatchString(path) {
 | 
					 | 
				
			||||||
			return false, nil
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		path = animeRegexp.ReplaceAllString(path, "")
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	return strings.HasPrefix(path, s.Name), nil
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (s *Show) matchRegexp(path string) (f bool, err error) {
 | 
					func (d Data) AllShows(silent bool) iter.Seq[Show] {
 | 
				
			||||||
	if s.re == nil {
 | 
						return func(yield func(Show) bool) {
 | 
				
			||||||
		p := s.Pattern
 | 
					 | 
				
			||||||
		if s.Anime {
 | 
					 | 
				
			||||||
			if strings.HasPrefix(p, "^") {
 | 
					 | 
				
			||||||
				p = strings.TrimPrefix(p, "^")
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			p = animePattern + p
 | 
					 | 
				
			||||||
		} else 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 {
 | 
							for _, show := range d.Config.Shows {
 | 
				
			||||||
			sh := (*d.Shows)[show]
 | 
					 | 
				
			||||||
			if sh == nil || len(*sh) < 1 {
 | 
					 | 
				
			||||||
				continue
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
			if !silent {
 | 
								if !silent {
 | 
				
			||||||
				fmt.Println("\nChecking", show, "shows\n")
 | 
									fmt.Println("\nChecking", show, "shows\n")
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
			for s := range sh.All() {
 | 
								for s := range d.Shows[show].All() {
 | 
				
			||||||
				if s == nil {
 | 
					 | 
				
			||||||
					continue
 | 
					 | 
				
			||||||
				}
 | 
					 | 
				
			||||||
				if !yield(s) {
 | 
									if !yield(s) {
 | 
				
			||||||
					return
 | 
										return
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,29 +1,23 @@
 | 
				
			||||||
package convids
 | 
					package convids
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import "regexp"
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
type Data struct {
 | 
					type Data struct {
 | 
				
			||||||
	Config *Config
 | 
						Config Config
 | 
				
			||||||
	Shows  *ShowMap
 | 
						Shows  ShowMap
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Config struct {
 | 
					type Config struct {
 | 
				
			||||||
	Source     string
 | 
						Source     string
 | 
				
			||||||
	Extensions []string
 | 
						Extensions []string
 | 
				
			||||||
	Shows      []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 {
 | 
					type Show struct {
 | 
				
			||||||
	Folder  string
 | 
						Folder  string
 | 
				
			||||||
	Pattern string
 | 
						Pattern string
 | 
				
			||||||
	Name    string
 | 
						Name    string
 | 
				
			||||||
	Anime   bool
 | 
						Anime   bool
 | 
				
			||||||
 | 
					 | 
				
			||||||
	re *regexp.Regexp
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue