Compare commits
5 Commits
Author | SHA1 | Date | |
---|---|---|---|
633eebfd2a | |||
5a31703840 | |||
162d0c758d | |||
14d64495b6 | |||
fe6e97e832 |
38
main.go
38
main.go
@@ -118,6 +118,30 @@ func main() {
|
||||
startTime := time.Now()
|
||||
var fileMutex sync.Mutex
|
||||
|
||||
// Create a map to store loggers for each command
|
||||
commandLoggers := make(map[string]*logger.Logger)
|
||||
for _, command := range commands {
|
||||
// Create a named logger for each command
|
||||
cmdName := command.Name
|
||||
if cmdName == "" {
|
||||
// If no name is provided, use a short version of the regex pattern
|
||||
if len(command.Regex) > 20 {
|
||||
cmdName = command.Regex[:17] + "..."
|
||||
} else {
|
||||
cmdName = command.Regex
|
||||
}
|
||||
}
|
||||
|
||||
// Parse the log level for this specific command
|
||||
cmdLogLevel := logger.ParseLevel(command.LogLevel)
|
||||
|
||||
// Create a logger with the command name as a field
|
||||
commandLoggers[command.Name] = logger.WithField("command", cmdName)
|
||||
commandLoggers[command.Name].SetLevel(cmdLogLevel)
|
||||
|
||||
logger.Debug("Created logger for command %q with log level %s", cmdName, cmdLogLevel.String())
|
||||
}
|
||||
|
||||
// This aggregation is great but what if one modification replaces the whole entire file?
|
||||
// Shit......
|
||||
// TODO: Add "Isolate" field to modifications which makes them run alone
|
||||
@@ -144,7 +168,7 @@ func main() {
|
||||
return
|
||||
}
|
||||
|
||||
fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex)
|
||||
fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex, commandLoggers)
|
||||
if err != nil {
|
||||
logger.Error("Failed to run other commands for file %q: %v", file, err)
|
||||
return
|
||||
@@ -230,11 +254,17 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
func RunOtherCommands(file string, fileDataStr string, association utils.FileCommandAssociation, fileMutex *sync.Mutex) (string, error) {
|
||||
func RunOtherCommands(file string, fileDataStr string, association utils.FileCommandAssociation, fileMutex *sync.Mutex, commandLoggers map[string]*logger.Logger) (string, error) {
|
||||
// Aggregate all the modifications and execute them
|
||||
modifications := []utils.ReplaceCommand{}
|
||||
for _, command := range association.Commands {
|
||||
logger.Info("Processing file %q with command %q", file, command.Regex)
|
||||
// Use command-specific logger if available, otherwise fall back to default logger
|
||||
cmdLogger := logger.DefaultLogger
|
||||
if cmdLog, ok := commandLoggers[command.Name]; ok {
|
||||
cmdLogger = cmdLog
|
||||
}
|
||||
|
||||
cmdLogger.Info("Processing file %q with command %q", file, command.Regex)
|
||||
newModifications, err := processor.ProcessRegex(fileDataStr, command, file)
|
||||
if err != nil {
|
||||
return fileDataStr, fmt.Errorf("failed to process file %q with command %q: %w", file, command.Regex, err)
|
||||
@@ -248,6 +278,8 @@ func RunOtherCommands(file string, fileDataStr string, association utils.FileCom
|
||||
count = 0
|
||||
}
|
||||
stats.ModificationsPerCommand.Store(command.Name, count.(int)+len(newModifications))
|
||||
|
||||
cmdLogger.Debug("Command %q generated %d modifications", command.Name, len(newModifications))
|
||||
}
|
||||
|
||||
if len(modifications) == 0 {
|
||||
|
@@ -156,7 +156,11 @@ func ProcessRegex(content string, command utils.ModifyCommand, filename string)
|
||||
}
|
||||
}
|
||||
|
||||
// Use the DeduplicateGroups flag to control whether to deduplicate capture groups
|
||||
if !command.NoDedup {
|
||||
logger.Debug("Deduplicating capture groups as specified in command settings")
|
||||
captureGroups = deduplicateGroups(captureGroups)
|
||||
}
|
||||
|
||||
if err := toLua(L, captureGroups); err != nil {
|
||||
logger.Error("Failed to set Lua variables: %v", err)
|
||||
|
@@ -19,6 +19,7 @@ type ModifyCommand struct {
|
||||
Reset bool `yaml:"reset"`
|
||||
LogLevel string `yaml:"loglevel"`
|
||||
Isolate bool `yaml:"isolate"`
|
||||
NoDedup bool `yaml:"nodedup"`
|
||||
}
|
||||
type CookFile []ModifyCommand
|
||||
|
||||
@@ -109,6 +110,7 @@ func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
|
||||
globs := make(map[string]struct{})
|
||||
for _, command := range commands {
|
||||
for _, glob := range command.Files {
|
||||
glob = strings.Replace(glob, "~", os.Getenv("HOME"), 1)
|
||||
globs[glob] = struct{}{}
|
||||
}
|
||||
}
|
||||
|
@@ -158,21 +158,24 @@ func TestAssociateFilesWithCommands(t *testing.T) {
|
||||
|
||||
// The associations expected depends on the implementation
|
||||
// Let's check the actual associations and verify they make sense
|
||||
for file, cmds := range associations {
|
||||
t.Logf("File %s is associated with %d commands", file, len(cmds))
|
||||
for i, cmd := range cmds {
|
||||
for file, assoc := range associations {
|
||||
t.Logf("File %s is associated with %d commands and %d isolate commands", file, len(assoc.Commands), len(assoc.IsolateCommands))
|
||||
for i, cmd := range assoc.Commands {
|
||||
t.Logf(" Command %d: Pattern=%s, Files=%v", i, cmd.Regex, cmd.Files)
|
||||
}
|
||||
for i, cmd := range assoc.IsolateCommands {
|
||||
t.Logf(" Isolate Command %d: Pattern=%s, Files=%v", i, cmd.Regex, cmd.Files)
|
||||
}
|
||||
|
||||
// Specific validation based on our file types
|
||||
switch file {
|
||||
case "file1.xml":
|
||||
if len(cmds) < 1 {
|
||||
t.Errorf("Expected at least 1 command for file1.xml, got %d", len(cmds))
|
||||
if len(assoc.Commands) < 1 {
|
||||
t.Errorf("Expected at least 1 command for file1.xml, got %d", len(assoc.Commands))
|
||||
}
|
||||
// Verify at least one command with *.xml pattern
|
||||
hasXmlGlob := false
|
||||
for _, cmd := range cmds {
|
||||
for _, cmd := range assoc.Commands {
|
||||
for _, glob := range cmd.Files {
|
||||
if glob == "*.xml" {
|
||||
hasXmlGlob = true
|
||||
@@ -187,12 +190,12 @@ func TestAssociateFilesWithCommands(t *testing.T) {
|
||||
t.Errorf("Expected command with *.xml glob for file1.xml")
|
||||
}
|
||||
case "file2.txt":
|
||||
if len(cmds) < 1 {
|
||||
t.Errorf("Expected at least 1 command for file2.txt, got %d", len(cmds))
|
||||
if len(assoc.Commands) < 1 {
|
||||
t.Errorf("Expected at least 1 command for file2.txt, got %d", len(assoc.Commands))
|
||||
}
|
||||
// Verify at least one command with *.txt pattern
|
||||
hasTxtGlob := false
|
||||
for _, cmd := range cmds {
|
||||
for _, cmd := range assoc.Commands {
|
||||
for _, glob := range cmd.Files {
|
||||
if glob == "*.txt" {
|
||||
hasTxtGlob = true
|
||||
@@ -207,12 +210,12 @@ func TestAssociateFilesWithCommands(t *testing.T) {
|
||||
t.Errorf("Expected command with *.txt glob for file2.txt")
|
||||
}
|
||||
case "subdir/file3.xml":
|
||||
if len(cmds) < 1 {
|
||||
t.Errorf("Expected at least 1 command for subdir/file3.xml, got %d", len(cmds))
|
||||
if len(assoc.Commands) < 1 {
|
||||
t.Errorf("Expected at least 1 command for subdir/file3.xml, got %d", len(assoc.Commands))
|
||||
}
|
||||
// Should match both *.xml and subdir/* patterns
|
||||
matches := 0
|
||||
for _, cmd := range cmds {
|
||||
for _, cmd := range assoc.Commands {
|
||||
for _, glob := range cmd.Files {
|
||||
if glob == "*.xml" || glob == "subdir/*" {
|
||||
matches++
|
||||
@@ -393,10 +396,10 @@ func TestLoadCommandsFromCookFileSuccess(t *testing.T) {
|
||||
// Arrange
|
||||
yamlData := []byte(`
|
||||
- name: command1
|
||||
pattern: "*.txt"
|
||||
regex: "*.txt"
|
||||
lua: replace
|
||||
- name: command2
|
||||
pattern: "*.go"
|
||||
regex: "*.go"
|
||||
lua: delete
|
||||
`)
|
||||
|
||||
@@ -420,11 +423,11 @@ func TestLoadCommandsFromCookFileWithComments(t *testing.T) {
|
||||
yamlData := []byte(`
|
||||
# This is a comment
|
||||
- name: command1
|
||||
pattern: "*.txt"
|
||||
regex: "*.txt"
|
||||
lua: replace
|
||||
# Another comment
|
||||
- name: command2
|
||||
pattern: "*.go"
|
||||
regex: "*.go"
|
||||
lua: delete
|
||||
`)
|
||||
|
||||
@@ -445,7 +448,7 @@ func TestLoadCommandsFromCookFileWithComments(t *testing.T) {
|
||||
// Handle different YAML formatting styles (flow vs block)
|
||||
func TestLoadCommandsFromCookFileWithFlowStyle(t *testing.T) {
|
||||
// Arrange
|
||||
yamlData := []byte(`[ { name: command1, pattern: "*.txt", lua: replace }, { name: command2, pattern: "*.go", lua: delete } ]`)
|
||||
yamlData := []byte(`[ { name: command1, regex: "*.txt", lua: replace }, { name: command2, regex: "*.go", lua: delete } ]`)
|
||||
|
||||
// Act
|
||||
commands, err := LoadCommandsFromCookFile(yamlData)
|
||||
@@ -496,13 +499,13 @@ func TestLoadCommandsFromCookFileWithMultipleEntries(t *testing.T) {
|
||||
// Arrange
|
||||
yamlData := []byte(`
|
||||
- name: command1
|
||||
pattern: "*.txt"
|
||||
regex: "*.txt"
|
||||
lua: replace
|
||||
- name: command2
|
||||
pattern: "*.go"
|
||||
regex: "*.go"
|
||||
lua: delete
|
||||
- name: command3
|
||||
pattern: "*.md"
|
||||
regex: "*.md"
|
||||
lua: append
|
||||
`)
|
||||
|
||||
|
Reference in New Issue
Block a user