22 Commits

Author SHA1 Message Date
75bf449bed Remove logger and replace it with a library 2025-04-18 12:47:47 +02:00
58586395fb Add file util for later 2025-04-13 21:31:19 +02:00
c5a68af5e6 PROPERLY implement doublestar 2025-04-13 21:29:18 +02:00
b4c0284734 Add rimworld cook file 2025-04-09 09:47:53 +02:00
c5d1dad8de Rename project to "cook" and ditch loading fgrom args, now files exclusively 2025-04-09 09:47:53 +02:00
4ff2ee80ee And fix the god damn backslashes fuck windows 2025-04-01 11:29:57 +02:00
633eebfd2a Support ~ in globs 2025-04-01 11:29:02 +02:00
5a31703840 Implement per command logger 2025-03-29 17:29:21 +01:00
162d0c758d Fix some tests 2025-03-29 17:29:21 +01:00
14d64495b6 Add deduplicate flag 2025-03-29 17:29:21 +01:00
fe6e97e832 Don't deduplicate (yet) 2025-03-29 17:23:21 +01:00
35b3d8b099 Reduce some of the reads and writes
It's really not necessary
2025-03-28 23:39:11 +01:00
2e3e958e15 Fix some tests and add some logs 2025-03-28 23:31:44 +01:00
955afc4295 Refactor running commands to separate functions 2025-03-28 16:59:22 +01:00
2c487bc443 Implement "Isolate" commands
Commands that run alone one by one on reading and writing the file
This should be used on commands that will modify a large part of the
file (or generally large parts)
Since that can fuck up the indices of other commands when ran together
2025-03-28 16:56:39 +01:00
b77224176b Add file lua value 2025-03-28 16:47:21 +01:00
a2201053c5 Remove some random ass fmt printf 2025-03-28 13:24:12 +01:00
04cedf5ece Fix the concurrent map writes 2025-03-28 11:35:38 +01:00
ebb07854cc Memoize the match table 2025-03-28 11:31:27 +01:00
8a86ae2f40 Add filter flag 2025-03-28 11:20:44 +01:00
e8f16dda2b Housekeeping 2025-03-28 02:14:27 +01:00
513773f641 Again 2025-03-28 01:26:26 +01:00
18 changed files with 455 additions and 975 deletions

35
.vscode/launch.json vendored
View File

@@ -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"
] ]
}, },
{ {
@@ -33,6 +32,28 @@
"cookassistant.yml", "cookassistant.yml",
] ]
}, },
{
"name": "Launch Package (Quasimorph cookfile)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"cwd": "C:/Users/Administrator/Seafile/Games-Quasimorph",
"args": [
"cook.yml",
]
},
{
"name": "Launch Package (Rimworld cookfile)",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}",
"cwd": "C:/Users/Administrator/Seafile/Games-Rimworld/294100",
"args": [
"cookVehicles.yml",
]
},
{ {
"name": "Launch Package (Workspace)", "name": "Launch Package (Workspace)",
"type": "go", "type": "go",
@@ -40,11 +61,7 @@
"mode": "auto", "mode": "auto",
"program": "${workspaceFolder}", "program": "${workspaceFolder}",
"args": [ "args": [
"-loglevel", "tester.yml",
"trace",
"(?-s)LightComponent!anyrange=\"(!num)\"",
"*4",
"**/Outpost*.xml"
] ]
} }
] ]

View File

@@ -1,8 +1,9 @@
package main package main
import ( import (
"modify/logger"
"time" "time"
logger "git.site.quack-lab.dev/dave/cylogger"
) )
func main() { func main() {

5
go.mod
View File

@@ -1,8 +1,9 @@
module modify module cook
go 1.24.1 go 1.24.2
require ( require (
git.site.quack-lab.dev/dave/cylogger v1.1.1
github.com/bmatcuk/doublestar/v4 v4.8.1 github.com/bmatcuk/doublestar/v4 v4.8.1
github.com/stretchr/testify v1.10.0 github.com/stretchr/testify v1.10.0
github.com/yuin/gopher-lua v1.1.1 github.com/yuin/gopher-lua v1.1.1

2
go.sum
View File

@@ -1,5 +1,7 @@
dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk= dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk= dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
git.site.quack-lab.dev/dave/cylogger v1.1.1 h1:LQZaigVKUo07hGbS/ZTKiR+l7j4Z2eNf13zsljednNU=
git.site.quack-lab.dev/dave/cylogger v1.1.1/go.mod h1:VS9MI4Y/cwjCBZgel7dSfCQlwtAgHmfvixOoBgBhtKg=
github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY= github.com/Microsoft/go-winio v0.5.2/go.mod h1:WpS1mjBmmwHBEWmogvA2mj8546UReBk4v8QkMxJ6pZY=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY= github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU= github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=

View File

@@ -1,465 +0,0 @@
package logger
import (
"bytes"
"fmt"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"strings"
"sync"
"time"
)
// LogLevel defines the severity of log messages
type LogLevel int
const (
// LevelError is for critical errors that should always be displayed
LevelError LogLevel = iota
// LevelWarning is for important warnings
LevelWarning
// LevelInfo is for informational messages
LevelInfo
// LevelDebug is for detailed debugging information
LevelDebug
// LevelTrace is for very detailed tracing information
LevelTrace
// LevelLua is specifically for output from Lua scripts
LevelLua
)
var levelNames = map[LogLevel]string{
LevelError: "ERROR",
LevelWarning: "WARNING",
LevelInfo: "INFO",
LevelDebug: "DEBUG",
LevelTrace: "TRACE",
LevelLua: "LUA",
}
var levelColors = map[LogLevel]string{
LevelError: "\033[1;31m", // Bold Red
LevelWarning: "\033[1;33m", // Bold Yellow
LevelInfo: "\033[1;32m", // Bold Green
LevelDebug: "\033[1;36m", // Bold Cyan
LevelTrace: "\033[1;35m", // Bold Magenta
LevelLua: "\033[1;34m", // Bold Blue
}
// ResetColor is the ANSI code to reset text color
const ResetColor = "\033[0m"
// Logger is our custom logger with level support
type Logger struct {
mu sync.Mutex
out io.Writer
currentLevel LogLevel
prefix string
flag int
useColors bool
callerOffset int
defaultFields map[string]interface{}
showGoroutine bool
}
var (
// DefaultLogger is the global logger instance
DefaultLogger *Logger
// defaultLogLevel is the default log level if not specified
defaultLogLevel = LevelInfo
// Global mutex for DefaultLogger initialization
initMutex sync.Mutex
)
// ParseLevel converts a string log level to LogLevel
func ParseLevel(levelStr string) LogLevel {
switch strings.ToUpper(levelStr) {
case "ERROR":
return LevelError
case "WARNING", "WARN":
return LevelWarning
case "INFO":
return LevelInfo
case "DEBUG":
return LevelDebug
case "TRACE":
return LevelTrace
case "LUA":
return LevelLua
default:
return defaultLogLevel
}
}
// String returns the string representation of the log level
func (l LogLevel) String() string {
if name, ok := levelNames[l]; ok {
return name
}
return fmt.Sprintf("Level(%d)", l)
}
// New creates a new Logger instance
func New(out io.Writer, prefix string, flag int) *Logger {
return &Logger{
out: out,
currentLevel: defaultLogLevel,
prefix: prefix,
flag: flag,
useColors: true,
callerOffset: 0,
defaultFields: make(map[string]interface{}),
showGoroutine: true,
}
}
// Init initializes the DefaultLogger
func Init(level LogLevel) {
initMutex.Lock()
defer initMutex.Unlock()
if DefaultLogger == nil {
DefaultLogger = New(os.Stdout, "", log.Lmicroseconds|log.Lshortfile)
}
DefaultLogger.SetLevel(level)
}
// SetLevel sets the current log level
func (l *Logger) SetLevel(level LogLevel) {
l.mu.Lock()
defer l.mu.Unlock()
l.currentLevel = level
}
// GetLevel returns the current log level
func (l *Logger) GetLevel() LogLevel {
l.mu.Lock()
defer l.mu.Unlock()
return l.currentLevel
}
// SetCallerOffset sets the caller offset for correct file and line reporting
func (l *Logger) SetCallerOffset(offset int) {
l.mu.Lock()
defer l.mu.Unlock()
l.callerOffset = offset
}
// SetShowGoroutine sets whether to include goroutine ID in log messages
func (l *Logger) SetShowGoroutine(show bool) {
l.mu.Lock()
defer l.mu.Unlock()
l.showGoroutine = show
}
// ShowGoroutine returns whether goroutine ID is included in log messages
func (l *Logger) ShowGoroutine() bool {
l.mu.Lock()
defer l.mu.Unlock()
return l.showGoroutine
}
// WithField adds a field to the logger's context
func (l *Logger) WithField(key string, value interface{}) *Logger {
newLogger := &Logger{
out: l.out,
currentLevel: l.currentLevel,
prefix: l.prefix,
flag: l.flag,
useColors: l.useColors,
callerOffset: l.callerOffset,
defaultFields: make(map[string]interface{}),
showGoroutine: l.showGoroutine,
}
// Copy existing fields
for k, v := range l.defaultFields {
newLogger.defaultFields[k] = v
}
// Add new field
newLogger.defaultFields[key] = value
return newLogger
}
// WithFields adds multiple fields to the logger's context
func (l *Logger) WithFields(fields map[string]interface{}) *Logger {
newLogger := &Logger{
out: l.out,
currentLevel: l.currentLevel,
prefix: l.prefix,
flag: l.flag,
useColors: l.useColors,
callerOffset: l.callerOffset,
defaultFields: make(map[string]interface{}),
showGoroutine: l.showGoroutine,
}
// Copy existing fields
for k, v := range l.defaultFields {
newLogger.defaultFields[k] = v
}
// Add new fields
for k, v := range fields {
newLogger.defaultFields[k] = v
}
return newLogger
}
// GetGoroutineID extracts the goroutine ID from the runtime stack
func GetGoroutineID() string {
buf := make([]byte, 64)
n := runtime.Stack(buf, false)
// Format of first line is "goroutine N [state]:"
// We only need the N part
buf = buf[:n]
idField := bytes.Fields(bytes.Split(buf, []byte{':'})[0])[1]
return string(idField)
}
// formatMessage formats a log message with level, time, file, and line information
func (l *Logger) formatMessage(level LogLevel, format string, args ...interface{}) string {
var msg string
if len(args) > 0 {
msg = fmt.Sprintf(format, args...)
} else {
msg = format
}
// Format default fields if any
var fields string
if len(l.defaultFields) > 0 {
var pairs []string
for k, v := range l.defaultFields {
pairs = append(pairs, fmt.Sprintf("%s=%v", k, v))
}
fields = " " + strings.Join(pairs, " ")
}
var levelColor, resetColor string
if l.useColors {
levelColor = levelColors[level]
resetColor = ResetColor
}
var caller string
if l.flag&log.Lshortfile != 0 || l.flag&log.Llongfile != 0 {
// Find the actual caller by scanning up the stack
// until we find a function outside the logger package
var file string
var line int
var ok bool
// Start at a reasonable depth and scan up to 10 frames
for depth := 4; depth < 15; depth++ {
_, file, line, ok = runtime.Caller(depth)
if !ok {
break
}
// If the caller is not in the logger package, we found our caller
if !strings.Contains(file, "logger/logger.go") {
break
}
}
if !ok {
file = "???"
line = 0
}
if l.flag&log.Lshortfile != 0 {
file = filepath.Base(file)
}
caller = fmt.Sprintf("%-25s ", file+":"+strconv.Itoa(line))
}
// Format the timestamp with fixed width
var timeStr string
if l.flag&(log.Ldate|log.Ltime|log.Lmicroseconds) != 0 {
t := time.Now()
if l.flag&log.Ldate != 0 {
timeStr += fmt.Sprintf("%04d/%02d/%02d ", t.Year(), t.Month(), t.Day())
}
if l.flag&(log.Ltime|log.Lmicroseconds) != 0 {
timeStr += fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second())
if l.flag&log.Lmicroseconds != 0 {
timeStr += fmt.Sprintf(".%06d", t.Nanosecond()/1000)
}
}
timeStr = fmt.Sprintf("%-15s ", timeStr)
}
// Add goroutine ID if enabled, with fixed width
var goroutineStr string
if l.showGoroutine {
goroutineID := GetGoroutineID()
goroutineStr = fmt.Sprintf("[g:%-4s] ", goroutineID)
}
// Create a colored level indicator with both brackets colored
levelStr := fmt.Sprintf("%s[%s]%s", levelColor, levelNames[level], levelColor)
// Add a space after the level and before the reset color
levelColumn := fmt.Sprintf("%s %s", levelStr, resetColor)
return fmt.Sprintf("%s%s%s%s%s%s%s\n",
l.prefix, timeStr, caller, goroutineStr, levelColumn, msg, fields)
}
// log logs a message at the specified level
func (l *Logger) log(level LogLevel, format string, args ...interface{}) {
// Always show LUA level logs regardless of the current log level
if level != LevelLua && level > l.currentLevel {
return
}
l.mu.Lock()
defer l.mu.Unlock()
msg := l.formatMessage(level, format, args...)
fmt.Fprint(l.out, msg)
}
// Error logs an error message
func (l *Logger) Error(format string, args ...interface{}) {
l.log(LevelError, format, args...)
}
// Warning logs a warning message
func (l *Logger) Warning(format string, args ...interface{}) {
l.log(LevelWarning, format, args...)
}
// Info logs an informational message
func (l *Logger) Info(format string, args ...interface{}) {
l.log(LevelInfo, format, args...)
}
// Debug logs a debug message
func (l *Logger) Debug(format string, args ...interface{}) {
l.log(LevelDebug, format, args...)
}
// Trace logs a trace message
func (l *Logger) Trace(format string, args ...interface{}) {
l.log(LevelTrace, format, args...)
}
// Lua logs a Lua message
func (l *Logger) Lua(format string, args ...interface{}) {
l.log(LevelLua, format, args...)
}
// Global log functions that use DefaultLogger
// Error logs an error message using the default logger
func Error(format string, args ...interface{}) {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
DefaultLogger.Error(format, args...)
}
// Warning logs a warning message using the default logger
func Warning(format string, args ...interface{}) {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
DefaultLogger.Warning(format, args...)
}
// Info logs an informational message using the default logger
func Info(format string, args ...interface{}) {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
DefaultLogger.Info(format, args...)
}
// Debug logs a debug message using the default logger
func Debug(format string, args ...interface{}) {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
DefaultLogger.Debug(format, args...)
}
// Trace logs a trace message using the default logger
func Trace(format string, args ...interface{}) {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
DefaultLogger.Trace(format, args...)
}
// Lua logs a Lua message using the default logger
func Lua(format string, args ...interface{}) {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
DefaultLogger.Lua(format, args...)
}
// LogPanic logs a panic error and its stack trace
func LogPanic(r interface{}) {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
stack := make([]byte, 4096)
n := runtime.Stack(stack, false)
DefaultLogger.Error("PANIC: %v\n%s", r, stack[:n])
}
// SetLevel sets the log level for the default logger
func SetLevel(level LogLevel) {
if DefaultLogger == nil {
Init(level)
return
}
DefaultLogger.SetLevel(level)
}
// GetLevel gets the log level for the default logger
func GetLevel() LogLevel {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
return DefaultLogger.GetLevel()
}
// WithField returns a new logger with the field added to the default logger's context
func WithField(key string, value interface{}) *Logger {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
return DefaultLogger.WithField(key, value)
}
// WithFields returns a new logger with the fields added to the default logger's context
func WithFields(fields map[string]interface{}) *Logger {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
return DefaultLogger.WithFields(fields)
}
// SetShowGoroutine enables or disables goroutine ID display in the default logger
func SetShowGoroutine(show bool) {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
DefaultLogger.SetShowGoroutine(show)
}
// ShowGoroutine returns whether goroutine ID is included in default logger's messages
func ShowGoroutine() bool {
if DefaultLogger == nil {
Init(defaultLogLevel)
}
return DefaultLogger.ShowGoroutine()
}

View File

@@ -1,49 +0,0 @@
package logger
import (
"fmt"
"runtime/debug"
)
// PanicHandler handles a panic and logs it
func PanicHandler() {
if r := recover(); r != nil {
goroutineID := GetGoroutineID()
stackTrace := debug.Stack()
Error("PANIC in goroutine %s: %v\n%s", goroutineID, r, stackTrace)
}
}
// SafeGo launches a goroutine with panic recovery
// Usage: logger.SafeGo(func() { ... your code ... })
func SafeGo(f func()) {
go func() {
defer PanicHandler()
f()
}()
}
// SafeGoWithArgs launches a goroutine with panic recovery and passes arguments
// Usage: logger.SafeGoWithArgs(func(arg1, arg2 interface{}) { ... }, "value1", 42)
func SafeGoWithArgs(f func(...interface{}), args ...interface{}) {
go func() {
defer PanicHandler()
f(args...)
}()
}
// SafeExec executes a function with panic recovery
// Useful for code that should not panic
func SafeExec(f func()) (err error) {
defer func() {
if r := recover(); r != nil {
goroutineID := GetGoroutineID()
stackTrace := debug.Stack()
Error("PANIC in goroutine %s: %v\n%s", goroutineID, r, stackTrace)
err = fmt.Errorf("panic recovered: %v", r)
}
}()
f()
return nil
}

186
main.go
View File

@@ -4,15 +4,16 @@ import (
"flag" "flag"
"fmt" "fmt"
"os" "os"
"sort"
"sync" "sync"
"time" "time"
"modify/processor" "cook/processor"
"modify/utils" "cook/utils"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"modify/logger" logger "git.site.quack-lab.dev/dave/cylogger"
) )
type GlobalStats struct { type GlobalStats struct {
@@ -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{} if err != nil {
for _, command := range commands { logger.Error("Failed to run isolate commands for file %q: %v", file, err)
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.Regex, 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.Default
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
}

View File

@@ -2,11 +2,12 @@ package processor
import ( import (
"fmt" "fmt"
"io"
"net/http"
"strings" "strings"
logger "git.site.quack-lab.dev/dave/cylogger"
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
"modify/logger"
) )
// Maybe we make this an interface again for the shits and giggles // Maybe we make this an interface again for the shits and giggles
@@ -249,6 +250,7 @@ modified = false
logger.Debug("Setting up Lua print function to Go") logger.Debug("Setting up Lua print function to Go")
L.SetGlobal("print", L.NewFunction(printToGo)) L.SetGlobal("print", L.NewFunction(printToGo))
L.SetGlobal("fetch", L.NewFunction(fetch))
return nil return nil
} }
@@ -324,3 +326,90 @@ func printToGo(L *lua.LState) int {
logger.Lua("%s", message) logger.Lua("%s", message)
return 0 return 0
} }
func fetch(L *lua.LState) int {
// Get URL from first argument
url := L.ToString(1)
if url == "" {
L.Push(lua.LNil)
L.Push(lua.LString("URL is required"))
return 2
}
// Get options from second argument if provided
var method string = "GET"
var headers map[string]string = make(map[string]string)
var body string = ""
if L.GetTop() > 1 {
options := L.ToTable(2)
if options != nil {
// Get method
if methodVal := options.RawGetString("method"); methodVal != lua.LNil {
method = methodVal.String()
}
// Get headers
if headersVal := options.RawGetString("headers"); headersVal != lua.LNil {
if headersTable, ok := headersVal.(*lua.LTable); ok {
headersTable.ForEach(func(key lua.LValue, value lua.LValue) {
headers[key.String()] = value.String()
})
}
}
// Get body
if bodyVal := options.RawGetString("body"); bodyVal != lua.LNil {
body = bodyVal.String()
}
}
}
// Create HTTP request
req, err := http.NewRequest(method, url, strings.NewReader(body))
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(fmt.Sprintf("Error creating request: %v", err)))
return 2
}
// Set headers
for key, value := range headers {
req.Header.Set(key, value)
}
// Make request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(fmt.Sprintf("Error making request: %v", err)))
return 2
}
defer resp.Body.Close()
// Read response body
bodyBytes, err := io.ReadAll(resp.Body)
if err != nil {
L.Push(lua.LNil)
L.Push(lua.LString(fmt.Sprintf("Error reading response: %v", err)))
return 2
}
// Create response table
responseTable := L.NewTable()
responseTable.RawSetString("status", lua.LNumber(resp.StatusCode))
responseTable.RawSetString("statusText", lua.LString(resp.Status))
responseTable.RawSetString("ok", lua.LBool(resp.StatusCode >= 200 && resp.StatusCode < 300))
responseTable.RawSetString("body", lua.LString(string(bodyBytes)))
// Set headers in response
headersTable := L.NewTable()
for key, values := range resp.Header {
headersTable.RawSetString(key, lua.LString(values[0]))
}
responseTable.RawSetString("headers", headersTable)
L.Push(responseTable)
return 1
}

View File

@@ -1,16 +1,15 @@
package processor package processor
import ( import (
"cook/utils"
"fmt" "fmt"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
logger "git.site.quack-lab.dev/dave/cylogger"
lua "github.com/yuin/gopher-lua" lua "github.com/yuin/gopher-lua"
"modify/logger"
"modify/utils"
) )
type CaptureGroup struct { type CaptureGroup struct {
@@ -21,7 +20,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 +32,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 +85,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 +160,11 @@ func ProcessRegex(content string, command utils.ModifyCommand) ([]utils.ReplaceC
} }
} }
captureGroups = deduplicateGroups(captureGroups) // 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 { 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 +216,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 +261,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 +279,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))`)

View File

@@ -2,9 +2,8 @@ package processor
import ( import (
"bytes" "bytes"
"fmt" "cook/utils"
"io" "io"
"modify/utils"
"os" "os"
"regexp" "regexp"
"strings" "strings"
@@ -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

View File

@@ -2,8 +2,9 @@ package processor
import ( import (
"io" "io"
"modify/logger"
"os" "os"
logger "git.site.quack-lab.dev/dave/cylogger"
) )
func init() { func init() {
@@ -20,7 +21,7 @@ func init() {
if disableTestLogs { if disableTestLogs {
// Create a new logger that writes to nowhere // Create a new logger that writes to nowhere
silentLogger := logger.New(io.Discard, "", 0) silentLogger := logger.New(io.Discard, "", 0)
logger.DefaultLogger = silentLogger logger.Default = silentLogger
} }
} }
} }

View File

@@ -1,8 +1,8 @@
package regression package regression
import ( import (
"modify/processor" "cook/processor"
"modify/utils" "cook/utils"
"os" "os"
"path/filepath" "path/filepath"
"testing" "testing"
@@ -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
} }

24
utils/file.go Normal file
View File

@@ -0,0 +1,24 @@
package utils
import (
"os"
"path/filepath"
"strings"
)
func CleanPath(path string) string {
path = filepath.Clean(path)
path = strings.ReplaceAll(path, "\\", "/")
return path
}
func ToAbs(path string) string {
if filepath.IsAbs(path) {
return CleanPath(path)
}
cwd, err := os.Getwd()
if err != nil {
return CleanPath(path)
}
return CleanPath(filepath.Join(cwd, path))
}

View File

@@ -10,6 +10,6 @@ var (
// Deprecated // Deprecated
ResetFlag = flag.Bool("reset", false, "Reset files to their original state") ResetFlag = flag.Bool("reset", false, "Reset files to their original state")
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")
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")
) )

View File

@@ -2,13 +2,13 @@ package utils
import ( import (
"fmt" "fmt"
"modify/logger"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
"github.com/go-git/go-git/v5/plumbing/object" logger "git.site.quack-lab.dev/dave/cylogger"
"github.com/go-git/go-git/v5" "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
) )
var ( var (

View File

@@ -2,9 +2,11 @@ package utils
import ( import (
"fmt" "fmt"
"modify/logger"
"os" "os"
"path/filepath"
"strings"
logger "git.site.quack-lab.dev/dave/cylogger"
"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,88 @@ 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
}
func SplitPattern(pattern string) (string, string) {
static, pattern := doublestar.SplitPattern(pattern)
cwd, err := os.Getwd()
if err != nil {
return "", ""
}
if static == "" {
static = cwd
}
if !filepath.IsAbs(static) {
static = filepath.Join(cwd, static)
static = filepath.Clean(static)
}
static = strings.ReplaceAll(static, "\\", "/")
return static, pattern
}
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 static, pattern := SplitPattern(glob)
matches, err := doublestar.Match(glob, file) file = strings.ReplaceAll(file, "\\", "/")
file = strings.Replace(file, static+`/`, "", 1)
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 +132,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.Replace(glob, "~", os.Getenv("HOME"), 1)
glob = strings.ReplaceAll(glob, "\\", "/")
globs[glob] = struct{}{} globs[glob] = struct{}{}
} }
} }
@@ -88,9 +153,11 @@ func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
logger.Debug("Expanding patterns from directory: %s", cwd) logger.Debug("Expanding patterns from directory: %s", cwd)
for pattern := range patterns { for pattern := range patterns {
logger.Trace("Processing pattern: %s", pattern) logger.Trace("Processing pattern: %s", pattern)
matches, _ := doublestar.Glob(os.DirFS(cwd), pattern) static, pattern := SplitPattern(pattern)
matches, _ := doublestar.Glob(os.DirFS(static), pattern)
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(static, 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)
@@ -112,69 +179,35 @@ func ExpandGLobs(patterns map[string]struct{}) ([]string, error) {
func LoadCommands(args []string) ([]ModifyCommand, error) { func LoadCommands(args []string) ([]ModifyCommand, error) {
commands := []ModifyCommand{} commands := []ModifyCommand{}
logger.Info("Loading commands from cook files: %s", *Cookfile) logger.Info("Loading commands from cook files: %s", args)
newcommands, err := LoadCommandsFromCookFiles(*Cookfile) for _, arg := range args {
if err != nil { newcommands, err := LoadCommandsFromCookFiles(arg)
return nil, fmt.Errorf("failed to load commands from cook files: %w", err) if err != nil {
} return nil, fmt.Errorf("failed to load commands from cook files: %w", err)
logger.Info("Successfully loaded %d commands from cook files", len(newcommands))
commands = append(commands, newcommands...)
logger.Info("Now total commands: %d", len(commands))
logger.Info("Loading commands from arguments: %v", args)
newcommands, err = LoadCommandFromArgs(args)
if err != nil {
if len(commands) == 0 {
return nil, fmt.Errorf("failed to load commands from args: %w", err)
} }
logger.Warning("Failed to load commands from args: %v", err) logger.Info("Successfully loaded %d commands from cook iles", len(newcommands))
commands = append(commands, newcommands...)
logger.Info("Now total commands: %d", len(commands))
} }
logger.Info("Successfully loaded %d commands from args", len(newcommands))
commands = append(commands, newcommands...)
logger.Info("Now total commands: %d", len(commands))
logger.Info("Loaded %d commands from all cook f", len(commands))
return commands, nil return commands, nil
} }
func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) { func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) {
// Cannot reset without git, right? static, pattern := SplitPattern(pattern)
if *ResetFlag {
*GitFlag = true
}
if len(args) < 3 {
return nil, fmt.Errorf("at least %d arguments are required", 3)
}
command := ModifyCommand{
Regex: args[0],
Lua: args[1],
Files: args[2:],
Git: *GitFlag,
Reset: *ResetFlag,
LogLevel: *LogLevel,
}
if err := command.Validate(); err != nil {
return nil, fmt.Errorf("invalid command: %w", err)
}
return []ModifyCommand{command}, nil
}
func LoadCommandsFromCookFiles(s string) ([]ModifyCommand, error) {
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("failed to get current working directory: %w", err)
}
commands := []ModifyCommand{} commands := []ModifyCommand{}
cookFiles, err := doublestar.Glob(os.DirFS(cwd), *Cookfile) cookFiles, err := doublestar.Glob(os.DirFS(static), pattern)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to glob cook files: %w", err) return nil, fmt.Errorf("failed to glob cook files: %w", err)
} }
for _, cookFile := range cookFiles { for _, cookFile := range cookFiles {
cookFile = filepath.Join(static, cookFile)
cookFile = filepath.Clean(cookFile)
cookFile = strings.ReplaceAll(cookFile, "\\", "/")
logger.Info("Loading commands from cook file: %s", cookFile)
cookFileData, err := os.ReadFile(cookFile) cookFileData, err := os.ReadFile(cookFile)
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to read cook file: %w", err) return nil, fmt.Errorf("failed to read cook file: %w", err)
@@ -206,3 +239,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
}

View File

@@ -158,21 +158,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 +190,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 +210,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++
@@ -266,137 +269,15 @@ func TestAggregateGlobs(t *testing.T) {
} }
} }
func TestLoadCommandFromArgs(t *testing.T) {
// Save original flags
origGitFlag := *GitFlag
origResetFlag := *ResetFlag
origLogLevel := *LogLevel
// Restore original flags after test
defer func() {
*GitFlag = origGitFlag
*ResetFlag = origResetFlag
*LogLevel = origLogLevel
}()
// Test cases
tests := []struct {
name string
args []string
gitFlag bool
resetFlag bool
logLevel string
shouldError bool
}{
{
name: "Valid command",
args: []string{"pattern", "expr", "file1", "file2"},
gitFlag: false,
resetFlag: false,
logLevel: "INFO",
shouldError: false,
},
{
name: "Not enough args",
args: []string{"pattern", "expr"},
gitFlag: false,
resetFlag: false,
logLevel: "INFO",
shouldError: true,
},
{
name: "With git flag",
args: []string{"pattern", "expr", "file1"},
gitFlag: true,
resetFlag: false,
logLevel: "INFO",
shouldError: false,
},
{
name: "With reset flag (forces git flag)",
args: []string{"pattern", "expr", "file1"},
gitFlag: false,
resetFlag: true,
logLevel: "INFO",
shouldError: false,
},
{
name: "With custom log level",
args: []string{"pattern", "expr", "file1"},
gitFlag: false,
resetFlag: false,
logLevel: "DEBUG",
shouldError: false,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
// Set flags for this test case
*GitFlag = tc.gitFlag
*ResetFlag = tc.resetFlag
*LogLevel = tc.logLevel
commands, err := LoadCommandFromArgs(tc.args)
if tc.shouldError {
if err == nil {
t.Errorf("Expected an error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if len(commands) != 1 {
t.Errorf("Expected 1 command, got %d", len(commands))
return
}
cmd := commands[0]
// Check command properties
if cmd.Regex != tc.args[0] {
t.Errorf("Expected pattern %q, got %q", tc.args[0], cmd.Regex)
}
if cmd.Lua != tc.args[1] {
t.Errorf("Expected LuaExpr %q, got %q", tc.args[1], cmd.Lua)
}
if len(cmd.Files) != len(tc.args)-2 {
t.Errorf("Expected %d files, got %d", len(tc.args)-2, len(cmd.Files))
}
// When reset is true, git should be true regardless of what was set
expectedGit := tc.gitFlag || tc.resetFlag
if cmd.Git != expectedGit {
t.Errorf("Expected Git flag %v, got %v", expectedGit, cmd.Git)
}
if cmd.Reset != tc.resetFlag {
t.Errorf("Expected Reset flag %v, got %v", tc.resetFlag, cmd.Reset)
}
if cmd.LogLevel != tc.logLevel {
t.Errorf("Expected LogLevel %q, got %q", tc.logLevel, cmd.LogLevel)
}
})
}
}
// Successfully unmarshal valid YAML data into ModifyCommand slice // Successfully unmarshal valid YAML data into ModifyCommand slice
func TestLoadCommandsFromCookFileSuccess(t *testing.T) { 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 +301,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 +326,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 +377,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
`) `)
@@ -553,155 +434,6 @@ func TestLoadCommandsFromCookFileLegitExample(t *testing.T) {
assert.Equal(t, "crewlayabout", commands[0].Name) assert.Equal(t, "crewlayabout", commands[0].Name)
} }
// Valid command with minimum 3 arguments returns a ModifyCommand slice with correct values
func TestLoadCommandFromArgsWithValidArguments(t *testing.T) {
// Setup
oldGitFlag := GitFlag
oldResetFlag := ResetFlag
oldLogLevel := LogLevel
gitValue := true
resetValue := false
logLevelValue := "info"
GitFlag = &gitValue
ResetFlag = &resetValue
LogLevel = &logLevelValue
defer func() {
GitFlag = oldGitFlag
ResetFlag = oldResetFlag
LogLevel = oldLogLevel
}()
args := []string{"*.go", "return x", "file1.go", "file2.go"}
// Execute
commands, err := LoadCommandFromArgs(args)
// Assert
assert.NoError(t, err)
assert.Len(t, commands, 1)
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)
assert.Equal(t, false, commands[0].Reset)
assert.Equal(t, "info", commands[0].LogLevel)
}
// Less than 3 arguments returns an error with appropriate message
func TestLoadCommandFromArgsWithInsufficientArguments(t *testing.T) {
// Setup
oldGitFlag := GitFlag
oldResetFlag := ResetFlag
oldLogLevel := LogLevel
gitValue := false
resetValue := false
logLevelValue := "info"
GitFlag = &gitValue
ResetFlag = &resetValue
LogLevel = &logLevelValue
defer func() {
GitFlag = oldGitFlag
ResetFlag = oldResetFlag
LogLevel = oldLogLevel
}()
testCases := []struct {
name string
args []string
}{
{"empty args", []string{}},
{"one arg", []string{"*.go"}},
{"two args", []string{"*.go", "return x"}},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Execute
commands, err := LoadCommandFromArgs(tc.args)
// Assert
assert.Error(t, err)
assert.Nil(t, commands)
assert.Contains(t, err.Error(), "at least 3 arguments are required")
})
}
}
// Pattern, Lua, and Files fields are correctly populated from args
func TestLoadCommandFromArgsPopulatesFieldsCorrectly(t *testing.T) {
// Setup
oldGitFlag := GitFlag
oldResetFlag := ResetFlag
oldLogLevel := LogLevel
gitValue := false
resetValue := false
logLevelValue := "debug"
GitFlag = &gitValue
ResetFlag = &resetValue
LogLevel = &logLevelValue
defer func() {
GitFlag = oldGitFlag
ResetFlag = oldResetFlag
LogLevel = oldLogLevel
}()
args := []string{"*.txt", "print('Hello')", "file1.txt", "file2.txt"}
// Execute
commands, err := LoadCommandFromArgs(args)
// Assert
assert.NoError(t, err)
assert.Len(t, commands, 1)
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)
assert.Equal(t, false, commands[0].Reset)
assert.Equal(t, "debug", commands[0].LogLevel)
}
// Git flag is set to true when ResetFlag is true
func TestLoadCommandFromArgsSetsGitFlagWhenResetFlagIsTrue(t *testing.T) {
// Setup
oldGitFlag := GitFlag
oldResetFlag := ResetFlag
oldLogLevel := LogLevel
gitValue := false
resetValue := true
logLevelValue := "info"
GitFlag = &gitValue
ResetFlag = &resetValue
LogLevel = &logLevelValue
defer func() {
GitFlag = oldGitFlag
ResetFlag = oldResetFlag
LogLevel = oldLogLevel
}()
args := []string{"*.go", "return x", "file1.go", "file2.go"}
// Execute
commands, err := LoadCommandFromArgs(args)
// Assert
assert.NoError(t, err)
assert.Len(t, commands, 1)
assert.Equal(t, true, commands[0].Git)
}
// TODO: Figure out how to mock shit // TODO: Figure out how to mock shit
// Can't be asked doing that right now... // Can't be asked doing that right now...
// Successfully loads commands from multiple YAML files in the current directory // Successfully loads commands from multiple YAML files in the current directory

View File

@@ -2,8 +2,9 @@ package utils
import ( import (
"fmt" "fmt"
"modify/logger"
"sort" "sort"
logger "git.site.quack-lab.dev/dave/cylogger"
) )
type ReplaceCommand struct { type ReplaceCommand struct {