✨ convids seems to work properly now
This commit is contained in:
		
					parent
					
						
							
								0272aa3b22
							
						
					
				
			
			
				commit
				
					
						5e8f954a14
					
				
			
		
					 3 changed files with 99 additions and 34 deletions
				
			
		|  | @ -56,9 +56,7 @@ tasks: | ||||||
|   build-convids: |   build-convids: | ||||||
|     desc: Builds the convids command |     desc: Builds the convids command | ||||||
|     sources: |     sources: | ||||||
|       - cmd/convids/**/*.go |       - '**/*.go' | ||||||
|       - convids/**/*.go |  | ||||||
|       - cli/err/*go |  | ||||||
|     generates: |     generates: | ||||||
|       - build/convids |       - build/convids | ||||||
|     cmds: |     cmds: | ||||||
|  |  | ||||||
|  | @ -1,10 +1,14 @@ | ||||||
| package main | package main | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"context" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"os" | 	"os" | ||||||
| 
 | 
 | ||||||
|  | 	"codeberg.org/danjones000/utils/chill" | ||||||
|  | 	c "codeberg.org/danjones000/utils/cli/context" | ||||||
| 	e "codeberg.org/danjones000/utils/cli/err" | 	e "codeberg.org/danjones000/utils/cli/err" | ||||||
|  | 	"codeberg.org/danjones000/utils/cli/spin" | ||||||
| 	"codeberg.org/danjones000/utils/convids" | 	"codeberg.org/danjones000/utils/convids" | ||||||
| 	"github.com/spf13/pflag" | 	"github.com/spf13/pflag" | ||||||
| ) | ) | ||||||
|  | @ -22,19 +26,43 @@ func main() { | ||||||
| 		os.Exit(2) | 		os.Exit(2) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	*dryRun = true // @todo temp | 	ctx, done := c.SelfCancelingCotext(context.Background()) | ||||||
|  | 	defer done() | ||||||
|  | 
 | ||||||
| 	var walker convids.ShowWalker | 	var walker convids.ShowWalker | ||||||
| 	if *dryRun { | 	if *dryRun { | ||||||
|  | 		*loop = false | ||||||
| 		walker = convids.DryRun(os.Stdout) | 		walker = convids.DryRun(os.Stdout) | ||||||
| 	} | 	} else { | ||||||
|  | 		gs := convids.GetShow(ctx) | ||||||
| 
 | 
 | ||||||
| 	if *loop { | 		walker = func(s *convids.Show, path string) error { | ||||||
| 		fmt.Println("looping") | 			ct := chill.Chill(ctx) | ||||||
|  | 			msg := fmt.Sprintf("Waiting for CPU to cool, for %s", path) | ||||||
|  | 			sp := spin.Spin(ct, msg) | ||||||
|  | 			err := sp.Wait() | ||||||
|  | 			if err != nil { | ||||||
|  | 				return err | ||||||
|  | 			} | ||||||
|  | 			return gs(s, path) | ||||||
|  | 		} | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	data, err := convids.NewData(dataPath) | 	data, err := convids.NewData(dataPath) | ||||||
| 	e.HandleErr(err) | 	e.HandleErr(err) | ||||||
| 
 | 
 | ||||||
| 	convids.WalkFiles(data, false, convids.PrintGroupName(os.Stdout), walker) | 	groupPrinter := convids.PrintGroupName(os.Stdout) | ||||||
|  | 
 | ||||||
|  | 	if *loop { | ||||||
|  | 		fmt.Println("looping") | ||||||
|  | 		for err == nil { | ||||||
|  | 			err = convids.WalkFiles(data, false, groupPrinter, walker) | ||||||
|  | 			fmt.Println(err) | ||||||
|  | 		} | ||||||
|  | 	} else { | ||||||
|  | 		err = convids.WalkFiles(data, false, groupPrinter, walker) | ||||||
|  | 	} | ||||||
|  | 	e.HandleErr(err) | ||||||
|  | 
 | ||||||
| 	fmt.Println("Done!") | 	fmt.Println("Done!") | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -1,10 +1,12 @@ | ||||||
| package convids | package convids | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
|  | 	"context" | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| 	"os" | 	"os" | ||||||
|  | 	"os/exec" | ||||||
| 	fp "path/filepath" | 	fp "path/filepath" | ||||||
| 	"regexp" | 	"regexp" | ||||||
| 	"strings" | 	"strings" | ||||||
|  | @ -34,6 +36,41 @@ func ensureExtRe(c *Config) (err error) { | ||||||
| type ShowWalker func(show *Show, path string) error | type ShowWalker func(show *Show, path string) error | ||||||
| type GroupPrinter func(name string, group Shows) | type GroupPrinter func(name string, group Shows) | ||||||
| 
 | 
 | ||||||
|  | var ErrNoFiles = errors.New("no files processed") | ||||||
|  | var ErrSkipped = errors.New("skipped") | ||||||
|  | 
 | ||||||
|  | func processFile(conf *Config, walk ShowWalker, show *Show, file os.DirEntry) (processed bool, processError, cbError error) { | ||||||
|  | 	if file.IsDir() { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	if !conf.extRe.MatchString(file.Name()) { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	var found bool | ||||||
|  | 	found, processError = show.Match(file.Name()) | ||||||
|  | 	if processError != nil || !found { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	path := fp.Join(conf.Source, file.Name()) | ||||||
|  | 	for _, ext := range conf.Skip { | ||||||
|  | 		if !strings.HasPrefix(ext, ".") { | ||||||
|  | 			ext = "." + ext | ||||||
|  | 		} | ||||||
|  | 		skipper := path + ext | ||||||
|  | 		_, statErr := os.Stat(skipper) | ||||||
|  | 		if !errors.Is(statErr, os.ErrNotExist) { | ||||||
|  | 			return processed, ErrSkipped, cbError | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	cbError = walk(show, path) | ||||||
|  | 	if cbError == nil { | ||||||
|  | 		processed = true | ||||||
|  | 	} | ||||||
|  | 	return | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err error) { | func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err error) { | ||||||
| 	err = ensureExtRe(d.Config) | 	err = ensureExtRe(d.Config) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  | @ -42,55 +79,57 @@ func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err e | ||||||
| 	if cb == nil { | 	if cb == nil { | ||||||
| 		cb = DryRun(os.Stdout) | 		cb = DryRun(os.Stdout) | ||||||
| 	} | 	} | ||||||
|  | 	count := 0 | ||||||
| 	var files []os.DirEntry | 	var files []os.DirEntry | ||||||
| 	files, err = os.ReadDir(d.Config.Source) | 	files, err = os.ReadDir(d.Config.Source) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
| 	for s := range d.AllShows(gp) { | 	for s := range d.AllShows(gp) { | ||||||
| 		var found bool |  | ||||||
| 	start: |  | ||||||
| 		for _, file := range files { | 		for _, file := range files { | ||||||
| 			if file.IsDir() { | 			processed, processErr, cbErr := processFile(d.Config, cb, s, file) | ||||||
|  | 			if !processed && processErr == nil && cbErr == nil { | ||||||
| 				continue | 				continue | ||||||
| 			} | 			} | ||||||
| 			if !d.Config.extRe.MatchString(file.Name()) { | 			if errors.Is(processErr, ErrSkipped) { | ||||||
| 				continue | 				continue | ||||||
| 			} | 			} | ||||||
| 			found, err = s.Match(file.Name()) | 			if processErr != nil { | ||||||
| 			if err != nil { | 				return processErr | ||||||
| 				return |  | ||||||
| 			} | 			} | ||||||
| 			if !found { | 			if cbErr != nil && stopOnError { | ||||||
| 				continue | 				return cbErr | ||||||
| 			} | 			} | ||||||
| 
 | 			if cbErr == nil && processed { | ||||||
| 			path := fp.Join(d.Config.Source, file.Name()) | 				count++ | ||||||
| 			for _, ext := range d.Config.Skip { |  | ||||||
| 				if !strings.HasPrefix(ext, ".") { |  | ||||||
| 					ext = "." + ext |  | ||||||
| 			} | 			} | ||||||
| 				skipper := path + ext | 			if cbErr != nil { | ||||||
| 				_, err := os.Stat(skipper) | 				err = cbErr | ||||||
| 				if !errors.Is(err, os.ErrNotExist) { |  | ||||||
| 					break start |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 
 |  | ||||||
| 			err = cb(s, path) |  | ||||||
| 			if err != nil && stopOnError { |  | ||||||
| 				return |  | ||||||
| 			} | 			} | ||||||
| 			break | 			break | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| 	return nil | 	if err == nil && count < 1 { | ||||||
|  | 		fmt.Println("Found nothing") | ||||||
|  | 		return ErrNoFiles | ||||||
|  | 	} | ||||||
|  | 	return | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| func DryRun(out io.Writer) ShowWalker { | func DryRun(out io.Writer) ShowWalker { | ||||||
| 	return func(s *Show, path string) error { | 	return func(s *Show, path string) error { | ||||||
| 		fmt.Fprintf(out, "Saving %s to %s\n", path, s.Folder) | 		fmt.Fprintf(out, "Saving %s to %s\n", path, s.Folder) | ||||||
| 		return nil | 		return ErrSkipped | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func GetShow(ctx context.Context) ShowWalker { | ||||||
|  | 	return func(s *Show, path string) error { | ||||||
|  | 		cmd := exec.CommandContext(ctx, "get-shows", s.Folder, path) | ||||||
|  | 		cmd.Stdin = os.Stdin | ||||||
|  | 		cmd.Stdout = os.Stdout | ||||||
|  | 		cmd.Stderr = os.Stderr | ||||||
|  | 		return cmd.Run() | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue