Rename pattern to regex
This commit is contained in:
4
main.go
4
main.go
@@ -121,10 +121,10 @@ func main() {
|
|||||||
// Aggregate all the modifications and execute them
|
// Aggregate all the modifications and execute them
|
||||||
modifications := []utils.ReplaceCommand{}
|
modifications := []utils.ReplaceCommand{}
|
||||||
for _, command := range commands {
|
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)
|
commands, err := processor.ProcessRegex(fileDataStr, command)
|
||||||
if err != nil {
|
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
|
return
|
||||||
}
|
}
|
||||||
modifications = append(modifications, commands...)
|
modifications = append(modifications, commands...)
|
||||||
|
@@ -23,14 +23,14 @@ type CaptureGroup struct {
|
|||||||
// ProcessContent applies regex replacement with Lua processing
|
// ProcessContent applies regex replacement with Lua processing
|
||||||
func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceCommand, error) {
|
func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceCommand, error) {
|
||||||
var commands []utils.ReplaceCommand
|
var commands []utils.ReplaceCommand
|
||||||
logger.Trace("Processing regex: %q", command.Pattern)
|
logger.Trace("Processing regex: %q", command.Regex)
|
||||||
|
|
||||||
// Start timing the regex processing
|
// Start timing the regex processing
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
|
|
||||||
// We don't HAVE to do this multiple times for a pattern
|
// We don't HAVE to do this multiple times for a pattern
|
||||||
// But it's quick enough for us to not care
|
// 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)
|
logger.Debug("Compiling regex pattern: %s", pattern)
|
||||||
|
|
||||||
patternCompileStart := time.Now()
|
patternCompileStart := time.Now()
|
||||||
|
@@ -33,7 +33,7 @@ func normalizeWhitespace(s string) string {
|
|||||||
|
|
||||||
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
|
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
|
||||||
command := utils.ModifyCommand{
|
command := utils.ModifyCommand{
|
||||||
Pattern: regex,
|
Regex: regex,
|
||||||
Lua: lua,
|
Lua: lua,
|
||||||
LogLevel: "TRACE",
|
LogLevel: "TRACE",
|
||||||
}
|
}
|
||||||
|
@@ -10,7 +10,7 @@ import (
|
|||||||
|
|
||||||
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
|
func ApiAdaptor(content string, regex string, lua string) (string, int, int, error) {
|
||||||
command := utils.ModifyCommand{
|
command := utils.ModifyCommand{
|
||||||
Pattern: regex,
|
Regex: regex,
|
||||||
Lua: lua,
|
Lua: lua,
|
||||||
LogLevel: "TRACE",
|
LogLevel: "TRACE",
|
||||||
}
|
}
|
||||||
|
@@ -11,7 +11,7 @@ import (
|
|||||||
|
|
||||||
type ModifyCommand struct {
|
type ModifyCommand struct {
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
Pattern string `yaml:"pattern"`
|
Regex string `yaml:"regex"`
|
||||||
Lua string `yaml:"lua"`
|
Lua string `yaml:"lua"`
|
||||||
Files []string `yaml:"files"`
|
Files []string `yaml:"files"`
|
||||||
Git bool `yaml:"git"`
|
Git bool `yaml:"git"`
|
||||||
@@ -21,7 +21,7 @@ type ModifyCommand struct {
|
|||||||
type CookFile []ModifyCommand
|
type CookFile []ModifyCommand
|
||||||
|
|
||||||
func (c *ModifyCommand) Validate() error {
|
func (c *ModifyCommand) Validate() error {
|
||||||
if c.Pattern == "" {
|
if c.Regex == "" {
|
||||||
return fmt.Errorf("pattern is required")
|
return fmt.Errorf("pattern is required")
|
||||||
}
|
}
|
||||||
if c.Lua == "" {
|
if c.Lua == "" {
|
||||||
@@ -49,7 +49,7 @@ func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[s
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if matches {
|
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)
|
fileCommands[file] = append(fileCommands[file], command)
|
||||||
associationCount++
|
associationCount++
|
||||||
}
|
}
|
||||||
@@ -147,7 +147,7 @@ func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
command := ModifyCommand{
|
command := ModifyCommand{
|
||||||
Pattern: args[0],
|
Regex: args[0],
|
||||||
Lua: args[1],
|
Lua: args[1],
|
||||||
Files: args[2:],
|
Files: args[2:],
|
||||||
Git: *GitFlag,
|
Git: *GitFlag,
|
||||||
|
@@ -17,7 +17,7 @@ func TestModifyCommandValidate(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "Valid command",
|
name: "Valid command",
|
||||||
command: ModifyCommand{
|
command: ModifyCommand{
|
||||||
Pattern: "test pattern",
|
Regex: "test pattern",
|
||||||
Lua: "test expression",
|
Lua: "test expression",
|
||||||
Files: []string{"file1", "file2"},
|
Files: []string{"file1", "file2"},
|
||||||
LogLevel: "INFO",
|
LogLevel: "INFO",
|
||||||
@@ -27,7 +27,7 @@ func TestModifyCommandValidate(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "Missing pattern",
|
name: "Missing pattern",
|
||||||
command: ModifyCommand{
|
command: ModifyCommand{
|
||||||
Pattern: "",
|
Regex: "",
|
||||||
Lua: "test expression",
|
Lua: "test expression",
|
||||||
Files: []string{"file1", "file2"},
|
Files: []string{"file1", "file2"},
|
||||||
LogLevel: "INFO",
|
LogLevel: "INFO",
|
||||||
@@ -37,7 +37,7 @@ func TestModifyCommandValidate(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "Missing LuaExpr",
|
name: "Missing LuaExpr",
|
||||||
command: ModifyCommand{
|
command: ModifyCommand{
|
||||||
Pattern: "test pattern",
|
Regex: "test pattern",
|
||||||
Lua: "",
|
Lua: "",
|
||||||
Files: []string{"file1", "file2"},
|
Files: []string{"file1", "file2"},
|
||||||
LogLevel: "INFO",
|
LogLevel: "INFO",
|
||||||
@@ -47,7 +47,7 @@ func TestModifyCommandValidate(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "Missing files",
|
name: "Missing files",
|
||||||
command: ModifyCommand{
|
command: ModifyCommand{
|
||||||
Pattern: "test pattern",
|
Regex: "test pattern",
|
||||||
Lua: "test expression",
|
Lua: "test expression",
|
||||||
Files: []string{},
|
Files: []string{},
|
||||||
LogLevel: "INFO",
|
LogLevel: "INFO",
|
||||||
@@ -57,7 +57,7 @@ func TestModifyCommandValidate(t *testing.T) {
|
|||||||
{
|
{
|
||||||
name: "Default log level",
|
name: "Default log level",
|
||||||
command: ModifyCommand{
|
command: ModifyCommand{
|
||||||
Pattern: "test pattern",
|
Regex: "test pattern",
|
||||||
Lua: "test expression",
|
Lua: "test expression",
|
||||||
Files: []string{"file1", "file2"},
|
Files: []string{"file1", "file2"},
|
||||||
LogLevel: "",
|
LogLevel: "",
|
||||||
@@ -128,17 +128,17 @@ func TestAssociateFilesWithCommands(t *testing.T) {
|
|||||||
// Define commands with different globs
|
// Define commands with different globs
|
||||||
commands := []ModifyCommand{
|
commands := []ModifyCommand{
|
||||||
{
|
{
|
||||||
Pattern: "pattern1",
|
Regex: "pattern1",
|
||||||
Lua: "expr1",
|
Lua: "expr1",
|
||||||
Files: []string{"*.xml"},
|
Files: []string{"*.xml"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Pattern: "pattern2",
|
Regex: "pattern2",
|
||||||
Lua: "expr2",
|
Lua: "expr2",
|
||||||
Files: []string{"*.txt"},
|
Files: []string{"*.txt"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Pattern: "pattern3",
|
Regex: "pattern3",
|
||||||
Lua: "expr3",
|
Lua: "expr3",
|
||||||
Files: []string{"subdir/*"},
|
Files: []string{"subdir/*"},
|
||||||
},
|
},
|
||||||
@@ -161,7 +161,7 @@ func TestAssociateFilesWithCommands(t *testing.T) {
|
|||||||
for file, cmds := range associations {
|
for file, cmds := range associations {
|
||||||
t.Logf("File %s is associated with %d commands", file, len(cmds))
|
t.Logf("File %s is associated with %d commands", file, len(cmds))
|
||||||
for i, cmd := range 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
|
// Specific validation based on our file types
|
||||||
@@ -230,17 +230,17 @@ func TestAssociateFilesWithCommands(t *testing.T) {
|
|||||||
func TestAggregateGlobs(t *testing.T) {
|
func TestAggregateGlobs(t *testing.T) {
|
||||||
commands := []ModifyCommand{
|
commands := []ModifyCommand{
|
||||||
{
|
{
|
||||||
Pattern: "pattern1",
|
Regex: "pattern1",
|
||||||
Lua: "expr1",
|
Lua: "expr1",
|
||||||
Files: []string{"*.xml", "*.txt"},
|
Files: []string{"*.xml", "*.txt"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Pattern: "pattern2",
|
Regex: "pattern2",
|
||||||
Lua: "expr2",
|
Lua: "expr2",
|
||||||
Files: []string{"*.xml", "*.json"},
|
Files: []string{"*.xml", "*.json"},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Pattern: "pattern3",
|
Regex: "pattern3",
|
||||||
Lua: "expr3",
|
Lua: "expr3",
|
||||||
Files: []string{"subdir/*.xml"},
|
Files: []string{"subdir/*.xml"},
|
||||||
},
|
},
|
||||||
@@ -359,8 +359,8 @@ func TestLoadCommandFromArgs(t *testing.T) {
|
|||||||
cmd := commands[0]
|
cmd := commands[0]
|
||||||
|
|
||||||
// Check command properties
|
// Check command properties
|
||||||
if cmd.Pattern != tc.args[0] {
|
if cmd.Regex != tc.args[0] {
|
||||||
t.Errorf("Expected pattern %q, got %q", tc.args[0], cmd.Pattern)
|
t.Errorf("Expected pattern %q, got %q", tc.args[0], cmd.Regex)
|
||||||
}
|
}
|
||||||
|
|
||||||
if cmd.Lua != tc.args[1] {
|
if cmd.Lua != tc.args[1] {
|
||||||
@@ -407,10 +407,10 @@ func TestLoadCommandsFromCookFileSuccess(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, commands, 2)
|
assert.Len(t, commands, 2)
|
||||||
assert.Equal(t, "command1", commands[0].Name)
|
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, "replace", commands[0].Lua)
|
||||||
assert.Equal(t, "command2", commands[1].Name)
|
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, "delete", commands[1].Lua)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -435,10 +435,10 @@ func TestLoadCommandsFromCookFileWithComments(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, commands, 2)
|
assert.Len(t, commands, 2)
|
||||||
assert.Equal(t, "command1", commands[0].Name)
|
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, "replace", commands[0].Lua)
|
||||||
assert.Equal(t, "command2", commands[1].Name)
|
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, "delete", commands[1].Lua)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -454,10 +454,10 @@ func TestLoadCommandsFromCookFileWithFlowStyle(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, commands, 2)
|
assert.Len(t, commands, 2)
|
||||||
assert.Equal(t, "command1", commands[0].Name)
|
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, "replace", commands[0].Lua)
|
||||||
assert.Equal(t, "command2", commands[1].Name)
|
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, "delete", commands[1].Lua)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -513,13 +513,13 @@ func TestLoadCommandsFromCookFileWithMultipleEntries(t *testing.T) {
|
|||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, commands, 3)
|
assert.Len(t, commands, 3)
|
||||||
assert.Equal(t, "command1", commands[0].Name)
|
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, "replace", commands[0].Lua)
|
||||||
assert.Equal(t, "command2", commands[1].Name)
|
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, "delete", commands[1].Lua)
|
||||||
assert.Equal(t, "command3", commands[2].Name)
|
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)
|
assert.Equal(t, "append", commands[2].Lua)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -582,7 +582,7 @@ func TestLoadCommandFromArgsWithValidArguments(t *testing.T) {
|
|||||||
// Assert
|
// Assert
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, commands, 1)
|
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, "return x", commands[0].Lua)
|
||||||
assert.Equal(t, []string{"file1.go", "file2.go"}, commands[0].Files)
|
assert.Equal(t, []string{"file1.go", "file2.go"}, commands[0].Files)
|
||||||
assert.Equal(t, true, commands[0].Git)
|
assert.Equal(t, true, commands[0].Git)
|
||||||
@@ -662,7 +662,7 @@ func TestLoadCommandFromArgsPopulatesFieldsCorrectly(t *testing.T) {
|
|||||||
// Assert
|
// Assert
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Len(t, commands, 1)
|
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, "print('Hello')", commands[0].Lua)
|
||||||
assert.Equal(t, []string{"file1.txt", "file2.txt"}, commands[0].Files)
|
assert.Equal(t, []string{"file1.txt", "file2.txt"}, commands[0].Files)
|
||||||
assert.Equal(t, false, commands[0].Git)
|
assert.Equal(t, false, commands[0].Git)
|
||||||
|
Reference in New Issue
Block a user