Add tests
This commit is contained in:
51
main.go
51
main.go
@@ -111,26 +111,29 @@ end
|
||||
|
||||
// Process each file
|
||||
for _, file := range files {
|
||||
err := processFile(file, pattern, fullScript)
|
||||
log.Printf("Processing file: %s", file)
|
||||
fullPath := filepath.Join(".", file)
|
||||
|
||||
content, err := os.ReadFile(fullPath)
|
||||
if err != nil {
|
||||
Error.Printf("error reading file: %v", err)
|
||||
continue
|
||||
}
|
||||
|
||||
fileContent := string(content)
|
||||
result, err := process(fileContent, pattern, fullScript)
|
||||
if err != nil {
|
||||
Error.Printf("Error processing file %s: %v", file, err)
|
||||
}
|
||||
|
||||
err = os.WriteFile(fullPath, []byte(result), 0644)
|
||||
if err != nil {
|
||||
Error.Printf("error writing file: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func processFile(filename string, pattern *regexp.Regexp, luaScript string) error {
|
||||
log.Printf("Processing file: %s", filename)
|
||||
fullPath := filepath.Join(".", filename)
|
||||
|
||||
content, err := os.ReadFile(fullPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error reading file: %v", err)
|
||||
}
|
||||
|
||||
fileContent := string(content)
|
||||
modified := false
|
||||
|
||||
// Initialize Lua state once per file
|
||||
func process(data string, pattern *regexp.Regexp, luaScript string) (string, error) {
|
||||
L := lua.NewState()
|
||||
defer L.Close()
|
||||
|
||||
@@ -138,16 +141,16 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
|
||||
L.Push(L.GetGlobal("require"))
|
||||
L.Push(lua.LString("math"))
|
||||
if err := L.PCall(1, 1, nil); err != nil {
|
||||
return fmt.Errorf("error loading Lua math library: %v", err)
|
||||
return data, fmt.Errorf("error loading Lua math library: %v", err)
|
||||
}
|
||||
|
||||
// Load the Lua script
|
||||
if err := L.DoString(luaScript); err != nil {
|
||||
return fmt.Errorf("error in Lua script: %v", err)
|
||||
return data, fmt.Errorf("error in Lua script: %v", err)
|
||||
}
|
||||
|
||||
// Process all regex matches
|
||||
result := pattern.ReplaceAllStringFunc(fileContent, func(match string) string {
|
||||
result := pattern.ReplaceAllStringFunc(data, func(match string) string {
|
||||
captures := pattern.FindStringSubmatch(match)
|
||||
if len(captures) <= 1 {
|
||||
// No capture groups, return unchanged
|
||||
@@ -200,7 +203,6 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
|
||||
// Replace old value with new value
|
||||
result = strings.Replace(result, oldVal, newVal, 1)
|
||||
}
|
||||
modified = true
|
||||
return result
|
||||
}
|
||||
|
||||
@@ -208,16 +210,5 @@ func processFile(filename string, pattern *regexp.Regexp, luaScript string) erro
|
||||
return match
|
||||
})
|
||||
|
||||
// Only write if changes were made
|
||||
if modified {
|
||||
err = os.WriteFile(fullPath, []byte(result), 0644)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error writing file: %v", err)
|
||||
}
|
||||
log.Printf("File %s updated successfully", filename)
|
||||
} else {
|
||||
log.Printf("No changes made to %s", filename)
|
||||
}
|
||||
|
||||
return nil
|
||||
return result, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user