| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | package convids | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 	"context" | 
					
						
							| 
									
										
										
										
											2024-11-04 15:50:34 -06:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2024-11-04 15:20:09 -06:00
										 |  |  | 	"fmt" | 
					
						
							|  |  |  | 	"io" | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | 	"os" | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 	"os/exec" | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | 	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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 15:20:09 -06:00
										 |  |  | type ShowWalker func(show *Show, path string) error | 
					
						
							|  |  |  | type GroupPrinter func(name string, group Shows) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-04 15:20:09 -06:00
										 |  |  | func WalkFiles(d *Data, stopOnError bool, gp GroupPrinter, cb ShowWalker) (err error) { | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | 	err = ensureExtRe(d.Config) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-11-04 15:20:09 -06:00
										 |  |  | 	if cb == nil { | 
					
						
							|  |  |  | 		cb = DryRun(os.Stdout) | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 	count := 0 | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | 	var files []os.DirEntry | 
					
						
							|  |  |  | 	files, err = os.ReadDir(d.Config.Source) | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-11-04 15:20:09 -06:00
										 |  |  | 	for s := range d.AllShows(gp) { | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | 		for _, file := range files { | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 			processed, processErr, cbErr := processFile(d.Config, cb, s, file) | 
					
						
							|  |  |  | 			if !processed && processErr == nil && cbErr == nil { | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 			if errors.Is(processErr, ErrSkipped) { | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | 				continue | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 			if processErr != nil { | 
					
						
							|  |  |  | 				return processErr | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 			if cbErr != nil && stopOnError { | 
					
						
							|  |  |  | 				return cbErr | 
					
						
							| 
									
										
										
										
											2024-11-04 15:50:34 -06:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 			if cbErr == nil && processed { | 
					
						
							|  |  |  | 				count++ | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 			if cbErr != nil { | 
					
						
							|  |  |  | 				err = cbErr | 
					
						
							| 
									
										
										
										
											2024-11-04 15:50:34 -06:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			break | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 	if err == nil && count < 1 { | 
					
						
							|  |  |  | 		fmt.Println("Found nothing") | 
					
						
							|  |  |  | 		return ErrNoFiles | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return | 
					
						
							| 
									
										
										
										
											2024-10-23 21:24:10 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2024-11-04 15:20:09 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | func DryRun(out io.Writer) ShowWalker { | 
					
						
							|  |  |  | 	return func(s *Show, path string) error { | 
					
						
							|  |  |  | 		fmt.Fprintf(out, "Saving %s to %s\n", path, s.Folder) | 
					
						
							| 
									
										
										
										
											2024-11-07 05:56:06 -06:00
										 |  |  | 		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() | 
					
						
							| 
									
										
										
										
											2024-11-04 15:20:09 -06:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func PrintGroupName(out io.Writer) GroupPrinter { | 
					
						
							|  |  |  | 	return func(name string, group Shows) { | 
					
						
							|  |  |  | 		fmt.Fprintf(out, "Checking %s shows\n\n", name) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |