Implement "Isolate" commands

Commands that run alone one by one on reading and writing the file
This should be used on commands that will modify a large part of the
file (or generally large parts)
Since that can fuck up the indices of other commands when ran together
This commit is contained in:
2025-03-28 16:56:39 +01:00
parent b77224176b
commit 2c487bc443
2 changed files with 68 additions and 11 deletions

View File

@@ -18,6 +18,7 @@ type ModifyCommand struct {
Git bool `yaml:"git"`
Reset bool `yaml:"reset"`
LogLevel string `yaml:"loglevel"`
Isolate bool `yaml:"isolate"`
}
type CookFile []ModifyCommand
@@ -39,6 +40,7 @@ func (c *ModifyCommand) Validate() error {
// Ehh.. Not much better... Guess this wasn't the big deal
var matchesMemoTable map[string]bool = make(map[string]bool)
func Matches(path string, glob string) (bool, error) {
key := fmt.Sprintf("%s:%s", path, glob)
if matches, ok := matchesMemoTable[key]; ok {
@@ -53,10 +55,22 @@ func Matches(path string, glob string) (bool, error) {
return matches, nil
}
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) {
type FileCommandAssociation struct {
File string
IsolateCommands []ModifyCommand
Commands []ModifyCommand
}
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string]FileCommandAssociation, error) {
associationCount := 0
fileCommands := make(map[string][]ModifyCommand)
fileCommands := make(map[string]FileCommandAssociation)
for _, file := range files {
fileCommands[file] = FileCommandAssociation{
File: file,
IsolateCommands: []ModifyCommand{},
Commands: []ModifyCommand{},
}
for _, command := range commands {
for _, glob := range command.Files {
matches, err := Matches(file, glob)
@@ -66,15 +80,25 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s
}
if matches {
logger.Debug("Found match for file %q and command %q", file, command.Regex)
fileCommands[file] = append(fileCommands[file], command)
association := fileCommands[file]
if command.Isolate {
association.IsolateCommands = append(association.IsolateCommands, command)
} else {
association.Commands = append(association.Commands, command)
}
fileCommands[file] = association
associationCount++
}
}
}
logger.Debug("Found %d commands for file %q", len(fileCommands[file]), file)
if len(fileCommands[file]) == 0 {
logger.Debug("Found %d commands for file %q", len(fileCommands[file].Commands), file)
if len(fileCommands[file].Commands) == 0 {
logger.Info("No commands found for file %q", file)
}
if len(fileCommands[file].IsolateCommands) > 0 {
logger.Info("Found %d isolate commands for file %q", len(fileCommands[file].IsolateCommands), file)
}
}
logger.Info("Found %d associations between %d files and %d commands", associationCount, len(files), len(commands))
return fileCommands, nil
@@ -234,4 +258,4 @@ func FilterCommands(commands []ModifyCommand, filter string) []ModifyCommand {
}
}
return filteredCommands
}
}