diff --git a/main.go b/main.go index c36db48..a1db185 100644 --- a/main.go +++ b/main.go @@ -27,12 +27,13 @@ type GlobalStats struct { var stats GlobalStats = GlobalStats{} var ( - gitFlag = flag.Bool("git", false, "Use git to manage files") - resetFlag = flag.Bool("reset", false, "Reset files to their original state") - logLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE") - cookfile = flag.String("cook", "**/cook.yml", "Path to cook config files, can be globbed") - repo *git.Repository - worktree *git.Worktree + gitFlag = flag.Bool("git", false, "Use git to manage files") + resetFlag = flag.Bool("reset", false, "Reset files to their original state") + logLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE") + cookfile = flag.String("cook", "**/cook.yml", "Path to cook config files, can be globbed") + parallelfiles = flag.Int("P", 100, "Number of files to process in parallel") + repo *git.Repository + worktree *git.Worktree ) type ModifyCommand struct { @@ -89,6 +90,8 @@ func main() { logger.Init(level) logger.Info("Initializing with log level: %s", level.String()) + // The plan is: + // Load all commands commands := []ModifyCommand{} logger.Info("Loading commands from cook files: %s", *cookfile) newcommands, err := LoadCommandsFromCookFiles(*cookfile) @@ -112,11 +115,46 @@ func main() { commands = append(commands, newcommands...) logger.Info("Now total commands: %d", len(commands)) - // The plan is: - // Load all commands // Then aggregate all the globs and deduplicate them + logger.Info("Aggregating file patterns for %d commands", len(commands)) + globs := make(map[string]struct{}) + for _, command := range commands { + for _, glob := range command.Files { + globs[glob] = struct{}{} + } + } + // Resolve all the files for all the globs - // Read each file into memory (could be much? Probably not... + logger.Info("Found %d unique file patterns", len(globs)) + files, err := ExpandGLobs(globs) + if err != nil { + logger.Error("Failed to expand file patterns: %v", err) + flag.Usage() + return + } + logger.Info("Found %d files to process", len(files)) + + associationCount := 0 + fileCommands := make(map[string][]ModifyCommand) + for _, file := range files { + for _, command := range commands { + for _, glob := range command.Files { + // TODO: Maybe memoize this function call + matches, err := doublestar.Match(glob, file) + if err != nil { + logger.Error("Failed to match glob %s with file %s: %v", glob, file, err) + continue + } + if matches { + logger.Debug("Found match for file %q and command %q", file, command.Pattern) + fileCommands[file] = append(fileCommands[file], command) + associationCount++ + } + } + } + logger.Debug("Found %d commands for file %q", len(fileCommands[file]), file) + } + logger.Info("Found %d associations between %d files and %d commands", associationCount, len(files), len(commands)) // Somehow connect files to commands via globs.. // Maybe do.... // For each file check every glob of every command @@ -133,22 +171,6 @@ func main() { // TODO: Maybe even figure out how to run individual commands...? // TODO: What to do with git? Figure it out .... - logger.Info("Expanding file patterns for %d commands", len(commands)) - globs := make(map[string]struct{}) - for _, command := range commands { - for _, glob := range command.Files { - globs[glob] = struct{}{} - } - } - logger.Info("Found %d unique file patterns", len(globs)) - files, err := ExpandGLobs(globs) - if err != nil { - logger.Error("Failed to expand file patterns: %v", err) - flag.Usage() - return - } - logger.Info("Found %d files to process", len(files)) - // if *gitFlag { // logger.Info("Git integration enabled, setting up git repository") // err := setupGit()