From 8a86ae2f40399584ddcee9b0a84297fa7cc42bf3 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 28 Mar 2025 11:20:35 +0100 Subject: [PATCH] Add filter flag --- main.go | 7 ++++++- utils/flags.go | 1 + utils/modifycommand.go | 14 ++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/main.go b/main.go index 36ee5ab..8536b91 100644 --- a/main.go +++ b/main.go @@ -53,7 +53,6 @@ func main() { 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") } - // TODO: Implement -f flag for filtering recipes by name // TODO: Fix bed shitting when doing *.yml in barotrauma directory // TODO: Fix disaster: // fatal error: concurrent map writes @@ -111,6 +110,12 @@ func main() { 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 globs := utils.AggregateGlobs(commands) logger.Debug("Aggregated %d globs before deduplication", utils.CountGlobsBeforeDedup(commands)) diff --git a/utils/flags.go b/utils/flags.go index 7c38f97..be629f3 100644 --- a/utils/flags.go +++ b/utils/flags.go @@ -12,4 +12,5 @@ var ( 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/modifycommand.go b/utils/modifycommand.go index f95b884..f15056f 100644 --- a/utils/modifycommand.go +++ b/utils/modifycommand.go @@ -4,6 +4,7 @@ import ( "fmt" "modify/logger" "os" + "strings" "github.com/bmatcuk/doublestar/v4" "gopkg.in/yaml.v3" @@ -206,3 +207,16 @@ func CountGlobsBeforeDedup(commands []ModifyCommand) int { } 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 +} \ No newline at end of file