Skip to content

Commit

Permalink
add readme, run tests on prs
Browse files Browse the repository at this point in the history
  • Loading branch information
elkrammer committed Sep 27, 2024
1 parent d0f70fc commit ba28536
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 87 deletions.
73 changes: 73 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Create Release on Push to Main

on:
push:
branches:
- main

concurrency:
group: release
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Wait
run: sleep 600

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'

- name: Build binaries
run: |
# Build for Linux
GOOS=linux GOARCH=amd64 go build -o ./irule-validator
# Build for macOS (Intel)
GOOS=darwin GOARCH=amd64 go build -o ./irule-validator-macos-amd64
# Build for macOS (Apple Silicon)
GOOS=darwin GOARCH=arm64 go build -o ./irule-validator-macos-arm64
# Build for Windows
GOOS=windows GOARCH=amd64 go build -o ./irule-validator.exe
- name: Get latest tag
id: get_tag
run: |
# Check if any tags exist
if [ -z "$(git tag)" ]; then
echo "No tags found, creating initial tag v0.0.1"
new_tag="v0.0.1"
else
# Get the latest tag
latest_tag=$(git describe --tags `git rev-list --tags --max-count=1`)
echo "Latest tag: $latest_tag"
# Increment the version (patch level)
new_tag=$(echo $latest_tag | awk -F. '{$NF = $NF + 1;} 1' | sed 's/ /./g')
fi
echo "new_tag=$new_tag" >> $GITHUB_ENV
- name: Create new tag
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "github-actions[bot]"
git tag ${{ env.new_tag }}
git push origin ${{ env.new_tag }}
# Create GitHub release
- name: Create GitHub Release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ env.new_tag }}
files: |
./irule-validator
./irule-validator-macos-amd64
./irule-validator-macos-arm64
./irule-validator.exe
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23 changes: 23 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Run Tests

on:
pull_request:
branches:
- main

jobs:
test:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.23'

- name: Run tests
run: |
./run_tests.sh
19 changes: 19 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2021 elkrammer

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# 📏 iRule-Validator

Ever tried writing an F5 iRule and thought, "will this work?" only to have F5
respond with, "Nah, invalid expression on line 42?" 😩

Wouldn't it be nice to catch those errors in your iRules **before** they break production?
Well, you're welcome! 🎁

Inspired by the awesome book [Writing an Interpreter in Go](https://interpreterbook.com),
this project aims to parse F5 iRules with style and grace! 🦸 (Well, at least
most of the time.)

## 🚀 Usage

```bash
Usage of ./irule-validator:
-d, --debug Debugging Mode
-h, --help Show help message
-p, --print-errors Print Errors

If no parameter is specified it will run in quiet mode returning only
the result.
If a file name is specified, it will parse the provided file.
If no file name is specified, it will go into REPL mode.

Examples:
./irule-validator http.irule # Parse http.irule and show only the result
./irule-validator -p http.irule # Parse http.irule and print errors
./irule-validator # Start REPL
```

Pro Tip: When using this in a CI/CD pipeline, be sure to call it with `-p` to
get those sweet error printouts you so desperately crave.

## 🦄 Disclaimer

Does it validate every possible command with perfect accuracy? Not quite.
Full syntax validation is *hard*, and I've realized that F5 iRules are
a bottomless pit of edge cases. 🕳️

Building a complete F5 iRule parser is like trying to solve a puzzle where
the pieces keep changing shape. But hey, I already have a parser that
covers most of the use cases I need and that's good enough for me! 🎉

## 🧑 Contributing

PRs are welcome! 💥
65 changes: 0 additions & 65 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,33 +293,6 @@ func (is *IfStatement) String() string {
return out.String()
}

// FUNCTION LITERALS
// type FunctionLiteral struct {
// Token token.Token // the 'proc' token
// Name *Identifier
// Parameters []*Identifier
// Body *BlockStatement
// }
//
// func (fl *FunctionLiteral) expressionNode() {}
// func (fl *FunctionLiteral) TokenLiteral() string { return fl.Token.Literal }
// func (fl *FunctionLiteral) String() string {
// var out bytes.Buffer
//
// params := []string{}
// for _, p := range fl.Parameters {
// params = append(params, p.String())
// }
//
// out.WriteString(fl.TokenLiteral())
// out.WriteString("(")
// out.WriteString(strings.Join(params, ", "))
// out.WriteString(")")
// out.WriteString(fl.Body.String())
//
// return out.String()
// }

// HASH LITERALS
type HashLiteral struct {
Token token.Token // the '{' token
Expand Down Expand Up @@ -408,16 +381,6 @@ func (ce *CallExpression) String() string {
return out.String()
}

// // 'expr'
// type ExprExpression struct {
// Token token.Token // The 'expr' token
// Expression Expression // The expression after 'expr'
// }
//
// func (ee *ExprExpression) expressionNode() {}
// func (ee *ExprExpression) TokenLiteral() string { return ee.Token.Literal }
// func (ee *ExprExpression) String() string { return "expr " + ee.Expression.String() }

type ParenthesizedExpression struct {
Expression Expression
}
Expand Down Expand Up @@ -451,34 +414,6 @@ func (al *ArrayLiteral) String() string {
return out.String()
}

// type ArrayOperation struct {
// Token token.Token
// Command string // e.g., "set", "get", "exists", etc.
// Name Expression // Array name
// Index Expression // Array index (optional, can be nil)
// Value Expression // For set operations (optional, can be nil)
// }
//
// func (ao *ArrayOperation) expressionNode() {}
// func (ao *ArrayOperation) TokenLiteral() string { return ao.Token.Literal }
// func (ao *ArrayOperation) String() string {
// var out bytes.Buffer
// out.WriteString("array ")
// out.WriteString(ao.Command)
// out.WriteString(" ")
// out.WriteString(ao.Name.String())
// if ao.Index != nil {
// out.WriteString("(")
// out.WriteString(ao.Index.String())
// out.WriteString(")")
// }
// if ao.Value != nil {
// out.WriteString(" ")
// out.WriteString(ao.Value.String())
// }
// return out.String()
// }

// CommandSubstitution represents a command substitution in TCL, enclosed in square brackets
type CommandSubstitution struct {
Token token.Token // the '[' token
Expand Down
34 changes: 31 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,44 @@
package config

import "github.com/spf13/pflag"
import (
"fmt"
"github.com/spf13/pflag"
"os"
)

// App Config
var DebugMode bool
var PrintErrors bool

// Setup program flags
func SetupFlags() {
// Define flags
pflag.BoolVarP(&DebugMode, "debug", "d", false, "Run in debug mode")
pflag.BoolVarP(&DebugMode, "debug", "d", false, "Debugging Mode")
pflag.BoolVarP(&PrintErrors, "print-errors", "p", false, "Print Errors")

pflag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
pflag.PrintDefaults() // prints the default flag options

fmt.Fprintf(os.Stderr, `
If no parameter is specified it will run in quiet mode returning only
the result.
If a file name is specified, it will parse the provided file.
If no file name is specified, it will go into REPL mode.
Examples:
./irule-validator http.irule # Parse http.irule and show only the result
./irule-validator -p http.irule # Parse http.irule and print errors
./irule-validator # Start REPL
`)
}

// Manually check for the help flag before parsing
help := pflag.BoolP("help", "h", false, "Show help message")

pflag.Parse()

if *help {
pflag.Usage()
os.Exit(0)
}
}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func main() {
args := pflag.Args()

if len(args) == 0 {
config.DebugMode = true
repl.Start(os.Stdin, os.Stdout)
return
}
Expand Down
19 changes: 0 additions & 19 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,26 +35,7 @@ func Start(in io.Reader, out io.Writer) {
}
}

const ROUTER = `
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡾⠃⠀⠀⠀⠀⠀⠀⠰⣶⡀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢠⡿⠁⣴⠇⠀⠀⠀⠀⠸⣦⠈⢿⡄⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣾⡇⢸⡏⢰⡇⠀⠀⢸⡆⢸⡆⢸⡇⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢹⡇⠘⣧⡈⠃⢰⡆⠘⢁⣼⠁⣸⡇⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢿⣄⠘⠃⠀⢸⡇⠀⠘⠁⣰⡟⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠃⠀⠀⢸⡇⠀⠀⠘⠋⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡇⠀⠀⠀⠀⠀⠀⠀
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⠃⠀⠀⠀⠀⠀⠀⠀
⠀⢸⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀
⠀⢸⣿⣟⠉⢻⡟⠉⢻⡟⠉⣻⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀
⠀⢸⣿⣿⣷⣿⣿⣶⣿⣿⣾⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇⠀
⠀⠈⠉⠉⢉⣉⣉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⠉⣉⣉⡉⠉⠉⠁⠀
⠀⠀⠀⠀⠉⠉⠉⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠉⠉⠀⠀⠀⠀
`

func printParserErrors(out io.Writer, errors []string) {
// io.WriteString(out, ROUTER)
io.WriteString(out, "Woops! We ran into some funky business here!\n")
io.WriteString(out, " parser errors:\n")
for _, msg := range errors {
Expand Down
7 changes: 7 additions & 0 deletions run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,10 @@ done
# Print the summary
printf "%*s\n" 60 "" | tr ' ' '-'
echo "Test Data results: $successful_tests/$total_tests"

# If there are failures, exit with code 1
if [ ${#failure_output[@]} -gt 0 ]; then
exit 1
else
exit 0
fi

0 comments on commit ba28536

Please sign in to comment.