diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 791a460..5c32523 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,12 +11,12 @@ jobs: build: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Go - uses: actions/setup-go@v3 + uses: actions/setup-go@v4 with: - go-version: 1.18 + go-version: '1.20' - name: Vet run: go vet -c=2 diff --git a/.golangci.yml b/.golangci.yml new file mode 100755 index 0000000..1c8fdfe --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,111 @@ +linters-settings: + govet: + check-shadowing: true + misspell: + locale: US + exhaustive: + default-signifies-exhaustive: true + gomodguard: + blocked: + modules: + - github.com/pkg/errors: + recommendations: + - errors + gci: + sections: + - standard + - default + - blank + - dot + custom-order: true + +linters: + enable: + - asciicheck # Simple linter to check that your code does not contain non-ASCII identifiers + - bodyclose # checks whether HTTP response body is closed successfully + - dogsled # Checks assignments with too many blank identifiers (e.g. x, _, _, _, := f()) + - dupl # Tool for code clone detection + - errcheck # Errcheck is a program for checking for unchecked errors in go programs. These unchecked errors can be critical bugs in some cases + - exhaustive # check exhaustiveness of enum switch statements + - exportloopref # checks for pointers to enclosing loop variables + - gochecknoglobals # Checks that no globals are present in Go code + - gochecknoinits # Checks that no init functions are present in Go code + - gocognit # Computes and checks the cognitive complexity of functions + - goconst # Finds repeated strings that could be replaced by a constant + - gocritic # The most opinionated Go source code linter + - godox # Tool for detection of FIXME, TODO and other comment keywords + - goerr113 # Golang linter to check the errors handling expressions + - gofmt # Gofmt checks whether code was gofmt-ed. By default this tool runs with -s option to check for code simplification + - goheader # Checks is file header matches to pattern + - goimports # Goimports does everything that gofmt does. Additionally it checks unused imports + - gomodguard # Allow and block list linter for direct Go module dependencies. This is different from depguard where there are different block types for example version constraints and module recommendations. + - gosec # Inspects source code for security problems + - gosimple # Linter for Go source code that specializes in simplifying a code + - govet # Vet examines Go source code and reports suspicious constructs, such as Printf calls whose arguments do not align with the format string + - ineffassign # Detects when assignments to existing variables are not used + - misspell # Finds commonly misspelled English words in comments + - nakedret # Finds naked returns in functions greater than a specified function length + - noctx # noctx finds sending http request without context.Context + - revive # Fast, configurable, extensible, flexible, and beautiful linter for Go. Drop-in replacement of golint + - staticcheck # Staticcheck is a go vet on steroids, applying a ton of static analysis checks + - stylecheck # Stylecheck is a replacement for golint + - typecheck # Like the front-end of a Go compiler, parses and type-checks Go code + - unconvert # Remove unnecessary type conversions + - unparam # Reports unused function parameters + - unused # Checks Go code for unused constants, variables, functions and types + - whitespace # Tool for detection of leading and trailing whitespace + disable: + - deadcode # Finds unused code + - depguard # Go linter that checks if package imports are in a list of acceptable packages + - funlen # Tool for detection of long functions + - gci # Gci control golang package import order and make it always deterministic. + - gocyclo # Computes and checks the cyclomatic complexity of functions + - godot # Check if comments end in a period + - gofumpt # Gofumpt checks whether code was gofumpt-ed. + - golint # Golint differs from gofmt. Gofmt reformats Go source code, whereas golint prints out style mistakes + - gomnd # An analyzer to detect magic numbers. + - goprintffuncname # Checks that printf-like functions are named with `f` at the end + - lll # Reports long lines + - maligned # Tool to detect Go structs that would take less memory if their fields were sorted + - nestif # Reports deeply nested if statements + - nlreturn # nlreturn checks for a new line before return and branch statements to increase code clarity + - nolintlint # Reports ill-formed or insufficient nolint directives + - prealloc # Finds slice declarations that could potentially be preallocated + - rowserrcheck # checks whether Err of rows is checked successfully + - scopelint # Scopelint checks for unpinned variables in go programs + - sqlclosecheck # Checks that sql.Rows and sql.Stmt are closed. + - structcheck # Finds unused struct fields + - testpackage # linter that makes you use a separate _test package + - varcheck # Finds unused global variables and constants + - wsl # Whitespace Linter - Forces you to use empty lines! + +issues: + exclude-use-default: false + exclude-rules: + # Allow complex tests, better to be self contained + - path: _test\.go + linters: + - gocognit + - noctx + - errcheck + - bodyclose + - goerr113 + - goconst + + # Allow complex main function in examples + - path: examples + text: "of func `main` is high" + linters: + - gocognit + + # Allow some DB mock rules + - path: dbmock\.go + linters: + - revive + - goerr113 + - goconst + +run: + skip-dirs-use-default: false + skip-dirs: + - examples diff --git a/.typos.toml b/.typos.toml new file mode 100644 index 0000000..562b8a4 --- /dev/null +++ b/.typos.toml @@ -0,0 +1,10 @@ +[default.extend-words] +#OpenSIPS = "OpenSIPS" + +[default.extend-identifiers] +# this just isn't worth the cost of fixing +exten = "exten" +Exten = "Exten" + +#[files] +#extend-exclude = ["*_test.go", "mock.go"] diff --git a/Makefile b/Makefile index 7c1c03b..e400aa3 100644 --- a/Makefile +++ b/Makefile @@ -28,3 +28,7 @@ docmd: clean: rm -f coverage.out go clean + +lint: + typos --config=.typos.toml + golangci-lint run diff --git a/README.md b/README.md index 2ea36c2..765e52b 100644 --- a/README.md +++ b/README.md @@ -14,7 +14,7 @@ Simple library that helps to build AGI scripts or FastAGI servers with Go. import "github.com/staskobzar/goagi" ``` -API documentation [link is here](docs/api.md). +API local documentation [link is here](docs/api.md) or [go.dev generated](https://pkg.go.dev/github.com/staskobzar/goagi) documentation. ## Usage FastAGI diff --git a/command.go b/command.go index b7b11c6..e4a9043 100644 --- a/command.go +++ b/command.go @@ -4,7 +4,7 @@ import ( "fmt" ) -// Command sends command as string to the AGI and returns response valus with +// Command sends command as string to the AGI and returns response values with // text response func (agi *AGI) Command(cmd string) (Response, error) { return agi.execute(cmd + "\n") @@ -227,8 +227,8 @@ recording is terminated, regardless of the escape_digits or timeout arguments If interrupted by DTMF, digits will be available in Response.Data() */ func (agi *AGI) RecordFile(file, format, escDigits string, - timeout, offset int, beep bool, silence int) (Response, error) { - + timeout, offset int, beep bool, silence int, +) (Response, error) { cmd := "RECORD FILE" cmd = fmt.Sprintf("%s %s %s %q %d", cmd, file, format, escDigits, timeout) if offset > 0 { diff --git a/docs/api.md b/docs/api.md index 87b33c8..5d187f6 100644 --- a/docs/api.md +++ b/docs/api.md @@ -146,7 +146,7 @@ Return values: func (agi *AGI) Command(cmd string) (Response, error) ``` -Command sends command as string to the AGI and returns response valus with text response +Command sends command as string to the AGI and returns response values with text response ### func \(\*AGI\) [ControlStreamFile]() diff --git a/examples/agi.go b/examples/agi.go index 9a90326..3606e80 100644 --- a/examples/agi.go +++ b/examples/agi.go @@ -1,8 +1,10 @@ // Example of usage NewAGI for Asterisk. +// // Dialplan example: // exten => _X.,1,NoOp(Test goagi) // same => n,Answer() // same => n,AGI(/path/to/application) +// // Reproduces Asterisk agi-test.agi script package main @@ -14,9 +16,7 @@ import ( "github.com/staskobzar/goagi" ) -var ( - tests, fail, pass int -) +var tests, fail, pass int func checkResult(err error, resp goagi.Response) { tests++ @@ -31,7 +31,7 @@ func checkResult(err error, resp goagi.Response) { log.Printf("Response endpos: %s", resp.EndPos()) } -func main() { +func main() { //nolint:typecheck agi, err := goagi.New(os.Stdin, os.Stdout, nil) if err != nil { log.Fatalln(err) diff --git a/go.mod b/go.mod index e5708a7..d210e8f 100644 --- a/go.mod +++ b/go.mod @@ -1,11 +1,11 @@ module github.com/staskobzar/goagi -go 1.18 +go 1.22 -require github.com/stretchr/testify v1.7.1 +require github.com/stretchr/testify v1.9.0 require ( - github.com/davecgh/go-spew v1.1.0 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 2dca7c9..60ce688 100644 --- a/go.sum +++ b/go.sum @@ -1,11 +1,10 @@ -github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= -github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= -github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= -github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= -gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=