Manually generate the example toml to "enforce" single quotes (looks nicer!)

This commit is contained in:
2025-10-26 15:14:02 +01:00
parent 3f6a03aee8
commit d905ad027a

223
main.go
View File

@@ -13,8 +13,6 @@ import (
"cook/processor"
"cook/utils"
"github.com/BurntSushi/toml"
logger "git.site.quack-lab.dev/dave/cylogger"
)
@@ -429,44 +427,37 @@ func CreateExampleConfig() {
createExampleConfigLogger := logger.Default.WithPrefix("CreateExampleConfig")
createExampleConfigLogger.Debug("Creating example configuration file")
// TOML structure with commands array
tomlData := struct {
Modifiers map[string]interface{} `toml:"modifiers"`
Commands []utils.ModifyCommand `toml:"commands"`
}{
Modifiers: map[string]interface{}{
"foobar": 4,
"multiply": 1.5,
"prefix": "NEW_",
"enabled": true,
},
Commands: []utils.ModifyCommand{
// Multi-regex example using variable in Lua
{
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,
},
// Named capture groups with arithmetic and string ops
{
Name: "UpdateAmountsAndItems",
Regex: `(?P<amount>!num)\s+units\s+of\s+(?P<item>[A-Za-z_\-]+)`,
Lua: `amount = amount * multiply; item = upper(item); return true`,
Files: []string{"data/**/*.txt"},
},
// Full replacement via Lua 'replacement' variable
{
Name: "BumpMinorVersion",
Regex: `version\s*=\s*"(?P<major>!num)\.(?P<minor>!num)\.(?P<patch>!num)"`,
Lua: `replacement = format("version=\"%s.%s.%s\"", major, num(minor)+1, 0); return true`,
Files: []string{"config/*.ini", "config/*.cfg"},
},
// TOML multiline regex example - this is the key feature!
{
Name: "StressValues",
Regex: `
// 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<amount>!num)\s+units\s+of\s+(?P<item>[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<major>!num)\.(?P<minor>!num)\.(?P<patch>!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
@@ -477,15 +468,15 @@ func CreateExampleConfig() {
hand_crank = !num
creative_motor = !num`,
Lua: "v1 * multiply",
Files: []string{"*.txt"},
Isolate: true,
},
// Network configuration with complex multiline regex
{
Name: "NetworkConfig",
Regex: `
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 \]
@@ -493,79 +484,73 @@ 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: []string{"*.conf"},
Isolate: true,
},
// Use equals operator shorthand and boolean variable
{
Name: "EnableFlags",
Regex: `enabled\s*=\s*(true|false)`,
Lua: `= enabled`,
Files: []string{"**/*.toml"},
},
// Demonstrate NoDedup to allow overlapping replacements
{
Name: "OverlappingGroups",
Regex: `(?P<a>!num)(?P<b>!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<block>!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<key>[A-Za-z0-9_]+)\s*=`,
Lua: `key = prefix .. key; return true`,
Files: []string{"**/*.properties"},
},
// JSON mode examples
{
Name: "JSONArrayMultiply",
JSON: true,
Lua: `for i, item in ipairs(data.items) do data.items[i].value = item.value * 2 end; return true`,
Files: []string{"data/**/*.json"},
},
{
Name: "JSONObjectUpdate",
JSON: true,
Lua: `data.version = "2.0.0"; data.enabled = true; return true`,
Files: []string{"config/**/*.json"},
},
{
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: []string{"settings/**/*.json"},
},
},
}
\]'''
lua = 'replacement = string.gsub(block[1], ''true'', ''false'')'
files = ['*.conf']
isolate = true
data, err := toml.Marshal(tomlData)
if err != nil {
createExampleConfigLogger.Error("Failed to marshal example config: %v", err)
return
}
# 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<a>!num)(?P<b>!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<block>!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<key>[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']
`
createExampleConfigLogger.Debug("Writing example_cook.toml")
err = os.WriteFile("example_cook.toml", data, 0644)
err := os.WriteFile("example_cook.toml", []byte(tomlContent), 0644)
if err != nil {
createExampleConfigLogger.Error("Failed to write example_cook.toml: %v", err)
return