Now only save CHANGED files to database (before changes of course)
This commit is contained in:
45
main.go
45
main.go
@@ -1,6 +1,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@@ -180,25 +181,34 @@ func main() {
|
|||||||
}
|
}
|
||||||
fileDataStr := string(fileData)
|
fileDataStr := string(fileData)
|
||||||
|
|
||||||
|
isChanged := false
|
||||||
|
logger.Debug("Running isolate commands for file %q", file)
|
||||||
|
fileDataStr, err = RunIsolateCommands(association, file, fileDataStr, &fileMutex)
|
||||||
|
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 && 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)
|
logger.Debug("Saving file %q to database", file)
|
||||||
err = db.SaveFile(file, fileData)
|
err = db.SaveFile(file, fileData)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Error("Failed to save file %q to database: %v", file, err)
|
logger.Error("Failed to save file %q to database: %v", file, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("Running isolate commands for file %q", file)
|
|
||||||
fileDataStr, err = RunIsolateCommands(association, file, fileDataStr, &fileMutex)
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Failed to run isolate commands for file %q: %v", file, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Debug("Running other commands for file %q", file)
|
|
||||||
fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex, commandLoggers)
|
|
||||||
if err != nil {
|
|
||||||
logger.Error("Failed to run other commands for file %q: %v", file, err)
|
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("Writing file %q", file)
|
logger.Debug("Writing file %q", file)
|
||||||
@@ -346,6 +356,8 @@ func CreateExampleConfig() {
|
|||||||
logger.Info("Wrote example_cook.yml")
|
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) {
|
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
|
// Aggregate all the modifications and execute them
|
||||||
modifications := []utils.ReplaceCommand{}
|
modifications := []utils.ReplaceCommand{}
|
||||||
@@ -377,7 +389,7 @@ func RunOtherCommands(file string, fileDataStr string, association utils.FileCom
|
|||||||
|
|
||||||
if len(modifications) == 0 {
|
if len(modifications) == 0 {
|
||||||
logger.Warning("No modifications found for file %q", file)
|
logger.Warning("No modifications found for file %q", file)
|
||||||
return fileDataStr, nil
|
return fileDataStr, NothingToDo
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort commands in reverse order for safe replacements
|
// 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) {
|
func RunIsolateCommands(association utils.FileCommandAssociation, file string, fileDataStr string, fileMutex *sync.Mutex) (string, error) {
|
||||||
|
anythingDone := false
|
||||||
for _, isolateCommand := range association.IsolateCommands {
|
for _, isolateCommand := range association.IsolateCommands {
|
||||||
logger.Info("Processing file %q with isolate command %q", file, isolateCommand.Regex)
|
logger.Info("Processing file %q with isolate command %q", file, isolateCommand.Regex)
|
||||||
modifications, err := processor.ProcessRegex(fileDataStr, isolateCommand, file)
|
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)
|
logger.Warning("No modifications found for file %q", file)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
anythingDone = true
|
||||||
|
|
||||||
var count int
|
var count int
|
||||||
fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr)
|
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)
|
logger.Info("Executed %d isolate modifications for file %q", count, file)
|
||||||
}
|
}
|
||||||
|
if !anythingDone {
|
||||||
|
return fileDataStr, NothingToDo
|
||||||
|
}
|
||||||
return fileDataStr, nil
|
return fileDataStr, nil
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user