diff --git a/go.mod b/go.mod index c1ca1eb..87ee085 100644 --- a/go.mod +++ b/go.mod @@ -1,4 +1,4 @@ -module modify +module cook go 1.24.1 diff --git a/main.go b/main.go index fc7f13e..462aa72 100644 --- a/main.go +++ b/main.go @@ -8,12 +8,12 @@ import ( "sync" "time" - "modify/processor" - "modify/utils" + "cook/processor" + "cook/utils" "github.com/go-git/go-git/v5" - "modify/logger" + "cook/logger" ) type GlobalStats struct { diff --git a/processor/processor.go b/processor/processor.go index 008d0b3..243d9a3 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -6,7 +6,7 @@ import ( lua "github.com/yuin/gopher-lua" - "modify/logger" + "cook/logger" ) // Maybe we make this an interface again for the shits and giggles diff --git a/processor/regex.go b/processor/regex.go index 293d84c..860deb1 100644 --- a/processor/regex.go +++ b/processor/regex.go @@ -9,8 +9,8 @@ import ( lua "github.com/yuin/gopher-lua" - "modify/logger" - "modify/utils" + "cook/logger" + "cook/utils" ) type CaptureGroup struct { @@ -33,6 +33,11 @@ func ProcessRegex(content string, command utils.ModifyCommand, filename string) // We don't HAVE to do this multiple times for a pattern // But it's quick enough for us to not care pattern := resolveRegexPlaceholders(command.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) patternCompileStart := time.Now() diff --git a/processor/regex_test.go b/processor/regex_test.go index 1209aa0..3e364e5 100644 --- a/processor/regex_test.go +++ b/processor/regex_test.go @@ -2,8 +2,8 @@ package processor import ( "bytes" + "cook/utils" "io" - "modify/utils" "os" "regexp" "strings" diff --git a/processor/test_helper.go b/processor/test_helper.go index ec09c11..a27be1f 100644 --- a/processor/test_helper.go +++ b/processor/test_helper.go @@ -2,7 +2,7 @@ package processor import ( "io" - "modify/logger" + "cook/logger" "os" ) diff --git a/regression/regression_test.go b/regression/regression_test.go index d03e7c2..0c9224e 100644 --- a/regression/regression_test.go +++ b/regression/regression_test.go @@ -1,8 +1,8 @@ package regression import ( - "modify/processor" - "modify/utils" + "cook/processor" + "cook/utils" "os" "path/filepath" "testing" diff --git a/utils/flags.go b/utils/flags.go index be629f3..02f03b3 100644 --- a/utils/flags.go +++ b/utils/flags.go @@ -10,7 +10,6 @@ var ( // Deprecated ResetFlag = flag.Bool("reset", false, "Reset files to their original state") 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") Filter = flag.String("filter", "", "Filter commands before running them") ) diff --git a/utils/git.go b/utils/git.go index db98cc4..4ae8ff9 100644 --- a/utils/git.go +++ b/utils/git.go @@ -1,14 +1,14 @@ package utils import ( + "cook/logger" "fmt" - "modify/logger" "os" "path/filepath" "time" - "github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5" + "github.com/go-git/go-git/v5/plumbing/object" ) var ( @@ -94,4 +94,4 @@ func CleanupGitFiles(files []string) error { } } return nil -} \ No newline at end of file +} diff --git a/utils/modifycommand.go b/utils/modifycommand.go index f7547ce..d773fd6 100644 --- a/utils/modifycommand.go +++ b/utils/modifycommand.go @@ -1,9 +1,10 @@ package utils import ( + "cook/logger" "fmt" - "modify/logger" "os" + "path/filepath" "strings" "github.com/bmatcuk/doublestar/v4" @@ -155,69 +156,38 @@ func ExpandGLobs(patterns map[string]struct{}) ([]string, error) { func LoadCommands(args []string) ([]ModifyCommand, error) { commands := []ModifyCommand{} - logger.Info("Loading commands from cook files: %s", *Cookfile) - newcommands, err := LoadCommandsFromCookFiles(*Cookfile) - 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.Info("Loading commands from cook files: %s", args) + for _, arg := range args { + newcommands, err := LoadCommandsFromCookFiles(arg) + if err != nil { + return nil, fmt.Errorf("failed to load commands from cook files: %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 } -func LoadCommandFromArgs(args []string) ([]ModifyCommand, error) { - // Cannot reset without git, right? - 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) { +func LoadCommandsFromCookFiles(pattern string) ([]ModifyCommand, error) { cwd, err := os.Getwd() if err != nil { return nil, fmt.Errorf("failed to get current working directory: %w", err) } commands := []ModifyCommand{} - cookFiles, err := doublestar.Glob(os.DirFS(cwd), *Cookfile) + cookFiles, err := doublestar.Glob(os.DirFS(cwd), pattern) if err != nil { return nil, fmt.Errorf("failed to glob cook files: %w", err) } for _, cookFile := range cookFiles { + cookFile = filepath.Clean(cookFile) + cookFile = strings.ReplaceAll(cookFile, "\\", "/") + logger.Info("Loading commands from cook file: %s", cookFile) + cookFileData, err := os.ReadFile(cookFile) if err != nil { return nil, fmt.Errorf("failed to read cook file: %w", err) diff --git a/utils/replacecommand.go b/utils/replacecommand.go index 59c8fe9..89f3a06 100644 --- a/utils/replacecommand.go +++ b/utils/replacecommand.go @@ -1,8 +1,8 @@ package utils import ( + "cook/logger" "fmt" - "modify/logger" "sort" )