Implement file command association

This commit is contained in:
2025-03-27 21:54:46 +01:00
parent 8d4db1da91
commit 8a40f463f7

60
main.go
View File

@@ -31,6 +31,7 @@ var (
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
)
@@ -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()