Refactor entirety of replace command to main for now
This commit is contained in:
50
main.go
50
main.go
@@ -6,6 +6,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -135,27 +136,30 @@ func main() {
|
||||
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 := []ReplaceCommand{}
|
||||
for _, command := range commands {
|
||||
logger.Info("Processing file %q with command %q", file, command.Pattern)
|
||||
// TODO: Run processor and return modifications
|
||||
}
|
||||
|
||||
for _, modification := range modifications {
|
||||
logger.Info("Modifying file %q from %d to %d with %q", file, modification.From, modification.To, modification.With)
|
||||
// TODO: Execute modifications
|
||||
}
|
||||
// Sort commands in reverse order for safe replacements
|
||||
sort.Slice(modifications, func(i, j int) bool {
|
||||
return modifications[i].From > modifications[j].From
|
||||
})
|
||||
logger.Trace("Applying %d replacement commands in reverse order", len(modifications))
|
||||
|
||||
err = os.WriteFile(file, fileData, 0644)
|
||||
fileDataStr = ExecuteModifications(modifications, fileDataStr)
|
||||
|
||||
err = os.WriteFile(file, []byte(fileDataStr), 0644)
|
||||
if err != nil {
|
||||
logger.Error("Failed to write file %q: %v", file, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Aggregate all the modifications and execute them
|
||||
// Taking care of the whole duplicates and shit
|
||||
// This will also relieve processor of some of the file loading
|
||||
// But we will also have to rework the tests.......
|
||||
// Also give each command its own logger, maybe prefix it with something... Maybe give commands a name?
|
||||
@@ -253,6 +257,38 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func ExecuteModifications(modifications []ReplaceCommand, fileData string) string {
|
||||
var err error
|
||||
for _, modification := range modifications {
|
||||
fileData, err = modification.Execute(fileData)
|
||||
if err != nil {
|
||||
logger.Error("Failed to execute modification: %v", err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
return fileData
|
||||
}
|
||||
|
||||
func (m *ReplaceCommand) Execute(fileDataStr string) (string, error) {
|
||||
err := m.Validate(len(fileDataStr))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to validate modification: %v", err)
|
||||
}
|
||||
|
||||
logger.Trace("Replace pos %d-%d with %q", m.From, m.To, m.With)
|
||||
return fileDataStr[:m.From] + m.With + fileDataStr[m.To:], nil
|
||||
}
|
||||
|
||||
func (m *ReplaceCommand) Validate(maxsize int) error {
|
||||
if m.To < m.From {
|
||||
return fmt.Errorf("command to is less than from: %v", m)
|
||||
}
|
||||
if m.From > maxsize || m.To > maxsize {
|
||||
return fmt.Errorf("command from or to is greater than replacement length: %v", m)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) {
|
||||
associationCount := 0
|
||||
fileCommands := make(map[string][]ModifyCommand)
|
||||
|
Reference in New Issue
Block a user