From 34477b2c34df056d8398577823eb06b886bf064d Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Tue, 25 Mar 2025 22:55:01 +0100 Subject: [PATCH] Make readme and rework the flags a little --- README.md | 116 +++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 32 ++++++--------- release.sh | 8 ++-- 3 files changed, 132 insertions(+), 24 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..d46e21d --- /dev/null +++ b/README.md @@ -0,0 +1,116 @@ +# Big Chef + +A Go-based tool for modifying XML, JSON, and text documents using XPath/JSONPath/Regex expressions and Lua transformations. + +## Features + +- **Multi-Format Processing**: + - XML (XPath) + - JSON (JSONPath) + - Text (Regex) +- **Node Value Modification**: Update text values in XML elements, JSON properties or text matches +- **Attribute Manipulation**: Modify XML attributes, JSON object keys or regex capture groups +- **Conditional Logic**: Apply transformations based on document content +- **Complex Operations**: + - Mathematical calculations + - String manipulations + - Date conversions + - Structural changes + - Whole ass Lua environment +- **Error Handling**: Comprehensive error detection for: + - Invalid XML/JSON + - Malformed XPath/JSONPath + - Lua syntax errors + +## Usage Examples + +### 1. Basic field modification +```xml + +44.95 + + +chef -xml "//price" "v=v*2" input.xml + + +89.9 +``` + +### 2. Supports glob patterns +```xml +chef -xml "//price" "v=v*2" data/**.xml +``` + +### 3. Attribute Update +```xml + + + + +chef -xml "//item/@price" "v=v*2" input.xml + + + +``` + +### 3. JSONPath Transformation +```json +// Input +{ + "products": [ + {"name": "Widget", "price": 19.99}, + {"name": "Gadget", "price": 29.99} + ] +} + +// Command +chef -json "$.products[*].price" "v=v*0.75" input.json + +// Output +{ + "products": [ + {"name": "Widget", "price": 14.99}, + {"name": "Gadget", "price": 22.49} + ] +} +``` + +### 4. Regex Text Replacement +Regex works slightly differently, up to 12 match groups are provided as v1..v12 and s1..s12 for numbers and strings respectively. +A special shorthand "!num" is also provided that simply expands to `(\d*\.?\d+)`. +```xml + +Price: $15.00 Special Offer + + +chef "Price: $!num Special Offer" "v1 = v1 * 0.92" input.xml + + +Price: $13.80 Special Offer +``` + +### 5. Conditional Transformation +```xml + + + + +chef -xml "//item" "if tonumber(v.stock) > 0 then v.price = v.price * 0.8 end" input.xml + + + +``` + +## Installation + +```bash +go build -o chef main.go +``` + +```bash +# Process XML file +./chef -xml "//price" "v=v*1.2" input.xml + +# Process JSON file +./chef -json "$.prices[*]" "v=v*0.9" input.json +``` diff --git a/main.go b/main.go index e44668d..d7c8ee5 100644 --- a/main.go +++ b/main.go @@ -19,19 +19,12 @@ type GlobalStats struct { FailedFiles int } -type FileMode string - -const ( - ModeRegex FileMode = "regex" - ModeXML FileMode = "xml" - ModeJSON FileMode = "json" -) - var stats GlobalStats var logger *log.Logger var ( - fileModeFlag = flag.String("mode", "regex", "Processing mode: regex, xml, json") + jsonFlag = flag.Bool("json", false, "Process JSON files") + xmlFlag = flag.Bool("xml", false, "Process XML files") ) func init() { @@ -67,7 +60,7 @@ func main() { args := flag.Args() if len(args) < 3 { - fmt.Fprintf(os.Stderr, "%s mode requires %d arguments minimum\n", *fileModeFlag, 3) + log.Printf("At least %d arguments are required", 3) flag.Usage() return } @@ -101,20 +94,19 @@ func main() { // Create the processor based on mode var proc processor.Processor - switch *fileModeFlag { - case "regex": - proc = &processor.RegexProcessor{} - logger.Printf("Starting regex modifier with pattern %q, expression %q on %d files", + switch { + case *xmlFlag: + proc = &processor.XMLProcessor{} + logger.Printf("Starting XML modifier with XPath %q, expression %q on %d files", pattern, luaExpr, len(files)) - // case "xml": - // proc = &processor.XMLProcessor{} - // pattern = *xpathFlag - // logger.Printf("Starting XML modifier with XPath %q, expression %q on %d files", - // pattern, luaExpr, len(files)) - case "json": + case *jsonFlag: proc = &processor.JSONProcessor{} logger.Printf("Starting JSON modifier with JSONPath %q, expression %q on %d files", pattern, luaExpr, len(files)) + default: + proc = &processor.RegexProcessor{} + logger.Printf("Starting regex modifier with pattern %q, expression %q on %d files", + pattern, luaExpr, len(files)) } var wg sync.WaitGroup diff --git a/release.sh b/release.sh index 418a72f..115e34b 100644 --- a/release.sh +++ b/release.sh @@ -16,7 +16,7 @@ fi echo "Tag: $TAG" echo "Building the thing..." -go build -o BigChef.exe . +go build -o chef.exe . echo "Creating a release..." TOKEN="$GITEA_API_KEY" @@ -43,6 +43,6 @@ echo "Release ID: $RELEASE_ID" echo "Uploading the things..." curl -X POST \ -H "Authorization: token $TOKEN" \ - -F "attachment=@BigChef.exe" \ - "$GITEA/api/v1/repos/$REPO/releases/${RELEASE_ID}/assets?name=BigChef.exe" -rm BigChef.exe + -F "attachment=@chef.exe" \ + "$GITEA/api/v1/repos/$REPO/releases/${RELEASE_ID}/assets?name=chef.exe" +rm chef.exe