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/processor"
"cook/utils" "cook/utils"
"github.com/BurntSushi/toml"
logger "git.site.quack-lab.dev/dave/cylogger" logger "git.site.quack-lab.dev/dave/cylogger"
) )
@@ -429,44 +427,37 @@ func CreateExampleConfig() {
createExampleConfigLogger := logger.Default.WithPrefix("CreateExampleConfig") createExampleConfigLogger := logger.Default.WithPrefix("CreateExampleConfig")
createExampleConfigLogger.Debug("Creating example configuration file") createExampleConfigLogger.Debug("Creating example configuration file")
// TOML structure with commands array // Manually construct TOML content with single quotes for better UX
tomlData := struct { tomlContent := `# Global variables (no name/regex/lua/files - only modifiers)
Modifiers map[string]interface{} `toml:"modifiers"` [[commands]]
Commands []utils.ModifyCommand `toml:"commands"` modifiers = { foobar = 4, multiply = 1.5, prefix = 'NEW_', enabled = true }
}{
Modifiers: map[string]interface{}{ # Multi-regex example using variable in Lua
"foobar": 4, [[commands]]
"multiply": 1.5, name = 'RFToolsMultiply'
"prefix": "NEW_", regexes = ['generatePerTick = !num', 'ticksPer\w+ = !num', 'generatorRFPerTick = !num']
"enabled": true, lua = '* foobar'
}, files = ['polymc/instances/**/rftools*.toml', 'polymc\instances\**\rftools*.toml']
Commands: []utils.ModifyCommand{ reset = true
// Multi-regex example using variable in Lua
{ # Named capture groups with arithmetic and string ops
Name: "RFToolsMultiply", [[commands]]
Regexes: []string{"generatePerTick = !num", "ticksPer\\w+ = !num", "generatorRFPerTick = !num"}, name = 'UpdateAmountsAndItems'
Lua: "* foobar", regex = '(?P<amount>!num)\s+units\s+of\s+(?P<item>[A-Za-z_\-]+)'
Files: []string{"polymc/instances/**/rftools*.toml", `polymc\instances\**\rftools*.toml`}, lua = 'amount = amount * multiply; item = upper(item); return true'
Reset: true, files = ['data/**/*.txt']
},
// Named capture groups with arithmetic and string ops # Full replacement via Lua 'replacement' variable
{ [[commands]]
Name: "UpdateAmountsAndItems", name = 'BumpMinorVersion'
Regex: `(?P<amount>!num)\s+units\s+of\s+(?P<item>[A-Za-z_\-]+)`, regex = 'version\s*=\s*"(?P<major>!num)\.(?P<minor>!num)\.(?P<patch>!num)"'
Lua: `amount = amount * multiply; item = upper(item); return true`, lua = 'replacement = format("version=\"%s.%s.%s\"", major, num(minor)+1, 0); return true'
Files: []string{"data/**/*.txt"}, files = ['config/*.ini', 'config/*.cfg']
},
// Full replacement via Lua 'replacement' variable # TOML multiline regex example - single quotes make regex natural!
{ [[commands]]
Name: "BumpMinorVersion", name = 'StressValues'
Regex: `version\s*=\s*"(?P<major>!num)\.(?P<minor>!num)\.(?P<patch>!num)"`, regex = '''
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: `
\[kinetics\.stressValues\.v2\.capacity\] \[kinetics\.stressValues\.v2\.capacity\]
steam_engine = !num steam_engine = !num
@@ -477,15 +468,15 @@ func CreateExampleConfig() {
hand_crank = !num hand_crank = !num
creative_motor = !num`, creative_motor = !num'''
Lua: "v1 * multiply", lua = 'v1 * multiply'
Files: []string{"*.txt"}, files = ['*.txt']
Isolate: true, isolate = true
},
// Network configuration with complex multiline regex # Network configuration with complex multiline regex
{ [[commands]]
Name: "NetworkConfig", name = 'NetworkConfig'
Regex: ` regex = '''
networking\.firewall\.allowPing = true networking\.firewall\.allowPing = true
networking\.firewall\.allowedTCPPorts = \[ 47984 47989 47990 \] networking\.firewall\.allowedTCPPorts = \[ 47984 47989 47990 \]
@@ -493,79 +484,73 @@ networking\.firewall\.allowedTCPPorts = \[ 47984 47989 47990 \]
networking\.firewall\.allowedUDPPortRanges = \[ networking\.firewall\.allowedUDPPortRanges = \[
\{ from = \d+; to = \d+; \} \{ from = \d+; to = \d+; \}
\{ from = 8000; to = 8010; \} \{ from = 8000; to = 8010; \}
\]`, \]'''
Lua: `replacement = string.gsub(block[1], 'true', 'false')`, lua = 'replacement = string.gsub(block[1], ''true'', ''false'')'
Files: []string{"*.conf"}, files = ['*.conf']
Isolate: true, 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"},
},
},
}
data, err := toml.Marshal(tomlData) # Simple regex with single quotes - no escaping needed!
if err != nil { [[commands]]
createExampleConfigLogger.Error("Failed to marshal example config: %v", err) name = 'EnableFlags'
return 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") 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 { if err != nil {
createExampleConfigLogger.Error("Failed to write example_cook.toml: %v", err) createExampleConfigLogger.Error("Failed to write example_cook.toml: %v", err)
return return