diff --git a/main.go b/main.go index 4efa463..9921e8c 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "errors" "flag" "fmt" "os" @@ -180,26 +181,35 @@ func main() { } fileDataStr := string(fileData) - logger.Debug("Saving file %q to database", file) - err = db.SaveFile(file, fileData) - if err != nil { - logger.Error("Failed to save file %q to database: %v", file, err) - return - } - + isChanged := false logger.Debug("Running isolate commands for file %q", file) fileDataStr, err = RunIsolateCommands(association, file, fileDataStr, &fileMutex) - if err != nil { + if err != nil && err != NothingToDo { logger.Error("Failed to run isolate commands for file %q: %v", file, err) return } + if err != NothingToDo { + isChanged = true + } logger.Debug("Running other commands for file %q", file) fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex, commandLoggers) - if err != nil { + if err != nil && err != NothingToDo { logger.Error("Failed to run other commands for file %q: %v", file, err) return } + if err != NothingToDo { + isChanged = true + } + + if isChanged { + logger.Debug("Saving file %q to database", file) + err = db.SaveFile(file, fileData) + if err != nil { + logger.Error("Failed to save file %q to database: %v", file, err) + return + } + } logger.Debug("Writing file %q", file) err = os.WriteFile(file, []byte(fileDataStr), 0644) @@ -346,6 +356,8 @@ func CreateExampleConfig() { logger.Info("Wrote example_cook.yml") } +var NothingToDo = errors.New("nothing to do") + func RunOtherCommands(file string, fileDataStr string, association utils.FileCommandAssociation, fileMutex *sync.Mutex, commandLoggers map[string]*logger.Logger) (string, error) { // Aggregate all the modifications and execute them modifications := []utils.ReplaceCommand{} @@ -377,7 +389,7 @@ func RunOtherCommands(file string, fileDataStr string, association utils.FileCom if len(modifications) == 0 { logger.Warning("No modifications found for file %q", file) - return fileDataStr, nil + return fileDataStr, NothingToDo } // Sort commands in reverse order for safe replacements @@ -394,6 +406,7 @@ func RunOtherCommands(file string, fileDataStr string, association utils.FileCom } func RunIsolateCommands(association utils.FileCommandAssociation, file string, fileDataStr string, fileMutex *sync.Mutex) (string, error) { + anythingDone := false for _, isolateCommand := range association.IsolateCommands { logger.Info("Processing file %q with isolate command %q", file, isolateCommand.Regex) modifications, err := processor.ProcessRegex(fileDataStr, isolateCommand, file) @@ -406,6 +419,7 @@ func RunIsolateCommands(association utils.FileCommandAssociation, file string, f logger.Warning("No modifications found for file %q", file) continue } + anythingDone = true var count int fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr) @@ -417,5 +431,8 @@ func RunIsolateCommands(association utils.FileCommandAssociation, file string, f logger.Info("Executed %d isolate modifications for file %q", count, file) } + if !anythingDone { + return fileDataStr, NothingToDo + } return fileDataStr, nil }