Add support (and preference) to toml from yml
This commit is contained in:
244
main.go
244
main.go
@@ -13,7 +13,7 @@ import (
|
||||
"cook/processor"
|
||||
"cook/utils"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
"github.com/BurntSushi/toml"
|
||||
|
||||
logger "git.site.quack-lab.dev/dave/cylogger"
|
||||
)
|
||||
@@ -402,132 +402,150 @@ func HandleSpecialArgs(args []string, db utils.DB) (bool, error) {
|
||||
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)
|
||||
{
|
||||
Modifiers: map[string]interface{}{
|
||||
"foobar": 4,
|
||||
"multiply": 1.5,
|
||||
"prefix": "NEW_",
|
||||
"enabled": true,
|
||||
|
||||
// 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,
|
||||
},
|
||||
},
|
||||
// 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,
|
||||
// LogLevel defaults to INFO
|
||||
},
|
||||
// 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"},
|
||||
// INFO log level
|
||||
},
|
||||
// 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"},
|
||||
},
|
||||
// Multiline regex example (DOTALL is auto-enabled). Captures numeric in nested XML.
|
||||
{
|
||||
Name: "XMLNestedValueMultiply",
|
||||
Regex: `<item>\s*\s*<name>!any<\/name>\s*\s*<value>(!num)<\/value>\s*\s*<\/item>`,
|
||||
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{
|
||||
`<entry>\s*\n\s*<id>(?P<id>!num)</id>\s*\n\s*<score>(?P<score>!num)</score>\s*\n\s*</entry>`,
|
||||
`\[block\]\nkey=(?P<key>[A-Za-z_]+)\nvalue=(?P<val>!num)`,
|
||||
// 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: `
|
||||
\[kinetics\.stressValues\.v2\.capacity\]
|
||||
|
||||
steam_engine = !num
|
||||
|
||||
water_wheel = !num
|
||||
|
||||
copper_valve_handle = !num
|
||||
|
||||
hand_crank = !num
|
||||
|
||||
creative_motor = !num`,
|
||||
Lua: "v1 * multiply",
|
||||
Files: []string{"*.txt"},
|
||||
Isolate: true,
|
||||
},
|
||||
// Network configuration with complex multiline regex
|
||||
{
|
||||
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: []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: `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: "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 := yaml.Marshal(commands)
|
||||
data, err := toml.Marshal(tomlData)
|
||||
if err != nil {
|
||||
createExampleConfigLogger.Error("Failed to marshal example config: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
createExampleConfigLogger.Debug("Writing example_cook.yml")
|
||||
err = os.WriteFile("example_cook.yml", data, 0644)
|
||||
createExampleConfigLogger.Debug("Writing example_cook.toml")
|
||||
err = os.WriteFile("example_cook.toml", data, 0644)
|
||||
if err != nil {
|
||||
createExampleConfigLogger.Error("Failed to write example_cook.yml: %v", err)
|
||||
createExampleConfigLogger.Error("Failed to write example_cook.toml: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
createExampleConfigLogger.Info("Wrote example_cook.yml")
|
||||
createExampleConfigLogger.Info("Wrote example_cook.toml")
|
||||
}
|
||||
|
||||
var NothingToDo = errors.New("nothing to do")
|
||||
|
||||
Reference in New Issue
Block a user