From 2a2e11d8e0791774b5694287ea4657ca623675d8 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Fri, 8 Aug 2025 09:51:32 +0200 Subject: [PATCH] Add more examples to the configuration file generator --- main.go | 101 +++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 86 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 15a3ee0..875c007 100644 --- a/main.go +++ b/main.go @@ -391,29 +391,100 @@ func CreateExampleConfig() { createExampleConfigLogger := logger.Default.WithPrefix("CreateExampleConfig") createExampleConfigLogger.Debug("Creating example configuration file") commands := []utils.ModifyCommand{ + // Global modifiers only entry (no name/regex/lua/files) { - Name: "DoubleNumericValues", - Regex: "(\\d+)<\\/value>", - Lua: "v1 * 2", - Files: []string{"data/*.xml"}, - LogLevel: "INFO", + Modifiers: map[string]interface{}{ + "foobar": 4, + "multiply": 1.5, + "prefix": "NEW_", + "enabled": true, + }, }, + // Multi-regex example using $variable in Lua { - Name: "UpdatePrices", - Regex: "price=\"(\\d+)\"", - Lua: "if num(v1) < 100 then return v1 * 1.5 else return v1 end", - Files: []string{"items/*.xml", "shop/*.xml"}, + Name: "RFToolsMultiply", + Regexes: []string{"generatePerTick = !num", "ticksPer\\w+ = !num", "generatorRFPerTick = !num"}, + Lua: "* $foobar", + Files: []string{"polymc/instances/**/rftools*.toml", `polymc\\instances\\**\\rftools*.toml`}, + Reset: true, + // LogLevel defaults to INFO + }, + // Named capture groups with arithmetic and string ops + { + Name: "UpdateAmountsAndItems", + Regex: `(?P!num)\s+units\s+of\s+(?P[A-Za-z_\-]+)`, + Lua: `amount = amount * $multiply; item = upper(item); return true`, + Files: []string{"data/**/*.txt"}, + // INFO log level + }, + // Full replacement via Lua 'replacement' variable + { + Name: "BumpMinorVersion", + Regex: `version\s*=\s*"(?P!num)\.(?P!num)\.(?P!num)"`, + Lua: `replacement = format("version=\"%s.%s.%s\"", major, num(minor)+1, 0); return true`, + Files: []string{"config/*.ini", "config/*.cfg"}, + }, + // Multiline regex example (DOTALL is auto-enabled). Captures numeric in nested XML. + { + Name: "XMLNestedValueMultiply", + Regex: ` +\s* +\s*!any\s* +\s*(!num)\s* +`, + Lua: `* $multiply`, + Files: []string{"data/**/*.xml"}, + // Demonstrates multiline regex in YAML + }, + // Multiline regexES array, with different patterns handled by same Lua + { + Name: "MultiLinePatterns", + Regexes: []string{ + `\s*\n\s*(?P!num)\s*\n\s*(?P!num)\s*\n\s*`, + `\[block\]\nkey=(?P[A-Za-z_]+)\nvalue=(?P!num)`, + }, + Lua: `if is_number(score) then score = score * 2 end; if is_number(val) then val = val * 3 end; return true`, + Files: []string{"examples/**/*.*"}, LogLevel: "DEBUG", }, + // Use equals operator shorthand and boolean variable { - Name: "IsolatedTagUpdate", - Regex: "(.*?)<\\/tag>", - Lua: "string.upper(s1)", - Files: []string{"config.xml"}, - Isolate: true, - NoDedup: true, + Name: "EnableFlags", + Regex: `enabled\s*=\s*(true|false)`, + Lua: `= $enabled`, + Files: []string{"**/*.toml"}, + }, + // Demonstrate NoDedup to allow overlapping replacements + { + Name: "OverlappingGroups", + Regex: `(?P!num)(?P!num)`, + Lua: `a = num(a) + 1; b = num(b) + 1; return true`, + Files: []string{"overlap/**/*.txt"}, + NoDedup: true, + }, + // Isolate command example operating on entire matched block + { + Name: "IsolateUppercaseBlock", + Regex: `BEGIN\n(?P!any)\nEND`, + Lua: `block = upper(block); return true`, + Files: []string{"logs/**/*.log"}, + Isolate: true, LogLevel: "TRACE", }, + // Using !rep placeholder and arrays of files + { + Name: "RepeatPlaceholderExample", + Regex: `name: (.*) !rep(, .* , 2)`, + Lua: `-- no-op, just demonstrate placeholder; return false`, + Files: []string{"lists/**/*.yml", "lists/**/*.yaml"}, + }, + // Using string variable in Lua expression + { + Name: "PrefixKeys", + Regex: `(?P[A-Za-z0-9_]+)\s*=`, + Lua: `key = $prefix .. key; return true`, + Files: []string{"**/*.properties"}, + }, } data, err := yaml.Marshal(commands)