Make readme and rework the flags a little

This commit is contained in:
2025-03-25 22:55:01 +01:00
parent d5c08d86f5
commit 34477b2c34
3 changed files with 132 additions and 24 deletions

116
README.md Normal file
View File

@@ -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
<!-- Input -->
<price>44.95</price>
<!-- Command -->
chef -xml "//price" "v=v*2" input.xml
<!-- Output -->
<price>89.9</price>
```
### 2. Supports glob patterns
```xml
chef -xml "//price" "v=v*2" data/**.xml
```
### 3. Attribute Update
```xml
<!-- Input -->
<item price="10.50"/>
<!-- Command -->
chef -xml "//item/@price" "v=v*2" input.xml
<!-- Output -->
<item price="21"/>
```
### 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
<!-- Input -->
<description>Price: $15.00 Special Offer</description>
<!-- Command -->
chef "Price: $!num Special Offer" "v1 = v1 * 0.92" input.xml
<!-- Output -->
<description>Price: $13.80 Special Offer</description>
```
### 5. Conditional Transformation
```xml
<!-- Input -->
<item stock="5" price="10.00"/>
<!-- Command -->
chef -xml "//item" "if tonumber(v.stock) > 0 then v.price = v.price * 0.8 end" input.xml
<!-- Output -->
<item stock="5" price="8.00"/>
```
## 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
```

32
main.go
View File

@@ -19,19 +19,12 @@ type GlobalStats struct {
FailedFiles int FailedFiles int
} }
type FileMode string
const (
ModeRegex FileMode = "regex"
ModeXML FileMode = "xml"
ModeJSON FileMode = "json"
)
var stats GlobalStats var stats GlobalStats
var logger *log.Logger var logger *log.Logger
var ( 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() { func init() {
@@ -67,7 +60,7 @@ func main() {
args := flag.Args() args := flag.Args()
if len(args) < 3 { 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() flag.Usage()
return return
} }
@@ -101,20 +94,19 @@ func main() {
// Create the processor based on mode // Create the processor based on mode
var proc processor.Processor var proc processor.Processor
switch *fileModeFlag { switch {
case "regex": case *xmlFlag:
proc = &processor.RegexProcessor{} proc = &processor.XMLProcessor{}
logger.Printf("Starting regex modifier with pattern %q, expression %q on %d files", logger.Printf("Starting XML modifier with XPath %q, expression %q on %d files",
pattern, luaExpr, len(files)) pattern, luaExpr, len(files))
// case "xml": case *jsonFlag:
// proc = &processor.XMLProcessor{}
// pattern = *xpathFlag
// logger.Printf("Starting XML modifier with XPath %q, expression %q on %d files",
// pattern, luaExpr, len(files))
case "json":
proc = &processor.JSONProcessor{} proc = &processor.JSONProcessor{}
logger.Printf("Starting JSON modifier with JSONPath %q, expression %q on %d files", logger.Printf("Starting JSON modifier with JSONPath %q, expression %q on %d files",
pattern, luaExpr, len(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 var wg sync.WaitGroup

View File

@@ -16,7 +16,7 @@ fi
echo "Tag: $TAG" echo "Tag: $TAG"
echo "Building the thing..." echo "Building the thing..."
go build -o BigChef.exe . go build -o chef.exe .
echo "Creating a release..." echo "Creating a release..."
TOKEN="$GITEA_API_KEY" TOKEN="$GITEA_API_KEY"
@@ -43,6 +43,6 @@ echo "Release ID: $RELEASE_ID"
echo "Uploading the things..." echo "Uploading the things..."
curl -X POST \ curl -X POST \
-H "Authorization: token $TOKEN" \ -H "Authorization: token $TOKEN" \
-F "attachment=@BigChef.exe" \ -F "attachment=@chef.exe" \
"$GITEA/api/v1/repos/$REPO/releases/${RELEASE_ID}/assets?name=BigChef.exe" "$GITEA/api/v1/repos/$REPO/releases/${RELEASE_ID}/assets?name=chef.exe"
rm BigChef.exe rm chef.exe