Open 030 opened 2 weeks ago
package main
import (
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strings"
log "github.com/sirupsen/logrus"
)
func replaceStringInFile(filename, oldString, newString string) error {
// Read the content of the file
content, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("failed to read file: %v", err)
}
// Replace the old string with the new string in the content
updatedContent := strings.ReplaceAll(string(content), oldString, newString)
// Write the updated content back to the file
err = os.WriteFile(filename, []byte(updatedContent), 0o666)
if err != nil {
return fmt.Errorf("failed to write to file: %v", err)
}
return nil
}
func main() {
startDir := "./e2e"
// funcRegex := regexp.MustCompile(`func\s+(\w+)\s*\(`)
funcRegex := regexp.MustCompile(`func\s+(\(?\w+?\s?\*?\w+\)?\s??\w+)\s*\(`)
err := filepath.WalkDir(startDir, func(path string, d os.DirEntry, err error) error {
if err != nil {
log.Errorf("Failed to access path %s: %v", path, err)
return err
}
if !d.IsDir() && filepath.Ext(d.Name()) == ".go" {
file, err := os.Open(path)
if err != nil {
log.Errorf("Failed to open file %s: %v", path, err)
return nil // Continue with the next file
}
defer func() {
if cerr := file.Close(); cerr != nil {
err = fmt.Errorf("some error: %w", cerr)
}
}()
data, err := io.ReadAll(file)
if err != nil {
log.Errorf("Failed to read file %s: %v", path, err)
return nil // Continue with the next file
}
content := string(data)
if strings.Contains(content, "Code generated by") {
return nil
}
matches := funcRegex.FindAllStringSubmatch(content, -1)
if len(matches) > 0 {
fmt.Printf("File: %s\n", path)
for _, match := range matches {
if len(match) > 1 {
println(match[1])
}
}
// fmt.Println()
}
updatedContent := content
originalFuncName := ""
newFuncName := ""
// println("Matches: ", originalFuncName, newFuncName)
for _, match := range matches {
if len(match) > 1 {
originalFuncName = match[1]
newFuncName = strings.ReplaceAll(originalFuncName, "_", "")
if originalFuncName != newFuncName {
// Replace the original function name with the new one without underscores
updatedContent = strings.ReplaceAll(updatedContent, originalFuncName, newFuncName)
fmt.Printf("Updated function name from %s to %s in file: %s\n", originalFuncName, newFuncName, path)
err := replaceStringInFile(path, originalFuncName, newFuncName)
if err != nil {
log.Fatalf("Error replacing string in file: %v\n", err)
}
// time.Sleep(1 * time.Second)
}
}
}
}
return nil
})
if err != nil {
log.Fatalf("Error walking the path: %v", err)
}
}