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-initArguments to swag can be passed as args.
The example below shows:
- How to pass the
--excludeflag toswag fmt - How to run the equivalent of
swag initwith 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 fmton all files in the project.swag-init- Runsswag initon the project. Please reference available CLI flags in the documentation.
- When using the
swag-fmthook together withend-of-file-fixerfrom pre-commit-hooks, consider:-
Defining the
end-of-file-fixerafter theswag-fmthook. -
Excluding the
swagger.jsonfile from pre-commit actions by adding this to the top-level exclude pattern, otherwise you end up in an infinite loop whereswag-fmtgenerates JSON without a new line at the end, andend-of-file-fixerkeeps adding it:exclude: '^.*swagger\.json$'
-