Rename pattern to regex

This commit is contained in:
2025-03-28 01:08:48 +01:00
parent 2629722f67
commit 2d523dfe64
6 changed files with 175 additions and 175 deletions

View File

@@ -121,10 +121,10 @@ func main() {
// Aggregate all the modifications and execute them
modifications := []utils.ReplaceCommand{}
for _, command := range commands {
logger.Info("Processing file %q with command %q", file, command.Pattern)
logger.Info("Processing file %q with command %q", file, command.Regex)
commands, err := processor.ProcessRegex(fileDataStr, command)
if err != nil {
logger.Error("Failed to process file %q with command %q: %v", file, command.Pattern, err)
logger.Error("Failed to process file %q with command %q: %v", file, command.Regex, err)
return
}
modifications = append(modifications, commands...)

View File

@@ -23,14 +23,14 @@ type CaptureGroup struct {
// ProcessContent applies regex replacement with Lua processing
func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceCommand, error) {
var commands []utils.ReplaceCommand
logger.Trace("Processing regex: %q", command.Pattern)
logger.Trace("Processing regex: %q", command.Regex)
// Start timing the regex processing
startTime := time.Now()
// We don't HAVE to do this multiple times for a pattern
// But it's quick enough for us to not care
pattern := resolveRegexPlaceholders(command.Pattern)
pattern := resolveRegexPlaceholders(command.Regex)
logger.Debug("Compiling regex pattern: %s", pattern)
patternCompileStart := time.Now()

View File

@@ -33,7 +33,7 @@ func normalizeWhitespace(s string) string {
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
command := utils.ModifyCommand{
Pattern: regex,
Regex: regex,
Lua: lua,
LogLevel: "TRACE",
}

View File

@@ -10,7 +10,7 @@ import (
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
command := utils.ModifyCommand{
Pattern: regex,
Regex: regex,
Lua: lua,
LogLevel: "TRACE",
}

View File

@@ -11,7 +11,7 @@ import (
type ModifyCommand struct {
Name string `yaml:"name"`
Pattern string `yaml:"pattern"`
Regex string `yaml:"regex"`
Lua string `yaml:"lua"`
Files []string `yaml:"files"`
Git bool `yaml:"git"`
@@ -21,7 +21,7 @@ type ModifyCommand struct {
type CookFile []ModifyCommand
func (c *ModifyCommand) Validate() error {
if c.Pattern == "" {
if c.Regex == "" {
return fmt.Errorf("pattern is required")
}
if c.Lua == "" {
@@ -49,7 +49,7 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s
continue
}
if matches {
logger.Debug("Found match for file %q and command %q", file, command.Pattern)
logger.Debug("Found match for file %q and command %q", file, command.Regex)
fileCommands[file] = append(fileCommands[file], command)
associationCount++
}
@@ -147,7 +147,7 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
}
command := ModifyCommand{
Pattern: args[0],
Regex: args[0],
Lua: args[1],
Files: args[2:],
Git: *GitFlag,

View File

@@ -17,7 +17,7 @@ func TestModifyCommandValidate(t *testing.T) {
{
name: "Valid command",
command: ModifyCommand{
Pattern: "test pattern",
Regex: "test pattern",
Lua: "test expression",
Files: []string{"file1", "file2"},
LogLevel: "INFO",
@@ -27,7 +27,7 @@ func TestModifyCommandValidate(t *testing.T) {
{
name: "Missing pattern",
command: ModifyCommand{
Pattern: "",
Regex: "",
Lua: "test expression",
Files: []string{"file1", "file2"},
LogLevel: "INFO",
@@ -37,7 +37,7 @@ func TestModifyCommandValidate(t *testing.T) {
{
name: "Missing LuaExpr",
command: ModifyCommand{
Pattern: "test pattern",
Regex: "test pattern",
Lua: "",
Files: []string{"file1", "file2"},
LogLevel: "INFO",
@@ -47,7 +47,7 @@ func TestModifyCommandValidate(t *testing.T) {
{
name: "Missing files",
command: ModifyCommand{
Pattern: "test pattern",
Regex: "test pattern",
Lua: "test expression",
Files: []string{},
LogLevel: "INFO",
@@ -57,7 +57,7 @@ func TestModifyCommandValidate(t *testing.T) {
{
name: "Default log level",
command: ModifyCommand{
Pattern: "test pattern",
Regex: "test pattern",
Lua: "test expression",
Files: []string{"file1", "file2"},
LogLevel: "",
@@ -128,17 +128,17 @@ func TestAssociateFilesWithCommands(t *testing.T) {
// Define commands with different globs
commands := []ModifyCommand{
{
Pattern: "pattern1",
Regex: "pattern1",
Lua: "expr1",
Files: []string{"*.xml"},
},
{
Pattern: "pattern2",
Regex: "pattern2",
Lua: "expr2",
Files: []string{"*.txt"},
},
{
Pattern: "pattern3",
Regex: "pattern3",
Lua: "expr3",
Files: []string{"subdir/*"},
},
@@ -161,7 +161,7 @@ func TestAssociateFilesWithCommands(t *testing.T) {
for file, cmds := range associations {
t.Logf("File %s is associated with %d commands", file, len(cmds))
for i, cmd := range cmds {
t.Logf(" Command %d: Pattern=%s, Files=%v", i, cmd.Pattern, cmd.Files)
t.Logf(" Command %d: Pattern=%s, Files=%v", i, cmd.Regex, cmd.Files)
}
// Specific validation based on our file types
@@ -230,17 +230,17 @@ func TestAssociateFilesWithCommands(t *testing.T) {
func TestAggregateGlobs(t *testing.T) {
commands := []ModifyCommand{
{
Pattern: "pattern1",
Regex: "pattern1",
Lua: "expr1",
Files: []string{"*.xml", "*.txt"},
},
{
Pattern: "pattern2",
Regex: "pattern2",
Lua: "expr2",
Files: []string{"*.xml", "*.json"},
},
{
Pattern: "pattern3",
Regex: "pattern3",
Lua: "expr3",
Files: []string{"subdir/*.xml"},
},
@@ -359,8 +359,8 @@ func TestLoadCommandFromArgs(t *testing.T) {
cmd := commands[0]
// Check command properties
if cmd.Pattern != tc.args[0] {
t.Errorf("Expected pattern %q, got %q", tc.args[0], cmd.Pattern)
if cmd.Regex != tc.args[0] {
t.Errorf("Expected pattern %q, got %q", tc.args[0], cmd.Regex)
}
if cmd.Lua != tc.args[1] {
@@ -407,10 +407,10 @@ func TestLoadCommandsFromCookFileSuccess(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, commands, 2)
assert.Equal(t, "command1", commands[0].Name)
assert.Equal(t, "*.txt", commands[0].Pattern)
assert.Equal(t, "*.txt", commands[0].Regex)
assert.Equal(t, "replace", commands[0].Lua)
assert.Equal(t, "command2", commands[1].Name)
assert.Equal(t, "*.go", commands[1].Pattern)
assert.Equal(t, "*.go", commands[1].Regex)
assert.Equal(t, "delete", commands[1].Lua)
}
@@ -435,10 +435,10 @@ func TestLoadCommandsFromCookFileWithComments(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, commands, 2)
assert.Equal(t, "command1", commands[0].Name)
assert.Equal(t, "*.txt", commands[0].Pattern)
assert.Equal(t, "*.txt", commands[0].Regex)
assert.Equal(t, "replace", commands[0].Lua)
assert.Equal(t, "command2", commands[1].Name)
assert.Equal(t, "*.go", commands[1].Pattern)
assert.Equal(t, "*.go", commands[1].Regex)
assert.Equal(t, "delete", commands[1].Lua)
}
@@ -454,10 +454,10 @@ func TestLoadCommandsFromCookFileWithFlowStyle(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, commands, 2)
assert.Equal(t, "command1", commands[0].Name)
assert.Equal(t, "*.txt", commands[0].Pattern)
assert.Equal(t, "*.txt", commands[0].Regex)
assert.Equal(t, "replace", commands[0].Lua)
assert.Equal(t, "command2", commands[1].Name)
assert.Equal(t, "*.go", commands[1].Pattern)
assert.Equal(t, "*.go", commands[1].Regex)
assert.Equal(t, "delete", commands[1].Lua)
}
@@ -513,13 +513,13 @@ func TestLoadCommandsFromCookFileWithMultipleEntries(t *testing.T) {
assert.NoError(t, err)
assert.Len(t, commands, 3)
assert.Equal(t, "command1", commands[0].Name)
assert.Equal(t, "*.txt", commands[0].Pattern)
assert.Equal(t, "*.txt", commands[0].Regex)
assert.Equal(t, "replace", commands[0].Lua)
assert.Equal(t, "command2", commands[1].Name)
assert.Equal(t, "*.go", commands[1].Pattern)
assert.Equal(t, "*.go", commands[1].Regex)
assert.Equal(t, "delete", commands[1].Lua)
assert.Equal(t, "command3", commands[2].Name)
assert.Equal(t, "*.md", commands[2].Pattern)
assert.Equal(t, "*.md", commands[2].Regex)
assert.Equal(t, "append", commands[2].Lua)
}
@@ -582,7 +582,7 @@ func TestLoadCommandFromArgsWithValidArguments(t *testing.T) {
// Assert
assert.NoError(t, err)
assert.Len(t, commands, 1)
assert.Equal(t, "*.go", commands[0].Pattern)
assert.Equal(t, "*.go", commands[0].Regex)
assert.Equal(t, "return x", commands[0].Lua)
assert.Equal(t, []string{"file1.go", "file2.go"}, commands[0].Files)
assert.Equal(t, true, commands[0].Git)
@@ -662,7 +662,7 @@ func TestLoadCommandFromArgsPopulatesFieldsCorrectly(t *testing.T) {
// Assert
assert.NoError(t, err)
assert.Len(t, commands, 1)
assert.Equal(t, "*.txt", commands[0].Pattern)
assert.Equal(t, "*.txt", commands[0].Regex)
assert.Equal(t, "print('Hello')", commands[0].Lua)
assert.Equal(t, []string{"file1.txt", "file2.txt"}, commands[0].Files)
assert.Equal(t, false, commands[0].Git)
@@ -706,72 +706,72 @@ func TestLoadCommandFromArgsSetsGitFlagWhenResetFlagIsTrue(t *testing.T) {
// Can't be asked doing that right now...
// Successfully loads commands from multiple YAML files in the current directory
// func TestLoadCommandsFromCookFilesSuccessfully(t *testing.T) {
// // Setup test directory with mock YAML files
// tempDir, err := os.MkdirTemp("", "cookfiles_test")
// if err != nil {
// t.Fatalf("Failed to create temp directory: %v", err)
// }
// defer os.RemoveAll(tempDir)
// // Setup test directory with mock YAML files
// tempDir, err := os.MkdirTemp("", "cookfiles_test")
// if err != nil {
// t.Fatalf("Failed to create temp directory: %v", err)
// }
// defer os.RemoveAll(tempDir)
// // Save current directory to restore later
// originalDir, err := os.Getwd()
// if err != nil {
// t.Fatalf("Failed to get current directory: %v", err)
// }
// // Save current directory to restore later
// originalDir, err := os.Getwd()
// if err != nil {
// t.Fatalf("Failed to get current directory: %v", err)
// }
// // Change to temp directory
// err = os.Chdir(tempDir)
// if err != nil {
// t.Fatalf("Failed to change directory: %v", err)
// }
// defer os.Chdir(originalDir)
// // Change to temp directory
// err = os.Chdir(tempDir)
// if err != nil {
// t.Fatalf("Failed to change directory: %v", err)
// }
// defer os.Chdir(originalDir)
// // Create mock YAML files
// yamlContent1 := []byte("commands:\n - name: command1\n type: test")
// yamlContent2 := []byte("commands:\n - name: command2\n type: test")
// // Create mock YAML files
// yamlContent1 := []byte("commands:\n - name: command1\n type: test")
// yamlContent2 := []byte("commands:\n - name: command2\n type: test")
// err = os.WriteFile("test1.yaml", yamlContent1, 0644)
// if err != nil {
// t.Fatalf("Failed to write test1.yaml: %v", err)
// }
// err = os.WriteFile("test1.yaml", yamlContent1, 0644)
// if err != nil {
// t.Fatalf("Failed to write test1.yaml: %v", err)
// }
// err = os.WriteFile("test2.yaml", yamlContent2, 0644)
// if err != nil {
// t.Fatalf("Failed to write test2.yaml: %v", err)
// }
// err = os.WriteFile("test2.yaml", yamlContent2, 0644)
// if err != nil {
// t.Fatalf("Failed to write test2.yaml: %v", err)
// }
// // Mock LoadCommandsFromCookFile to return expected commands
// originalLoadFunc := LoadCommandsFromCookFile
// defer func() { LoadCommandsFromCookFile = originalLoadFunc }()
// // Mock LoadCommandsFromCookFile to return expected commands
// originalLoadFunc := LoadCommandsFromCookFile
// defer func() { LoadCommandsFromCookFile = originalLoadFunc }()
// LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) {
// var result []ModifyCommand
// if string(data) == string(yamlContent1) {
// result = []ModifyCommand{{Name: "command1", Type: "test"}}
// } else if string(data) == string(yamlContent2) {
// result = []ModifyCommand{{Name: "command2", Type: "test"}}
// }
// return result, nil
// }
// LoadCommandsFromCookFile = func(data []byte) ([]ModifyCommand, error) {
// var result []ModifyCommand
// if string(data) == string(yamlContent1) {
// result = []ModifyCommand{{Name: "command1", Type: "test"}}
// } else if string(data) == string(yamlContent2) {
// result = []ModifyCommand{{Name: "command2", Type: "test"}}
// }
// return result, nil
// }
// // Execute function
// commands, err := LoadCommandsFromCookFiles("")
// // Execute function
// commands, err := LoadCommandsFromCookFiles("")
// // Assertions
// if err != nil {
// t.Errorf("Expected no error, got: %v", err)
// }
// // Assertions
// if err != nil {
// t.Errorf("Expected no error, got: %v", err)
// }
// if len(commands) != 2 {
// t.Errorf("Expected 2 commands, got: %d", len(commands))
// }
// if len(commands) != 2 {
// t.Errorf("Expected 2 commands, got: %d", len(commands))
// }
// expectedNames := []string{"command1", "command2"}
// for i, cmd := range commands {
// if cmd.Name != expectedNames[i] {
// t.Errorf("Expected command name %s, got: %s", expectedNames[i], cmd.Name)
// }
// }
// expectedNames := []string{"command1", "command2"}
// for i, cmd := range commands {
// if cmd.Name != expectedNames[i] {
// t.Errorf("Expected command name %s, got: %s", expectedNames[i], cmd.Name)
// }
// }
// }
// No YAML files exist in the current directory