diff --git a/main.go b/main.go index 2085746..819e6af 100644 --- a/main.go +++ b/main.go @@ -134,7 +134,8 @@ func main() { defer wg.Done() logger.Printf("Processing file: %s", file) - modCount, matchCount, err := proc.Process(file, pattern, luaExpr) + // It's a bit fucked, maybe I could do better to call it from proc... But it'll do for now + modCount, matchCount, err := processor.Process(proc, file, pattern, luaExpr) if err != nil { fmt.Fprintf(os.Stderr, "Failed to process file %s: %v\n", file, err) stats.FailedFiles++ diff --git a/processor/json.go b/processor/json.go index 57b4f0b..0217db5 100644 --- a/processor/json.go +++ b/processor/json.go @@ -5,8 +5,6 @@ import ( "fmt" "log" "modify/processor/jsonpath" - "os" - "path/filepath" lua "github.com/yuin/gopher-lua" ) @@ -14,39 +12,6 @@ import ( // JSONProcessor implements the Processor interface for JSON documents type JSONProcessor struct{} -// Process implements the Processor interface for JSONProcessor -func (p *JSONProcessor) Process(filename string, pattern string, luaExpr string) (int, int, error) { - // Read file content - cwd, err := os.Getwd() - if err != nil { - return 0, 0, fmt.Errorf("error getting current working directory: %v", err) - } - - fullPath := filepath.Join(cwd, filename) - content, err := os.ReadFile(fullPath) - if err != nil { - return 0, 0, fmt.Errorf("error reading file: %v", err) - } - - fileContent := string(content) - - // Process the content - modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, pattern, luaExpr) - if err != nil { - return 0, 0, err - } - - // If we made modifications, save the file - if modCount > 0 { - err = os.WriteFile(fullPath, []byte(modifiedContent), 0644) - if err != nil { - return 0, 0, fmt.Errorf("error writing file: %v", err) - } - } - - return modCount, matchCount, nil -} - // ProcessContent implements the Processor interface for JSONProcessor func (p *JSONProcessor) ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error) { // Parse JSON document diff --git a/processor/processor.go b/processor/processor.go index b137d01..566487f 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -2,6 +2,8 @@ package processor import ( "fmt" + "os" + "path/filepath" "strings" lua "github.com/yuin/gopher-lua" @@ -10,7 +12,8 @@ import ( // Processor defines the interface for all file processors type Processor interface { // Process handles processing a file with the given pattern and Lua expression - Process(filename string, pattern string, luaExpr string) (int, int, error) + // Now implemented as a base function in processor.go + // Process(filename string, pattern string, luaExpr string) (int, int, error) // ProcessContent handles processing a string content directly with the given pattern and Lua expression // Returns the modified content, modification count, match count, and any error @@ -51,6 +54,38 @@ func NewLuaState() (*lua.LState, error) { return L, nil } +func Process(p Processor, filename string, pattern string, luaExpr string) (int, int, error) { + // Read file content + cwd, err := os.Getwd() + if err != nil { + return 0, 0, fmt.Errorf("error getting current working directory: %v", err) + } + + fullPath := filepath.Join(cwd, filename) + content, err := os.ReadFile(fullPath) + if err != nil { + return 0, 0, fmt.Errorf("error reading file: %v", err) + } + + fileContent := string(content) + + // Process the content + modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, pattern, luaExpr) + if err != nil { + return 0, 0, err + } + + // If we made modifications, save the file + if modCount > 0 { + err = os.WriteFile(fullPath, []byte(modifiedContent), 0644) + if err != nil { + return 0, 0, fmt.Errorf("error writing file: %v", err) + } + } + + return modCount, matchCount, nil +} + // ToLua converts a struct or map to a Lua table recursively func ToLua(L *lua.LState, data interface{}) (lua.LValue, error) { switch v := data.(type) { diff --git a/processor/regex.go b/processor/regex.go index 74d66b9..ae3251d 100644 --- a/processor/regex.go +++ b/processor/regex.go @@ -2,8 +2,6 @@ package processor import ( "fmt" - "os" - "path/filepath" "regexp" "strconv" "strings" @@ -14,34 +12,6 @@ import ( // RegexProcessor implements the Processor interface using regex patterns type RegexProcessor struct{} -// Process implements the Processor interface for RegexProcessor -func (p *RegexProcessor) Process(filename string, pattern string, luaExpr string) (int, int, error) { - // Read file content - fullPath := filepath.Join(".", filename) - content, err := os.ReadFile(fullPath) - if err != nil { - return 0, 0, fmt.Errorf("error reading file: %v", err) - } - - fileContent := string(content) - - // Process the content - modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, pattern, luaExpr) - if err != nil { - return 0, 0, err - } - - // If we made modifications, save the file - if modCount > 0 { - err = os.WriteFile(fullPath, []byte(modifiedContent), 0644) - if err != nil { - return 0, 0, fmt.Errorf("error writing file: %v", err) - } - } - - return modCount, matchCount, nil -} - // ToLua sets capture groups as Lua variables (v1, v2, etc. for numeric values and s1, s2, etc. for strings) func (p *RegexProcessor) ToLua(L *lua.LState, data interface{}) error { captures, ok := data.([]string) diff --git a/processor/xml.go b/processor/xml.go index a7e9e3a..cf9cb97 100644 --- a/processor/xml.go +++ b/processor/xml.go @@ -2,8 +2,6 @@ package processor import ( "fmt" - "os" - "path/filepath" "strings" "github.com/antchfx/xmlquery" @@ -13,34 +11,6 @@ import ( // XMLProcessor implements the Processor interface for XML documents type XMLProcessor struct{} -// Process implements the Processor interface for XMLProcessor -func (p *XMLProcessor) Process(filename string, pattern string, luaExpr string) (int, int, error) { - // Read file content - fullPath := filepath.Join(".", filename) - content, err := os.ReadFile(fullPath) - if err != nil { - return 0, 0, fmt.Errorf("error reading file: %v", err) - } - - fileContent := string(content) - - // Process the content - modifiedContent, modCount, matchCount, err := p.ProcessContent(fileContent, pattern, luaExpr) - if err != nil { - return 0, 0, err - } - - // If we made modifications, save the file - if modCount > 0 { - err = os.WriteFile(fullPath, []byte(modifiedContent), 0644) - if err != nil { - return 0, 0, fmt.Errorf("error writing file: %v", err) - } - } - - return modCount, matchCount, nil -} - // ProcessContent implements the Processor interface for XMLProcessor func (p *XMLProcessor) ProcessContent(content string, pattern string, luaExpr string) (string, int, int, error) { // Parse XML document