pre-commit hooks for Go projects using https://github.com/swaggo/swag
Add this to your .pre-commit-config.yaml
file:
repos:
# ... other repos ...
- repo: https://github.com/muya/swaggo-hooks
rev: "main"
hooks:
- id: swag-fmt
- id: swag-init
Arguments to swag
can be passed as args
.
The example below shows:
- How to pass the
--exclude
flag toswag fmt
- How to run the equivalent of
swag init
with these flags:
swag init --parseInternal --requiredByDefault --parseDependency --dir "./cmd/api/" --generalInfo routes.go --output ./cmd/api/docs/
repos:
# ... other repos ...
- repo: https://github.com/muya/swaggo-hooks
rev: "main"
hooks:
- id: swag-fmt
args:
- --exclude=./a/special/file.go
- id: swag-init
args:
- --parseInternal
- --requiredByDefault
- --parseDependency
- --dir=./cmd/api/
- --generalInfo=routes.go
- --output=./cmd/api/docs/
All of these hooks require swaggo/swag to be installed, and available
in your $PATH
.
swag-fmt
- Runsswag fmt
on all files in the project.swag-init
- Runsswag init
on the project. Please reference available CLI flags in the documentation.
- When using the
swag-fmt
hook together withend-of-file-fixer
from pre-commit-hooks, consider:-
Defining the
end-of-file-fixer
after theswag-fmt
hook. -
Excluding the
swagger.json
file from pre-commit actions by adding this to the top-level exclude pattern, otherwise you end up in an infinite loop whereswag-fmt
generates JSON without a new line at the end, andend-of-file-fixer
keeps adding it:exclude: '^.*swagger\.json$'
-