diff --git a/example_cook.toml b/example_cook.toml new file mode 100644 index 0000000..c174e9f --- /dev/null +++ b/example_cook.toml @@ -0,0 +1,119 @@ +# Global variables (no name/regex/lua/files - only modifiers) +[[commands]] +modifiers = { foobar = 4, multiply = 1.5, prefix = 'NEW_', enabled = true } + +# Multi-regex example using variable in Lua +[[commands]] +name = 'RFToolsMultiply' +regexes = [ + 'generatePerTick = !num', + 'ticksPer\w+ = !num', + 'generatorRFPerTick = !num', +] +lua = '* foobar' +files = [ + 'polymc/instances/**/rftools*.toml', + 'polymc\instances\**\rftools*.toml', +] +reset = true + +# Named capture groups with arithmetic and string ops +[[commands]] +name = 'UpdateAmountsAndItems' +regex = '(?P!num)\s+units\s+of\s+(?P[A-Za-z_\-]+)' +lua = 'amount = amount * multiply; item = upper(item); return true' +files = ['data/**/*.txt'] + +# Full replacement via Lua 'replacement' variable +[[commands]] +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 = ['config/*.ini', 'config/*.cfg'] + +# TOML multiline regex example - single quotes make regex natural! +[[commands]] +name = 'StressValues' +regex = ''' +\[kinetics\.stressValues\.v2\.capacity\] + steam_engine = !num + water_wheel = !num + copper_valve_handle = !num + hand_crank = !num + creative_motor = !num''' +lua = 'v1 * multiply' +files = ['*.txt'] +isolate = true + +# Network configuration with complex multiline regex +[[commands]] +name = 'NetworkConfig' +regex = ''' +networking\.firewall\.allowPing = true +networking\.firewall\.allowedTCPPorts = \[ 47984 47989 47990 \] +networking\.firewall\.allowedUDPPortRanges = \[ + \{ from = \d+; to = \d+; \} + \{ from = 8000; to = 8010; \} +\]''' +lua = "replacement = string.gsub(block[1], 'true', 'false')" +files = ['*.conf'] +isolate = true + +# Simple regex with single quotes - no escaping needed! +[[commands]] +name = 'EnableFlags' +regex = 'enabled\s*=\s*(true|false)' +lua = '= enabled' +files = ['**/*.toml'] + +# Demonstrate NoDedup to allow overlapping replacements +[[commands]] +name = 'OverlappingGroups' +regex = '(?P!num)(?P!num)' +lua = 'a = num(a) + 1; b = num(b) + 1; return true' +files = ['overlap/**/*.txt'] +nodedup = true + +# Isolate command example operating on entire matched block +[[commands]] +name = 'IsolateUppercaseBlock' +regex = '''BEGIN +(?P!any) +END''' +lua = 'block = upper(block); return true' +files = ['logs/**/*.log'] +loglevel = 'TRACE' +isolate = true + +# Using !rep placeholder and arrays of files +[[commands]] +name = 'RepeatPlaceholderExample' +regex = 'name: (.*) !rep(, .* , 2)' +lua = '-- no-op, just demonstrate placeholder; return false' +files = ['lists/**/*.yml', 'lists/**/*.yaml'] + +# Using string variable in Lua expression +[[commands]] +name = 'PrefixKeys' +regex = '(?P[A-Za-z0-9_]+)\s*=' +lua = 'key = prefix .. key; return true' +files = ['**/*.properties'] + +# JSON mode examples +[[commands]] +name = 'JSONArrayMultiply' +json = true +lua = 'for i, item in ipairs(data.items) do data.items[i].value = item.value * 2 end; return true' +files = ['data/**/*.json'] + +[[commands]] +name = 'JSONObjectUpdate' +json = true +lua = 'data.version = "2.0.0"; data.enabled = true; return true' +files = ['config/**/*.json'] + +[[commands]] +name = 'JSONNestedModify' +json = true +lua = 'if data.settings and data.settings.performance then data.settings.performance.multiplier = data.settings.performance.multiplier * 1.5 end; return true' +files = ['settings/**/*.json'] diff --git a/main.go b/main.go index 90ad38c..b55fd6a 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + _ "embed" "errors" "flag" "fmt" @@ -16,6 +17,9 @@ import ( logger "git.site.quack-lab.dev/dave/cylogger" ) +//go:embed example_cook.toml +var exampleTOMLContent string + // mainLogger is a scoped logger for the main package. var mainLogger = logger.Default.WithPrefix("main") @@ -427,130 +431,9 @@ func CreateExampleConfig() { createExampleConfigLogger := logger.Default.WithPrefix("CreateExampleConfig") createExampleConfigLogger.Debug("Creating example configuration file") - // Manually construct TOML content with single quotes for better UX - tomlContent := `# Global variables (no name/regex/lua/files - only modifiers) -[[commands]] -modifiers = { foobar = 4, multiply = 1.5, prefix = 'NEW_', enabled = true } - -# Multi-regex example using variable in Lua -[[commands]] -name = 'RFToolsMultiply' -regexes = ['generatePerTick = !num', 'ticksPer\w+ = !num', 'generatorRFPerTick = !num'] -lua = '* foobar' -files = ['polymc/instances/**/rftools*.toml', 'polymc\instances\**\rftools*.toml'] -reset = true - -# Named capture groups with arithmetic and string ops -[[commands]] -name = 'UpdateAmountsAndItems' -regex = '(?P!num)\s+units\s+of\s+(?P[A-Za-z_\-]+)' -lua = 'amount = amount * multiply; item = upper(item); return true' -files = ['data/**/*.txt'] - -# Full replacement via Lua 'replacement' variable -[[commands]] -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 = ['config/*.ini', 'config/*.cfg'] - -# TOML multiline regex example - single quotes make regex natural! -[[commands]] -name = 'StressValues' -regex = ''' -\[kinetics\.stressValues\.v2\.capacity\] - - steam_engine = !num - - water_wheel = !num - - copper_valve_handle = !num - - hand_crank = !num - - creative_motor = !num''' -lua = 'v1 * multiply' -files = ['*.txt'] -isolate = true - -# Network configuration with complex multiline regex -[[commands]] -name = 'NetworkConfig' -regex = ''' -networking\.firewall\.allowPing = true - -networking\.firewall\.allowedTCPPorts = \[ 47984 47989 47990 \] - -networking\.firewall\.allowedUDPPortRanges = \[ - \{ from = \d+; to = \d+; \} - \{ from = 8000; to = 8010; \} -\]''' -lua = 'replacement = string.gsub(block[1], ''true'', ''false'')' -files = ['*.conf'] -isolate = true - -# Simple regex with single quotes - no escaping needed! -[[commands]] -name = 'EnableFlags' -regex = 'enabled\s*=\s*(true|false)' -lua = '= enabled' -files = ['**/*.toml'] - -# Demonstrate NoDedup to allow overlapping replacements -[[commands]] -name = 'OverlappingGroups' -regex = '(?P!num)(?P!num)' -lua = 'a = num(a) + 1; b = num(b) + 1; return true' -files = ['overlap/**/*.txt'] -nodedup = true - -# Isolate command example operating on entire matched block -[[commands]] -name = 'IsolateUppercaseBlock' -regex = '''BEGIN -(?P!any) -END''' -lua = 'block = upper(block); return true' -files = ['logs/**/*.log'] -loglevel = 'TRACE' -isolate = true - -# Using !rep placeholder and arrays of files -[[commands]] -name = 'RepeatPlaceholderExample' -regex = 'name: (.*) !rep(, .* , 2)' -lua = '-- no-op, just demonstrate placeholder; return false' -files = ['lists/**/*.yml', 'lists/**/*.yaml'] - -# Using string variable in Lua expression -[[commands]] -name = 'PrefixKeys' -regex = '(?P[A-Za-z0-9_]+)\s*=' -lua = 'key = prefix .. key; return true' -files = ['**/*.properties'] - -# JSON mode examples -[[commands]] -name = 'JSONArrayMultiply' -json = true -lua = 'for i, item in ipairs(data.items) do data.items[i].value = item.value * 2 end; return true' -files = ['data/**/*.json'] - -[[commands]] -name = 'JSONObjectUpdate' -json = true -lua = 'data.version = "2.0.0"; data.enabled = true; return true' -files = ['config/**/*.json'] - -[[commands]] -name = 'JSONNestedModify' -json = true -lua = 'if data.settings and data.settings.performance then data.settings.performance.multiplier = data.settings.performance.multiplier * 1.5 end; return true' -files = ['settings/**/*.json'] -` - + // Save the embedded TOML content to disk createExampleConfigLogger.Debug("Writing example_cook.toml") - err := os.WriteFile("example_cook.toml", []byte(tomlContent), 0644) + err := os.WriteFile("example_cook.toml", []byte(exampleTOMLContent), 0644) if err != nil { createExampleConfigLogger.Error("Failed to write example_cook.toml: %v", err) return