Compare commits
23 Commits
Author | SHA1 | Date | |
---|---|---|---|
05bc31aa90 | |||
81d69b8ce0 | |||
679322a8ac | |||
2b973be0c1 | |||
d21e20d387 | |||
0fc5300786 | |||
4ff2ee80ee | |||
633eebfd2a | |||
5a31703840 | |||
162d0c758d | |||
14d64495b6 | |||
fe6e97e832 | |||
35b3d8b099 | |||
2e3e958e15 | |||
955afc4295 | |||
2c487bc443 | |||
b77224176b | |||
a2201053c5 | |||
04cedf5ece | |||
ebb07854cc | |||
8a86ae2f40 | |||
e8f16dda2b | |||
513773f641 |
12
.vscode/launch.json
vendored
12
.vscode/launch.json
vendored
@@ -12,11 +12,10 @@
|
|||||||
"program": "${workspaceFolder}",
|
"program": "${workspaceFolder}",
|
||||||
"cwd": "C:/Users/Administrator/Seafile/Games-Barotrauma",
|
"cwd": "C:/Users/Administrator/Seafile/Games-Barotrauma",
|
||||||
"args": [
|
"args": [
|
||||||
"loglevel",
|
"-loglevel",
|
||||||
"trace",
|
"trace",
|
||||||
"(?-s)LightComponent!anyrange=\"(!num)\"",
|
"-cook",
|
||||||
"*4",
|
"*.yml",
|
||||||
"**/Outpost*.xml"
|
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@@ -42,9 +41,8 @@
|
|||||||
"args": [
|
"args": [
|
||||||
"-loglevel",
|
"-loglevel",
|
||||||
"trace",
|
"trace",
|
||||||
"(?-s)LightComponent!anyrange=\"(!num)\"",
|
"-cook",
|
||||||
"*4",
|
"cookscoop.yml",
|
||||||
"**/Outpost*.xml"
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
178
main.go
178
main.go
@@ -4,6 +4,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -20,14 +21,14 @@ type GlobalStats struct {
|
|||||||
TotalModifications int
|
TotalModifications int
|
||||||
ProcessedFiles int
|
ProcessedFiles int
|
||||||
FailedFiles int
|
FailedFiles int
|
||||||
ModificationsPerCommand map[string]int
|
ModificationsPerCommand sync.Map
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
repo *git.Repository
|
repo *git.Repository
|
||||||
worktree *git.Worktree
|
worktree *git.Worktree
|
||||||
stats GlobalStats = GlobalStats{
|
stats GlobalStats = GlobalStats{
|
||||||
ModificationsPerCommand: make(map[string]int),
|
ModificationsPerCommand: sync.Map{},
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -52,6 +53,7 @@ func main() {
|
|||||||
fmt.Fprintf(os.Stderr, " You can use any valid Lua code, including if statements, loops, etc.\n")
|
fmt.Fprintf(os.Stderr, " You can use any valid Lua code, including if statements, loops, etc.\n")
|
||||||
fmt.Fprintf(os.Stderr, " Glob patterns are supported for file selection (*.xml, data/**.xml, etc.)\n")
|
fmt.Fprintf(os.Stderr, " Glob patterns are supported for file selection (*.xml, data/**.xml, etc.)\n")
|
||||||
}
|
}
|
||||||
|
// TODO: Fix bed shitting when doing *.yml in barotrauma directory
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
args := flag.Args()
|
args := flag.Args()
|
||||||
|
|
||||||
@@ -68,10 +70,27 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if *utils.Filter != "" {
|
||||||
|
logger.Info("Filtering commands by name: %s", *utils.Filter)
|
||||||
|
commands = utils.FilterCommands(commands, *utils.Filter)
|
||||||
|
logger.Info("Filtered %d commands", len(commands))
|
||||||
|
}
|
||||||
|
|
||||||
// Then aggregate all the globs and deduplicate them
|
// Then aggregate all the globs and deduplicate them
|
||||||
globs := utils.AggregateGlobs(commands)
|
globs := utils.AggregateGlobs(commands)
|
||||||
logger.Debug("Aggregated %d globs before deduplication", utils.CountGlobsBeforeDedup(commands))
|
logger.Debug("Aggregated %d globs before deduplication", utils.CountGlobsBeforeDedup(commands))
|
||||||
|
|
||||||
|
for _, command := range commands {
|
||||||
|
logger.Trace("Command: %s", command.Name)
|
||||||
|
logger.Trace("Regex: %s", command.Regex)
|
||||||
|
logger.Trace("Files: %v", command.Files)
|
||||||
|
logger.Trace("Lua: %s", command.Lua)
|
||||||
|
logger.Trace("Git: %t", command.Git)
|
||||||
|
logger.Trace("Reset: %t", command.Reset)
|
||||||
|
logger.Trace("Isolate: %t", command.Isolate)
|
||||||
|
logger.Trace("LogLevel: %s", command.LogLevel)
|
||||||
|
}
|
||||||
|
|
||||||
// Resolve all the files for all the globs
|
// Resolve all the files for all the globs
|
||||||
logger.Info("Found %d unique file patterns", len(globs))
|
logger.Info("Found %d unique file patterns", len(globs))
|
||||||
files, err := utils.ExpandGLobs(globs)
|
files, err := utils.ExpandGLobs(globs)
|
||||||
@@ -91,7 +110,6 @@ func main() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Utilize parallel workers for this
|
|
||||||
// Then for each file run all commands associated with the file
|
// Then for each file run all commands associated with the file
|
||||||
workers := make(chan struct{}, *utils.ParallelFiles)
|
workers := make(chan struct{}, *utils.ParallelFiles)
|
||||||
wg := sync.WaitGroup{}
|
wg := sync.WaitGroup{}
|
||||||
@@ -100,7 +118,34 @@ func main() {
|
|||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
var fileMutex sync.Mutex
|
var fileMutex sync.Mutex
|
||||||
|
|
||||||
for file, commands := range associations {
|
// 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
|
||||||
|
for file, association := range associations {
|
||||||
workers <- struct{}{}
|
workers <- struct{}{}
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
logger.SafeGoWithArgs(func(args ...interface{}) {
|
logger.SafeGoWithArgs(func(args ...interface{}) {
|
||||||
@@ -115,39 +160,19 @@ func main() {
|
|||||||
logger.Error("Failed to read file %q: %v", file, err)
|
logger.Error("Failed to read file %q: %v", file, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Trace("Loaded %d bytes of data for file %q", len(fileData), file)
|
|
||||||
fileDataStr := string(fileData)
|
fileDataStr := string(fileData)
|
||||||
|
|
||||||
// Aggregate all the modifications and execute them
|
fileDataStr, err = RunIsolateCommands(association, file, fileDataStr, &fileMutex)
|
||||||
modifications := []utils.ReplaceCommand{}
|
|
||||||
for _, command := range commands {
|
|
||||||
logger.Info("Processing file %q with command %q", file, command.Regex)
|
|
||||||
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.Regex, err)
|
logger.Error("Failed to run isolate commands for file %q: %v", file, err)
|
||||||
return
|
|
||||||
}
|
|
||||||
modifications = append(modifications, commands...)
|
|
||||||
// It is not guranteed that all the commands will be executed...
|
|
||||||
// TODO: Make this better
|
|
||||||
// We'd have to pass the map to executemodifications or something...
|
|
||||||
stats.ModificationsPerCommand[command.Name] += len(commands)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(modifications) == 0 {
|
|
||||||
logger.Info("No modifications found for file %q", file)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort commands in reverse order for safe replacements
|
fileDataStr, err = RunOtherCommands(file, fileDataStr, association, &fileMutex, commandLoggers)
|
||||||
fileDataStr, count := utils.ExecuteModifications(modifications, fileDataStr)
|
if err != nil {
|
||||||
|
logger.Error("Failed to run other commands for file %q: %v", file, err)
|
||||||
fileMutex.Lock()
|
return
|
||||||
stats.ProcessedFiles++
|
}
|
||||||
stats.TotalModifications += count
|
|
||||||
fileMutex.Unlock()
|
|
||||||
|
|
||||||
logger.Info("Executed %d modifications for file %q", count, file)
|
|
||||||
|
|
||||||
err = os.WriteFile(file, []byte(fileDataStr), 0644)
|
err = os.WriteFile(file, []byte(fileDataStr), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -208,14 +233,95 @@ func main() {
|
|||||||
// Print summary
|
// Print summary
|
||||||
if stats.TotalModifications == 0 {
|
if stats.TotalModifications == 0 {
|
||||||
logger.Warning("No modifications were made in any files")
|
logger.Warning("No modifications were made in any files")
|
||||||
fmt.Fprintf(os.Stderr, "No modifications were made in any files\n")
|
|
||||||
} else {
|
} else {
|
||||||
logger.Info("Operation complete! Modified %d values in %d/%d files",
|
logger.Info("Operation complete! Modified %d values in %d/%d files",
|
||||||
stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles)
|
stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles)
|
||||||
fmt.Printf("Operation complete! Modified %d values in %d/%d files\n",
|
sortedCommands := []string{}
|
||||||
stats.TotalModifications, stats.ProcessedFiles, stats.ProcessedFiles+stats.FailedFiles)
|
stats.ModificationsPerCommand.Range(func(key, value interface{}) bool {
|
||||||
for command, count := range stats.ModificationsPerCommand {
|
sortedCommands = append(sortedCommands, key.(string))
|
||||||
logger.Info("Command %q made %d modifications", command, count)
|
return true
|
||||||
|
})
|
||||||
|
sort.Strings(sortedCommands)
|
||||||
|
|
||||||
|
for _, command := range sortedCommands {
|
||||||
|
count, _ := stats.ModificationsPerCommand.Load(command)
|
||||||
|
if count.(int) > 0 {
|
||||||
|
logger.Info("\tCommand %q made %d modifications", command, count)
|
||||||
|
} else {
|
||||||
|
logger.Warning("\tCommand %q made no modifications", command)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
// 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)
|
||||||
|
}
|
||||||
|
modifications = append(modifications, newModifications...)
|
||||||
|
// It is not guranteed that all the commands will be executed...
|
||||||
|
// TODO: Make this better
|
||||||
|
// We'd have to pass the map to executemodifications or something...
|
||||||
|
count, ok := stats.ModificationsPerCommand.Load(command.Name)
|
||||||
|
if !ok {
|
||||||
|
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 {
|
||||||
|
logger.Info("No modifications found for file %q", file)
|
||||||
|
return fileDataStr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sort commands in reverse order for safe replacements
|
||||||
|
var count int
|
||||||
|
fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr)
|
||||||
|
|
||||||
|
fileMutex.Lock()
|
||||||
|
stats.ProcessedFiles++
|
||||||
|
stats.TotalModifications += count
|
||||||
|
fileMutex.Unlock()
|
||||||
|
|
||||||
|
logger.Info("Executed %d modifications for file %q", count, file)
|
||||||
|
return fileDataStr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func RunIsolateCommands(association utils.FileCommandAssociation, file string, fileDataStr string, fileMutex *sync.Mutex) (string, error) {
|
||||||
|
for _, isolateCommand := range association.IsolateCommands {
|
||||||
|
logger.Info("Processing file %q with isolate command %q", file, isolateCommand.Regex)
|
||||||
|
modifications, err := processor.ProcessRegex(fileDataStr, isolateCommand, file)
|
||||||
|
if err != nil {
|
||||||
|
return fileDataStr, fmt.Errorf("failed to process file %q with isolate command %q: %w", file, isolateCommand.Regex, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(modifications) == 0 {
|
||||||
|
logger.Warning("No modifications found for file %q", file)
|
||||||
|
return fileDataStr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
var count int
|
||||||
|
fileDataStr, count = utils.ExecuteModifications(modifications, fileDataStr)
|
||||||
|
|
||||||
|
fileMutex.Lock()
|
||||||
|
stats.ProcessedFiles++
|
||||||
|
stats.TotalModifications += count
|
||||||
|
fileMutex.Unlock()
|
||||||
|
|
||||||
|
logger.Info("Executed %d isolate modifications for file %q", count, file)
|
||||||
|
}
|
||||||
|
return fileDataStr, nil
|
||||||
|
}
|
||||||
|
@@ -21,7 +21,9 @@ 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) {
|
// The filename here exists ONLY so we can pass it to the lua environment
|
||||||
|
// It's not used for anything else
|
||||||
|
func ProcessRegex(content string, command utils.ModifyCommand, filename string) ([]utils.ReplaceCommand, error) {
|
||||||
var commands []utils.ReplaceCommand
|
var commands []utils.ReplaceCommand
|
||||||
logger.Trace("Processing regex: %q", command.Regex)
|
logger.Trace("Processing regex: %q", command.Regex)
|
||||||
|
|
||||||
@@ -31,6 +33,11 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
|
|||||||
// 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.Regex)
|
pattern := resolveRegexPlaceholders(command.Regex)
|
||||||
|
// I'm not too happy about having to trim regex, we could have meaningful whitespace or newlines
|
||||||
|
// But it's a compromise that allows us to use | in yaml
|
||||||
|
// Otherwise we would have to escape every god damn pair of quotation marks
|
||||||
|
// And a bunch of other shit
|
||||||
|
pattern = strings.TrimSpace(pattern)
|
||||||
logger.Debug("Compiling regex pattern: %s", pattern)
|
logger.Debug("Compiling regex pattern: %s", pattern)
|
||||||
|
|
||||||
patternCompileStart := time.Now()
|
patternCompileStart := time.Now()
|
||||||
@@ -79,6 +86,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
|
|||||||
logger.Error("Error creating Lua state: %v", err)
|
logger.Error("Error creating Lua state: %v", err)
|
||||||
return commands, fmt.Errorf("error creating Lua state: %v", err)
|
return commands, fmt.Errorf("error creating Lua state: %v", err)
|
||||||
}
|
}
|
||||||
|
L.SetGlobal("file", lua.LString(filename))
|
||||||
// Hmm... Maybe we don't want to defer this..
|
// Hmm... Maybe we don't want to defer this..
|
||||||
// Maybe we want to close them every iteration
|
// Maybe we want to close them every iteration
|
||||||
// We'll leave it as is for now
|
// We'll leave it as is for now
|
||||||
@@ -153,7 +161,11 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
captureGroups = deduplicateGroups(captureGroups)
|
||||||
|
}
|
||||||
|
|
||||||
if err := toLua(L, captureGroups); err != nil {
|
if err := toLua(L, captureGroups); err != nil {
|
||||||
logger.Error("Failed to set Lua variables: %v", err)
|
logger.Error("Failed to set Lua variables: %v", err)
|
||||||
@@ -205,7 +217,7 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
|
|||||||
|
|
||||||
for _, capture := range captureGroups {
|
for _, capture := range captureGroups {
|
||||||
if capture.Value == capture.Updated {
|
if capture.Value == capture.Updated {
|
||||||
logger.Info("Capture group unchanged: %s", capture.Value)
|
logger.Info("Capture group unchanged: %s", LimitString(capture.Value, 50))
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,7 +262,7 @@ func deduplicateGroups(captureGroups []*CaptureGroup) []*CaptureGroup {
|
|||||||
}
|
}
|
||||||
if overlaps {
|
if overlaps {
|
||||||
// We CAN just continue despite this fuckup
|
// We CAN just continue despite this fuckup
|
||||||
logger.Error("Overlapping capture group: %s", group.Name)
|
logger.Warning("Overlapping capture group: %s", group.Name)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
logger.Debug("No overlap detected for capture group: %s. Adding to deduplicated groups.", group.Name)
|
logger.Debug("No overlap detected for capture group: %s. Adding to deduplicated groups.", group.Name)
|
||||||
@@ -268,8 +280,6 @@ func resolveRegexPlaceholders(pattern string) string {
|
|||||||
// Handle special pattern modifications
|
// Handle special pattern modifications
|
||||||
if !strings.HasPrefix(pattern, "(?s)") {
|
if !strings.HasPrefix(pattern, "(?s)") {
|
||||||
pattern = "(?s)" + pattern
|
pattern = "(?s)" + pattern
|
||||||
// Use fmt.Printf for test compatibility
|
|
||||||
fmt.Printf("Pattern modified to include (?s): %s\n", pattern)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
namedGroupNum := regexp.MustCompile(`(?:(\?<[^>]+>)(!num))`)
|
namedGroupNum := regexp.MustCompile(`(?:(\?<[^>]+>)(!num))`)
|
||||||
|
@@ -2,7 +2,6 @@ package processor
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"modify/utils"
|
"modify/utils"
|
||||||
"os"
|
"os"
|
||||||
@@ -38,7 +37,7 @@ func ApiAdaptor(content string, regex string, lua string) (string, int, int, err
|
|||||||
LogLevel: "TRACE",
|
LogLevel: "TRACE",
|
||||||
}
|
}
|
||||||
|
|
||||||
commands, err := ProcessRegex(content, command)
|
commands, err := ProcessRegex(content, command, "test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", 0, 0, err
|
return "", 0, 0, err
|
||||||
}
|
}
|
||||||
@@ -979,29 +978,12 @@ func TestPatternWithoutPrefixGetsModified(t *testing.T) {
|
|||||||
// Setup
|
// Setup
|
||||||
pattern := "some.*pattern"
|
pattern := "some.*pattern"
|
||||||
|
|
||||||
// Redirect stdout to capture fmt.Printf output
|
|
||||||
oldStdout := os.Stdout
|
|
||||||
r, w, _ := os.Pipe()
|
|
||||||
os.Stdout = w
|
|
||||||
|
|
||||||
// Execute function
|
// Execute function
|
||||||
result := resolveRegexPlaceholders(pattern)
|
result := resolveRegexPlaceholders(pattern)
|
||||||
|
|
||||||
// Restore stdout
|
|
||||||
w.Close()
|
|
||||||
os.Stdout = oldStdout
|
|
||||||
|
|
||||||
// Read captured output
|
|
||||||
var buf bytes.Buffer
|
|
||||||
io.Copy(&buf, r)
|
|
||||||
output := buf.String()
|
|
||||||
|
|
||||||
// Verify results
|
// Verify results
|
||||||
expectedPattern := "(?s)some.*pattern"
|
expectedPattern := "(?s)some.*pattern"
|
||||||
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
||||||
|
|
||||||
expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern)
|
|
||||||
assert.Equal(t, expectedOutput, output, "Expected output message %q, got %q", expectedOutput, output)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Empty string input returns "(?s)"
|
// Empty string input returns "(?s)"
|
||||||
@@ -1009,29 +991,12 @@ func TestEmptyStringReturnsWithPrefix(t *testing.T) {
|
|||||||
// Setup
|
// Setup
|
||||||
pattern := ""
|
pattern := ""
|
||||||
|
|
||||||
// Redirect stdout to capture fmt.Printf output
|
|
||||||
oldStdout := os.Stdout
|
|
||||||
r, w, _ := os.Pipe()
|
|
||||||
os.Stdout = w
|
|
||||||
|
|
||||||
// Execute function
|
// Execute function
|
||||||
result := resolveRegexPlaceholders(pattern)
|
result := resolveRegexPlaceholders(pattern)
|
||||||
|
|
||||||
// Restore stdout
|
|
||||||
w.Close()
|
|
||||||
os.Stdout = oldStdout
|
|
||||||
|
|
||||||
// Read captured output
|
|
||||||
var buf bytes.Buffer
|
|
||||||
io.Copy(&buf, r)
|
|
||||||
output := buf.String()
|
|
||||||
|
|
||||||
// Verify results
|
// Verify results
|
||||||
expectedPattern := "(?s)"
|
expectedPattern := "(?s)"
|
||||||
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
assert.Equal(t, expectedPattern, result, "Expected pattern to be %q, got %q", expectedPattern, result)
|
||||||
|
|
||||||
expectedOutput := fmt.Sprintf("Pattern modified to include (?s): %s\n", expectedPattern)
|
|
||||||
assert.Equal(t, expectedOutput, output, "Expected output message %q, got %q", expectedOutput, output)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Named group with "!num" pattern gets replaced with proper regex for numbers
|
// Named group with "!num" pattern gets replaced with proper regex for numbers
|
||||||
|
@@ -15,7 +15,7 @@ func ApiAdaptor(content string, regex string, lua string) (string, int, int, err
|
|||||||
LogLevel: "TRACE",
|
LogLevel: "TRACE",
|
||||||
}
|
}
|
||||||
|
|
||||||
commands, err := processor.ProcessRegex(content, command)
|
commands, err := processor.ProcessRegex(content, command, "test")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", 0, 0, err
|
return "", 0, 0, err
|
||||||
}
|
}
|
||||||
|
@@ -12,4 +12,5 @@ var (
|
|||||||
LogLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE")
|
LogLevel = flag.String("loglevel", "INFO", "Set log level: ERROR, WARNING, INFO, DEBUG, TRACE")
|
||||||
Cookfile = flag.String("cook", "**/cook.yml", "Path to cook config files, can be globbed")
|
Cookfile = flag.String("cook", "**/cook.yml", "Path to cook config files, can be globbed")
|
||||||
ParallelFiles = flag.Int("P", 100, "Number of files to process in parallel")
|
ParallelFiles = flag.Int("P", 100, "Number of files to process in parallel")
|
||||||
|
Filter = flag.String("filter", "", "Filter commands before running them")
|
||||||
)
|
)
|
||||||
|
@@ -4,6 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"modify/logger"
|
"modify/logger"
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/bmatcuk/doublestar/v4"
|
"github.com/bmatcuk/doublestar/v4"
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
@@ -17,6 +19,8 @@ type ModifyCommand struct {
|
|||||||
Git bool `yaml:"git"`
|
Git bool `yaml:"git"`
|
||||||
Reset bool `yaml:"reset"`
|
Reset bool `yaml:"reset"`
|
||||||
LogLevel string `yaml:"loglevel"`
|
LogLevel string `yaml:"loglevel"`
|
||||||
|
Isolate bool `yaml:"isolate"`
|
||||||
|
NoDedup bool `yaml:"nodedup"`
|
||||||
}
|
}
|
||||||
type CookFile []ModifyCommand
|
type CookFile []ModifyCommand
|
||||||
|
|
||||||
@@ -36,29 +40,75 @@ func (c *ModifyCommand) Validate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string][]ModifyCommand, error) {
|
// Ehh.. Not much better... Guess this wasn't the big deal
|
||||||
|
var matchesMemoTable map[string]bool = make(map[string]bool)
|
||||||
|
|
||||||
|
func Matches(path string, glob string) (bool, error) {
|
||||||
|
key := fmt.Sprintf("%s:%s", path, glob)
|
||||||
|
if matches, ok := matchesMemoTable[key]; ok {
|
||||||
|
logger.Debug("Found match for file %q and glob %q in memo table", path, glob)
|
||||||
|
return matches, nil
|
||||||
|
}
|
||||||
|
matches, err := doublestar.Match(glob, path)
|
||||||
|
if err != nil {
|
||||||
|
return false, fmt.Errorf("failed to match glob %s with file %s: %w", glob, path, err)
|
||||||
|
}
|
||||||
|
matchesMemoTable[key] = matches
|
||||||
|
return matches, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type FileCommandAssociation struct {
|
||||||
|
File string
|
||||||
|
IsolateCommands []ModifyCommand
|
||||||
|
Commands []ModifyCommand
|
||||||
|
}
|
||||||
|
|
||||||
|
func AssociateFilesWithCommands(files []string, commands []ModifyCommand) (map[string]FileCommandAssociation, error) {
|
||||||
associationCount := 0
|
associationCount := 0
|
||||||
fileCommands := make(map[string][]ModifyCommand)
|
fileCommands := make(map[string]FileCommandAssociation)
|
||||||
|
|
||||||
for _, file := range files {
|
for _, file := range files {
|
||||||
|
fileCommands[file] = FileCommandAssociation{
|
||||||
|
File: file,
|
||||||
|
IsolateCommands: []ModifyCommand{},
|
||||||
|
Commands: []ModifyCommand{},
|
||||||
|
}
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
for _, glob := range command.Files {
|
for _, glob := range command.Files {
|
||||||
// TODO: Maybe memoize this function call
|
_, pattern, err := FigureOutGlobRoot(glob)
|
||||||
matches, err := doublestar.Match(glob, file)
|
if err != nil {
|
||||||
|
logger.Trace("Failed to figure out glob root for %s: %v", glob, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
file = filepath.Clean(file)
|
||||||
|
file = strings.ReplaceAll(file, "\\", "/")
|
||||||
|
|
||||||
|
matches, err := Matches(file, pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
logger.Trace("Failed to match glob %s with file %s: %v", glob, file, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if matches {
|
if matches {
|
||||||
logger.Debug("Found match for file %q and command %q", file, command.Regex)
|
logger.Debug("Found match for file %q and command %q", file, command.Regex)
|
||||||
fileCommands[file] = append(fileCommands[file], command)
|
association := fileCommands[file]
|
||||||
|
|
||||||
|
if command.Isolate {
|
||||||
|
association.IsolateCommands = append(association.IsolateCommands, command)
|
||||||
|
} else {
|
||||||
|
association.Commands = append(association.Commands, command)
|
||||||
|
}
|
||||||
|
fileCommands[file] = association
|
||||||
associationCount++
|
associationCount++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.Debug("Found %d commands for file %q", len(fileCommands[file]), file)
|
logger.Debug("Found %d commands for file %q", len(fileCommands[file].Commands), file)
|
||||||
if len(fileCommands[file]) == 0 {
|
if len(fileCommands[file].Commands) == 0 {
|
||||||
logger.Info("No commands found for file %q", file)
|
logger.Info("No commands found for file %q", file)
|
||||||
}
|
}
|
||||||
|
if len(fileCommands[file].IsolateCommands) > 0 {
|
||||||
|
logger.Info("Found %d isolate commands for file %q", len(fileCommands[file].IsolateCommands), file)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
logger.Info("Found %d associations between %d files and %d commands", associationCount, len(files), len(commands))
|
logger.Info("Found %d associations between %d files and %d commands", associationCount, len(files), len(commands))
|
||||||
return fileCommands, nil
|
return fileCommands, nil
|
||||||
@@ -69,6 +119,8 @@ func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
|
|||||||
globs := make(map[string]struct{})
|
globs := make(map[string]struct{})
|
||||||
for _, command := range commands {
|
for _, command := range commands {
|
||||||
for _, glob := range command.Files {
|
for _, glob := range command.Files {
|
||||||
|
glob = strings.ReplaceAll(glob, "~", os.Getenv("USERPROFILE"))
|
||||||
|
glob = strings.ReplaceAll(glob, "\\", "/")
|
||||||
globs[glob] = struct{}{}
|
globs[glob] = struct{}{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -76,21 +128,87 @@ func AggregateGlobs(commands []ModifyCommand) map[string]struct{} {
|
|||||||
return globs
|
return globs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FigureOutGlobRoot(inputPattern string) (root, pattern string, err error) {
|
||||||
|
logger.Debug("Starting to figure out glob root for input pattern: %s", inputPattern)
|
||||||
|
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
logger.Error("Failed to get current working directory: %v", err)
|
||||||
|
return "", inputPattern, fmt.Errorf("failed to get current working directory: %w", err)
|
||||||
|
}
|
||||||
|
logger.Trace("Current working directory: %s", cwd)
|
||||||
|
|
||||||
|
root = inputPattern
|
||||||
|
if !filepath.IsAbs(inputPattern) {
|
||||||
|
root = filepath.Join(cwd, inputPattern)
|
||||||
|
logger.Info("Input pattern is not absolute. Using combined path: %s", root)
|
||||||
|
}
|
||||||
|
root = filepath.Clean(root)
|
||||||
|
logger.Debug("Cleaned root path: %s", root)
|
||||||
|
|
||||||
|
// In either case (whatever our root may be), we have to figure out
|
||||||
|
// Where to start, what our FS will be
|
||||||
|
// The best place would be the last sure entry
|
||||||
|
// That is to say the final directory that is not a wildcard
|
||||||
|
|
||||||
|
finalroot := ""
|
||||||
|
// TODO: This will probably explode on linux because oooooooooo we have to be clever oooooooooo / on linux \\ on windows ooooooooooo
|
||||||
|
parts := strings.Split(root, "\\")
|
||||||
|
lastIndex := len(parts) - 1
|
||||||
|
logger.Debug("Split root into parts: %v", parts)
|
||||||
|
|
||||||
|
// In the case our pattern ends with a file (and many of them do)
|
||||||
|
// Look for only the folders, we cannot mount a file as a FS
|
||||||
|
// In any case we have to match files so they have to be the last part
|
||||||
|
for i := 0; i < len(parts)-1; i++ {
|
||||||
|
part := parts[i]
|
||||||
|
logger.Trace("Processing part: %s", part)
|
||||||
|
if part == "*" || part == "**" || part == "?" || part == "[" {
|
||||||
|
lastIndex = i
|
||||||
|
logger.Debug("Found wildcard part: %s, updating lastIndex to: %d", part, lastIndex)
|
||||||
|
break
|
||||||
|
}
|
||||||
|
// We can't use join here because it joins C: and Users as C:Users
|
||||||
|
// Instead of C:/Users/
|
||||||
|
// God damn it
|
||||||
|
if finalroot != "" {
|
||||||
|
finalroot = finalroot + "/" + part
|
||||||
|
} else {
|
||||||
|
finalroot = finalroot + part
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finalroot = filepath.Clean(finalroot)
|
||||||
|
logger.Debug("Final root after processing: %s", finalroot)
|
||||||
|
|
||||||
|
// After all this juggling our pattern is whatever is left after the finalroot
|
||||||
|
// Which is, in "worst" case, only a file
|
||||||
|
pattern = strings.Join(parts[lastIndex:], "/")
|
||||||
|
logger.Info("Determined pattern: %s", pattern)
|
||||||
|
|
||||||
|
return finalroot, pattern, nil
|
||||||
|
}
|
||||||
|
|
||||||
func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
|
func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
|
||||||
var files []string
|
var files []string
|
||||||
filesMap := make(map[string]bool)
|
filesMap := make(map[string]bool)
|
||||||
|
|
||||||
cwd, err := os.Getwd()
|
for pattern := range patterns {
|
||||||
|
root, pattern, err := FigureOutGlobRoot(pattern)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get current working directory: %w", err)
|
return nil, fmt.Errorf("failed to figure out glob root: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
logger.Debug("Expanding patterns from directory: %s", cwd)
|
|
||||||
for pattern := range patterns {
|
|
||||||
logger.Trace("Processing pattern: %s", pattern)
|
logger.Trace("Processing pattern: %s", pattern)
|
||||||
matches, _ := doublestar.Glob(os.DirFS(cwd), pattern)
|
matches, err := doublestar.Glob(os.DirFS(root), pattern)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("failed to glob pattern %s: %w", pattern, err)
|
||||||
|
}
|
||||||
logger.Debug("Found %d matches for pattern %s", len(matches), pattern)
|
logger.Debug("Found %d matches for pattern %s", len(matches), pattern)
|
||||||
for _, m := range matches {
|
for _, m := range matches {
|
||||||
|
m = filepath.Join(root, m)
|
||||||
|
m = filepath.Clean(m)
|
||||||
|
m = strings.ReplaceAll(m, "\\", "/")
|
||||||
|
|
||||||
info, err := os.Stat(m)
|
info, err := os.Stat(m)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Warning("Error getting file info for %s: %v", m, err)
|
logger.Warning("Error getting file info for %s: %v", m, err)
|
||||||
@@ -206,3 +324,16 @@ func CountGlobsBeforeDedup(commands []ModifyCommand) int {
|
|||||||
}
|
}
|
||||||
return count
|
return count
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func FilterCommands(commands []ModifyCommand, filter string) []ModifyCommand {
|
||||||
|
filteredCommands := []ModifyCommand{}
|
||||||
|
filters := strings.Split(filter, ",")
|
||||||
|
for _, cmd := range commands {
|
||||||
|
for _, filter := range filters {
|
||||||
|
if strings.Contains(cmd.Name, filter) {
|
||||||
|
filteredCommands = append(filteredCommands, cmd)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return filteredCommands
|
||||||
|
}
|
||||||
|
@@ -3,6 +3,7 @@ package utils
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -158,21 +159,24 @@ func TestAssociateFilesWithCommands(t *testing.T) {
|
|||||||
|
|
||||||
// The associations expected depends on the implementation
|
// The associations expected depends on the implementation
|
||||||
// Let's check the actual associations and verify they make sense
|
// Let's check the actual associations and verify they make sense
|
||||||
for file, cmds := range associations {
|
for file, assoc := range associations {
|
||||||
t.Logf("File %s is associated with %d commands", file, len(cmds))
|
t.Logf("File %s is associated with %d commands and %d isolate commands", file, len(assoc.Commands), len(assoc.IsolateCommands))
|
||||||
for i, cmd := range cmds {
|
for i, cmd := range assoc.Commands {
|
||||||
t.Logf(" Command %d: Pattern=%s, Files=%v", i, cmd.Regex, cmd.Files)
|
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
|
// Specific validation based on our file types
|
||||||
switch file {
|
switch file {
|
||||||
case "file1.xml":
|
case "file1.xml":
|
||||||
if len(cmds) < 1 {
|
if len(assoc.Commands) < 1 {
|
||||||
t.Errorf("Expected at least 1 command for file1.xml, got %d", len(cmds))
|
t.Errorf("Expected at least 1 command for file1.xml, got %d", len(assoc.Commands))
|
||||||
}
|
}
|
||||||
// Verify at least one command with *.xml pattern
|
// Verify at least one command with *.xml pattern
|
||||||
hasXmlGlob := false
|
hasXmlGlob := false
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range assoc.Commands {
|
||||||
for _, glob := range cmd.Files {
|
for _, glob := range cmd.Files {
|
||||||
if glob == "*.xml" {
|
if glob == "*.xml" {
|
||||||
hasXmlGlob = true
|
hasXmlGlob = true
|
||||||
@@ -187,12 +191,12 @@ func TestAssociateFilesWithCommands(t *testing.T) {
|
|||||||
t.Errorf("Expected command with *.xml glob for file1.xml")
|
t.Errorf("Expected command with *.xml glob for file1.xml")
|
||||||
}
|
}
|
||||||
case "file2.txt":
|
case "file2.txt":
|
||||||
if len(cmds) < 1 {
|
if len(assoc.Commands) < 1 {
|
||||||
t.Errorf("Expected at least 1 command for file2.txt, got %d", len(cmds))
|
t.Errorf("Expected at least 1 command for file2.txt, got %d", len(assoc.Commands))
|
||||||
}
|
}
|
||||||
// Verify at least one command with *.txt pattern
|
// Verify at least one command with *.txt pattern
|
||||||
hasTxtGlob := false
|
hasTxtGlob := false
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range assoc.Commands {
|
||||||
for _, glob := range cmd.Files {
|
for _, glob := range cmd.Files {
|
||||||
if glob == "*.txt" {
|
if glob == "*.txt" {
|
||||||
hasTxtGlob = true
|
hasTxtGlob = true
|
||||||
@@ -207,12 +211,12 @@ func TestAssociateFilesWithCommands(t *testing.T) {
|
|||||||
t.Errorf("Expected command with *.txt glob for file2.txt")
|
t.Errorf("Expected command with *.txt glob for file2.txt")
|
||||||
}
|
}
|
||||||
case "subdir/file3.xml":
|
case "subdir/file3.xml":
|
||||||
if len(cmds) < 1 {
|
if len(assoc.Commands) < 1 {
|
||||||
t.Errorf("Expected at least 1 command for subdir/file3.xml, got %d", len(cmds))
|
t.Errorf("Expected at least 1 command for subdir/file3.xml, got %d", len(assoc.Commands))
|
||||||
}
|
}
|
||||||
// Should match both *.xml and subdir/* patterns
|
// Should match both *.xml and subdir/* patterns
|
||||||
matches := 0
|
matches := 0
|
||||||
for _, cmd := range cmds {
|
for _, cmd := range assoc.Commands {
|
||||||
for _, glob := range cmd.Files {
|
for _, glob := range cmd.Files {
|
||||||
if glob == "*.xml" || glob == "subdir/*" {
|
if glob == "*.xml" || glob == "subdir/*" {
|
||||||
matches++
|
matches++
|
||||||
@@ -393,10 +397,10 @@ func TestLoadCommandsFromCookFileSuccess(t *testing.T) {
|
|||||||
// Arrange
|
// Arrange
|
||||||
yamlData := []byte(`
|
yamlData := []byte(`
|
||||||
- name: command1
|
- name: command1
|
||||||
pattern: "*.txt"
|
regex: "*.txt"
|
||||||
lua: replace
|
lua: replace
|
||||||
- name: command2
|
- name: command2
|
||||||
pattern: "*.go"
|
regex: "*.go"
|
||||||
lua: delete
|
lua: delete
|
||||||
`)
|
`)
|
||||||
|
|
||||||
@@ -420,11 +424,11 @@ func TestLoadCommandsFromCookFileWithComments(t *testing.T) {
|
|||||||
yamlData := []byte(`
|
yamlData := []byte(`
|
||||||
# This is a comment
|
# This is a comment
|
||||||
- name: command1
|
- name: command1
|
||||||
pattern: "*.txt"
|
regex: "*.txt"
|
||||||
lua: replace
|
lua: replace
|
||||||
# Another comment
|
# Another comment
|
||||||
- name: command2
|
- name: command2
|
||||||
pattern: "*.go"
|
regex: "*.go"
|
||||||
lua: delete
|
lua: delete
|
||||||
`)
|
`)
|
||||||
|
|
||||||
@@ -445,7 +449,7 @@ func TestLoadCommandsFromCookFileWithComments(t *testing.T) {
|
|||||||
// Handle different YAML formatting styles (flow vs block)
|
// Handle different YAML formatting styles (flow vs block)
|
||||||
func TestLoadCommandsFromCookFileWithFlowStyle(t *testing.T) {
|
func TestLoadCommandsFromCookFileWithFlowStyle(t *testing.T) {
|
||||||
// Arrange
|
// 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
|
// Act
|
||||||
commands, err := LoadCommandsFromCookFile(yamlData)
|
commands, err := LoadCommandsFromCookFile(yamlData)
|
||||||
@@ -496,13 +500,13 @@ func TestLoadCommandsFromCookFileWithMultipleEntries(t *testing.T) {
|
|||||||
// Arrange
|
// Arrange
|
||||||
yamlData := []byte(`
|
yamlData := []byte(`
|
||||||
- name: command1
|
- name: command1
|
||||||
pattern: "*.txt"
|
regex: "*.txt"
|
||||||
lua: replace
|
lua: replace
|
||||||
- name: command2
|
- name: command2
|
||||||
pattern: "*.go"
|
regex: "*.go"
|
||||||
lua: delete
|
lua: delete
|
||||||
- name: command3
|
- name: command3
|
||||||
pattern: "*.md"
|
regex: "*.md"
|
||||||
lua: append
|
lua: append
|
||||||
`)
|
`)
|
||||||
|
|
||||||
@@ -1266,3 +1270,164 @@ func TestLoadCommandsFromCookFilesNoYamlFiles(t *testing.T) {
|
|||||||
// t.Errorf("Expected 0 commands, got: %d", len(commands))
|
// t.Errorf("Expected 0 commands, got: %d", len(commands))
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// Absolute path without wildcards returns correct root and pattern
|
||||||
|
func TestFigureOutGlobRootWithAbsolutePathNoWildcards(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
absPath := filepath.Join("C:\\Users", "test", "documents", "file.txt")
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
root, pattern, err := FigureOutGlobRoot(absPath)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
expectedRoot := filepath.Clean(filepath.Join("C:\\Users", "test", "documents"))
|
||||||
|
assert.Equal(t, expectedRoot, root)
|
||||||
|
assert.Equal(t, "file.txt", pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Empty input pattern handling - we expect our "Patter", such as it is, to be a folder
|
||||||
|
func TestFigureOutGlobRootWithEmptyPattern(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
emptyPattern := ""
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
root, pattern, err := FigureOutGlobRoot(emptyPattern)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
cwd, _ := os.Getwd()
|
||||||
|
cwdParts := strings.Split(filepath.Clean(cwd), "\\")
|
||||||
|
expectedRoot := strings.Join(cwdParts[:len(cwdParts)-1], "\\")
|
||||||
|
expectedPattern := cwdParts[len(cwdParts)-1]
|
||||||
|
assert.Equal(t, expectedRoot, root)
|
||||||
|
assert.Equal(t, expectedPattern, pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Relative path is correctly joined with current working directory
|
||||||
|
func TestFigureOutGlobRootWithRelativePath(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
relPath := "test/documents/file.txt"
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
root, pattern, err := FigureOutGlobRoot(relPath)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
expectedRoot := filepath.Clean(filepath.Join(cwd, "test/documents"))
|
||||||
|
assert.Equal(t, expectedRoot, root)
|
||||||
|
assert.Equal(t, "file.txt", pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path with wildcards correctly identifies the last non-wildcard directory as root
|
||||||
|
func TestFigureOutGlobRootWithWildcards(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
inputPattern := filepath.Join("C:\\Users", "test", "documents", "*", "file.txt")
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
root, pattern, err := FigureOutGlobRoot(inputPattern)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
expectedRoot := filepath.Clean(filepath.Join("C:\\Users", "test", "documents"))
|
||||||
|
assert.Equal(t, expectedRoot, root)
|
||||||
|
assert.Equal(t, "*/file.txt", pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Windows-style paths are properly handled and converted
|
||||||
|
func TestFigureOutGlobRootWithRelativePathAndWildcards(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
inputPattern := "documents\\*\\file?.txt"
|
||||||
|
cwd, _ := os.Getwd()
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
root, pattern, err := FigureOutGlobRoot(inputPattern)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
expectedRoot := filepath.Clean(filepath.Join(cwd, "documents"))
|
||||||
|
assert.Equal(t, expectedRoot, root)
|
||||||
|
assert.Equal(t, "*/file?.txt", pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path with only wildcards (e.g., "*" or "**")
|
||||||
|
func TestFigureOutGlobRootWithOnlyWildcards(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
wildcardPattern := "*"
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
root, pattern, err := FigureOutGlobRoot(wildcardPattern)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
expectedRoot, _ := os.Getwd()
|
||||||
|
assert.Equal(t, expectedRoot, root)
|
||||||
|
assert.Equal(t, "*", pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Multiple path segments are correctly processed and joined
|
||||||
|
func TestFigureOutGlobRootWithRelativePathAndWildcards2(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
relPath := filepath.Join("test", "data", "*", "file?.txt")
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
root, pattern, err := FigureOutGlobRoot(relPath)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
expectedRoot := filepath.Clean(filepath.Join(cwd, "test", "data"))
|
||||||
|
assert.Equal(t, expectedRoot, root)
|
||||||
|
assert.Equal(t, "*/file?.txt", pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path with mixed forward and backward slashes
|
||||||
|
func TestFigureOutGlobRootWithMixedSlashes(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
mixedPath := "C:\\Users/test\\documents\\file.txt"
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
root, pattern, err := FigureOutGlobRoot(mixedPath)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
expectedRoot := filepath.Clean(filepath.Join("C:\\Users", "test", "documents"))
|
||||||
|
assert.Equal(t, expectedRoot, root)
|
||||||
|
assert.Equal(t, "file.txt", pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Path with wildcards in the first segment
|
||||||
|
func TestFigureOutGlobRootWithWildcardInFirstSegment(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
inputPattern := "*\\Users\\test\\documents\\file.txt"
|
||||||
|
cwd, _ := os.Getwd()
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
root, pattern, err := FigureOutGlobRoot(inputPattern)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
expectedRoot := filepath.Clean(cwd)
|
||||||
|
assert.Equal(t, expectedRoot, root)
|
||||||
|
assert.Equal(t, "*/Users/test/documents/file.txt", pattern)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handling of relative paths with ".." or "." components
|
||||||
|
func TestFigureOutGlobRootWithRelativePathAndDotComponents(t *testing.T) {
|
||||||
|
// Setup
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
relPath := filepath.Join("..", ".", "test", "documents", "file.txt")
|
||||||
|
|
||||||
|
// Execute
|
||||||
|
root, pattern, err := FigureOutGlobRoot(relPath)
|
||||||
|
|
||||||
|
// Assert
|
||||||
|
assert.NoError(t, err)
|
||||||
|
expectedRoot := filepath.Clean(filepath.Join(cwd, "..", "test", "documents"))
|
||||||
|
assert.Equal(t, expectedRoot, root)
|
||||||
|
assert.Equal(t, "file.txt", pattern)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user