From 867b188718c6bed3fefe8c4337a739e44b010c27 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Thu, 27 Mar 2025 22:02:36 +0100 Subject: [PATCH] Work out file reading and writing --- main.go | 54 ++++++++++++++++++++++++++++++------------ processor/processor.go | 9 ------- processor/regex.go | 5 ---- 3 files changed, 39 insertions(+), 29 deletions(-) diff --git a/main.go b/main.go index 5e184a5..8f181ee 100644 --- a/main.go +++ b/main.go @@ -44,6 +44,11 @@ type ModifyCommand struct { Reset bool `yaml:"reset"` LogLevel string `yaml:"loglevel"` } +type ReplaceCommand struct { + From int + To int + With string +} type CookFile []ModifyCommand func (c *ModifyCommand) Validate() error { @@ -107,25 +112,48 @@ func main() { files, err := ExpandGLobs(globs) if err != nil { logger.Error("Failed to expand file patterns: %v", err) - flag.Usage() return } logger.Info("Found %d files to process", len(files)) - associations, err := AssociateFilesWithCommands(files, commands) - if err != nil { - logger.Error("Failed to associate files with commands: %v", err) - flag.Usage() - return - } - - log.Printf("%#v", associations) // Somehow connect files to commands via globs.. - // Maybe do.... // For each file check every glob of every command // Maybe memoize this part // That way we know what commands affect what files + associations, err := AssociateFilesWithCommands(files, commands) + if err != nil { + logger.Error("Failed to associate files with commands: %v", err) + return + } + + // TODO: Utilize parallel workers for this // Then for each file run all commands associated with the file + for file, commands := range associations { + 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) + + 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 + } + + err = os.WriteFile(file, fileData, 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 @@ -296,8 +324,6 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) { } if len(args) < 3 { - logger.Error("At least %d arguments are required", 3) - flag.Usage() return nil, fmt.Errorf("at least %d arguments are required", 3) } @@ -311,9 +337,7 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) { } if err := command.Validate(); err != nil { - logger.Error("Invalid command: %v", err) - flag.Usage() - return nil, fmt.Errorf("invalid command: %v", err) + return nil, fmt.Errorf("invalid command: %w", err) } return []ModifyCommand{command}, nil diff --git a/processor/processor.go b/processor/processor.go index a94a3ed..60ad678 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -31,15 +31,6 @@ type Processor interface { FromLua(L *lua.LState) (interface{}, error) } -// ModificationRecord tracks a single value modification -type ModificationRecord struct { - File string - OldValue string - NewValue string - Operation string - Context string -} - func NewLuaState() (*lua.LState, error) { L := lua.NewState() // defer L.Close() diff --git a/processor/regex.go b/processor/regex.go index 62a4e0f..419e14b 100644 --- a/processor/regex.go +++ b/processor/regex.go @@ -90,11 +90,6 @@ type CaptureGroup struct { Updated string Range [2]int } -type ReplaceCommand struct { - From int - To int - With string -} // ProcessContent applies regex replacement with Lua processing func (p *RegexProcessor) ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error) {