Refactor running commands to separate functions
This commit is contained in:
164
main.go
164
main.go
@@ -120,85 +120,15 @@ func main() {
|
||||
// Track per-file processing time
|
||||
fileStartTime := time.Now()
|
||||
|
||||
for _, isolateCommand := range association.IsolateCommands {
|
||||
fileData, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
logger.Error("Failed to read file %q: %v", file, err)
|
||||
return
|
||||
}
|
||||
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
|
||||
fileDataStr := string(fileData)
|
||||
|
||||
logger.Info("Processing file %q with isolate command %q", file, isolateCommand.Regex)
|
||||
modifications, err := processor.ProcessRegex(fileDataStr, isolateCommand, file)
|
||||
if err != nil {
|
||||
logger.Error("Failed to process file %q with isolate command %q: %v", file, isolateCommand.Regex, err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(modifications) == 0 {
|
||||
logger.Info("No modifications found for file %q", file)
|
||||
return
|
||||
}
|
||||
|
||||
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
|
||||
|
||||
fileMutex.Lock()
|
||||
stats.ProcessedFiles++
|
||||
stats.TotalModifications += count
|
||||
fileMutex.Unlock()
|
||||
|
||||
logger.Info("Executed %d isolate modifications for file %q", count, file)
|
||||
|
||||
err = os.WriteFile(file, []byte(fileDataStr), 0644)
|
||||
}
|
||||
|
||||
fileData, err := os.ReadFile(file)
|
||||
err := RunIsolateCommands(association, file, &fileMutex)
|
||||
if err != nil {
|
||||
logger.Error("Failed to read file %q: %v", file, err)
|
||||
return
|
||||
}
|
||||
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
|
||||
fileDataStr := string(fileData)
|
||||
|
||||
// Aggregate all the modifications and execute them
|
||||
modifications := []utils.ReplaceCommand{}
|
||||
for _, command := range association.Commands {
|
||||
logger.Info("Processing file %q with command %q", file, command.Regex)
|
||||
newModifications, err := processor.ProcessRegex(fileDataStr, command, file)
|
||||
if err != nil {
|
||||
logger.Error("Failed to process file %q with command %q: %v", file, command.Regex, err)
|
||||
return
|
||||
}
|
||||
modifications = append(modifications, newModifications...)
|
||||
// It is not guranteed that all the commands will be executed...
|
||||
// TODO: Make this better
|
||||
// We'd have to pass the map to executemodifications or something...
|
||||
count, ok := stats.ModificationsPerCommand.Load(command.Name)
|
||||
if !ok {
|
||||
count = 0
|
||||
}
|
||||
stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(newModifications))
|
||||
}
|
||||
|
||||
if len(modifications) == 0 {
|
||||
logger.Info("No modifications found for file %q", file)
|
||||
logger.Error("Failed to run isolate commands for file %q: %v", file, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Sort commands in reverse order for safe replacements
|
||||
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
|
||||
|
||||
fileMutex.Lock()
|
||||
stats.ProcessedFiles++
|
||||
stats.TotalModifications += count
|
||||
fileMutex.Unlock()
|
||||
|
||||
logger.Info("Executed %d modifications for file %q", count, file)
|
||||
|
||||
err = os.WriteFile(file, []byte(fileDataStr), 0644)
|
||||
err = RunOtherCommands(file, association, &fileMutex)
|
||||
if err != nil {
|
||||
logger.Error("Failed to write file %q: %v", file, err)
|
||||
logger.Error("Failed to run other commands for file %q: %v", file, err)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -275,3 +205,89 @@ func main() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func RunOtherCommands(file string, association utils.FileCommandAssociation, fileMutex *sync.Mutex) error {
|
||||
fileData, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read file %q: %w", file, err)
|
||||
}
|
||||
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
|
||||
fileDataStr := string(fileData)
|
||||
|
||||
// Aggregate all the modifications and execute them
|
||||
modifications := []utils.ReplaceCommand{}
|
||||
for _, command := range association.Commands {
|
||||
logger.Info("Processing file %q with command %q", file, command.Regex)
|
||||
newModifications, err := processor.ProcessRegex(fileDataStr, command, file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to process file %q with command %q: %w", file, command.Regex, err)
|
||||
}
|
||||
modifications = append(modifications, newModifications...)
|
||||
// It is not guranteed that all the commands will be executed...
|
||||
// TODO: Make this better
|
||||
// We'd have to pass the map to executemodifications or something...
|
||||
count, ok := stats.ModificationsPerCommand.Load(command.Name)
|
||||
if !ok {
|
||||
count = 0
|
||||
}
|
||||
stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(newModifications))
|
||||
}
|
||||
|
||||
if len(modifications) == 0 {
|
||||
logger.Info("No modifications found for file %q", file)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Sort commands in reverse order for safe replacements
|
||||
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
|
||||
|
||||
fileMutex.Lock()
|
||||
stats.ProcessedFiles++
|
||||
stats.TotalModifications += count
|
||||
fileMutex.Unlock()
|
||||
|
||||
logger.Info("Executed %d modifications for file %q", count, file)
|
||||
|
||||
err = os.WriteFile(file, []byte(fileDataStr), 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write file %q: %w", file, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func RunIsolateCommands(association utils.FileCommandAssociation, file string, fileMutex *sync.Mutex) error {
|
||||
for _, isolateCommand := range association.IsolateCommands {
|
||||
fileData, err := os.ReadFile(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to read file %q: %w", file, err)
|
||||
}
|
||||
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
|
||||
fileDataStr := string(fileData)
|
||||
|
||||
logger.Info("Processing file %q with isolate command %q", file, isolateCommand.Regex)
|
||||
modifications, err := processor.ProcessRegex(fileDataStr, isolateCommand, file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to process file %q with isolate command %q: %w", file, isolateCommand.Regex, err)
|
||||
}
|
||||
|
||||
if len(modifications) == 0 {
|
||||
logger.Info("No modifications found for file %q", file)
|
||||
return nil
|
||||
}
|
||||
|
||||
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
|
||||
|
||||
fileMutex.Lock()
|
||||
stats.ProcessedFiles++
|
||||
stats.TotalModifications += count
|
||||
fileMutex.Unlock()
|
||||
|
||||
logger.Info("Executed %d isolate modifications for file %q", count, file)
|
||||
|
||||
err = os.WriteFile(file, []byte(fileDataStr), 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to write file %q: %w", file, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user