Skip to content

Commit

Permalink
Add magefile for running development commands (#315)
Browse files Browse the repository at this point in the history
* Add magefile for running development commands

* Add doc command

* No package
  • Loading branch information
anuraaga authored Aug 18, 2022
1 parent 0974320 commit 671b082
Show file tree
Hide file tree
Showing 7 changed files with 140 additions and 20 deletions.
40 changes: 21 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
*.exe
*.exe~
*.dll
*.so
*.o
*.dylib
.DS_Store
.idea
# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
test/crs
/main
vendor/
coraza-waf
__debug_bin
seclang/crs_test.go
*.exe
*.exe~
*.dll
*.so
*.o
*.dylib
.DS_Store
.idea
# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
test/crs
/main
vendor/
coraza-waf
__debug_bin
seclang/crs_test.go

build/
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,8 @@ linters:
- goimports
- gofmt
- gocritic
issues:
exclude-rules:
- path: magefile\.go
linters:
- deadcode
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,25 @@ func main() {
* [Coraza Playground](https://playground.coraza.io/): sandbox rule testing web interface
* [OWASP Core Ruleset](https://github.com/coreruleset/coreruleset/): Awesome rule set, compatible with Coraza

## Development

Coraza only requires Go for development. You can run `mage.go` to issue development commands.

See the list of commands

```shell
go run mage.go -l
```

For example, to format your code before submission, run

```shell
go run mage.go format
```

## Contribute

Contributions are welcome! Please refer to [CONTRIBUTING.md](https://github.com/corazawaf/coraza/blob/v2/master/CONTRIBUTING.md) for guidance.
Contributions are welcome! Please refer to [CONTRIBUTING.md](./CONTRIBUTING.md) for guidance.

## Thanks

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/anuraaga/go-modsecurity v0.0.0-20220816070944-f36055ce7d5d
github.com/corazawaf/libinjection-go v0.0.0-20220207031228-44e9c4250eb5
github.com/foxcpp/go-mockdns v1.0.0
github.com/magefile/mage v1.13.0
github.com/miekg/dns v1.1.50 // indirect
github.com/petar-dambovaliev/aho-corasick v0.0.0-20211021192214-5ab2d9280aa9
github.com/tidwall/gjson v1.14.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ github.com/corazawaf/libinjection-go v0.0.0-20220207031228-44e9c4250eb5 h1:Sukhx
github.com/corazawaf/libinjection-go v0.0.0-20220207031228-44e9c4250eb5/go.mod h1:OP4TM7xdJ2skyXqNX1AN1wN5nNZEmJNuWbNPOItn7aw=
github.com/foxcpp/go-mockdns v1.0.0 h1:7jBqxd3WDWwi/6WhDvacvH1XsN3rOLXyHM1uhvIx6FI=
github.com/foxcpp/go-mockdns v1.0.0/go.mod h1:lgRN6+KxQBawyIghpnl5CezHFGS9VLzvtVlwxvzXTQ4=
github.com/magefile/mage v1.13.0 h1:XtLJl8bcCM7EFoO8FyH8XK3t7G5hQAeK+i4tq+veT9M=
github.com/magefile/mage v1.13.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/miekg/dns v1.1.25/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/miekg/dns v1.1.50 h1:DQUfb9uc6smULcREF09Uc+/Gd46YWqJd5DbpPE9xkcA=
github.com/miekg/dns v1.1.50/go.mod h1:e3IlAVfNqAllflbibAZEWOXOQ+Ynzk/dDozDxY7XnME=
Expand Down
16 changes: 16 additions & 0 deletions mage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build ignore
// +build ignore

// Entrypoint to mage for running without needing to install the command.
// https://magefile.org/zeroinstall/
package main

import (
"os"

"github.com/magefile/mage/mage"
)

func main() {
os.Exit(mage.Main())
}
78 changes: 78 additions & 0 deletions magefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//go:build mage
// +build mage

// Copyright 2022 Juan Pablo Tosso
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package main

import (
"errors"
"fmt"
"os"

"github.com/magefile/mage/mg"
"github.com/magefile/mage/sh"
)

var golangCILintVer = "v1.48.0" // https://github.com/golangci/golangci-lint/releases
var gosImportsVer = "v0.1.5" // https://github.com/rinchsan/gosimports/releases/tag/v0.1.5

var errCommitFormatting = errors.New("files not formatted, please commit formatting changes")

// Format formats code in this repository.
func Format() error {
if err := sh.RunV("go", "mod", "tidy"); err != nil {
return err
}
return sh.RunV("go", "run", fmt.Sprintf("github.com/rinchsan/gosimports/cmd/gosimports@%s", gosImportsVer), "-w", ".")
}

// Lint verifies code quality.
func Lint() error {
mg.SerialDeps(Format)

if sh.Run("git", "diff", "--exit-code") != nil {
return errCommitFormatting
}

return sh.RunV("go", "run", fmt.Sprintf("github.com/golangci/golangci-lint/cmd/golangci-lint@%s", golangCILintVer), "run")
}

// Test runs all tests.
func Test() error {
return sh.RunV("go", "test", "./...")
}

// Coverage runs tests with coverage and race detector enabled.
func Coverage() error {
if err := os.MkdirAll("build", 0755); err != nil {
return err
}
if err := sh.RunV("go", "test", "-race", "-coverprofile=build/coverage.txt", "-covermode=atomic", "-coverpkg=./...", "./..."); err != nil {
return err
}

return sh.RunV("go", "tool", "cover", "-html=build/coverage.txt", "-o", "build/coverage.html")
}

// Doc runs godoc, access at http://localhost:6060
func Doc() error {
return sh.RunV("go", "run", "golang.org/x/tools/cmd/godoc@latest", "-http=:6060")
}

// Check runs tests and lint.
func Check() {
mg.SerialDeps(Test, Lint)
}

0 comments on commit 671b082

Please sign in to comment.