Skip to content

muya/swaggo-hooks

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

swaggo-hooks

pre-commit hooks for Go projects using https://github.com/swaggo/swag

Usage

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 to swag 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/

Available Hooks

All of these hooks require swaggo/swag to be installed, and available in your $PATH.

Additional Notes

Usage with end-of-file-fixer

  • When using the swag-fmt hook together with end-of-file-fixer from pre-commit-hooks, consider:
    • Defining the end-of-file-fixer after the swag-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 where swag-fmt generates JSON without a new line at the end, and end-of-file-fixer keeps adding it:

      exclude: '^.*swagger\.json$'