Skip to content

Commit 12b3e30

Browse files
committed
Add swag-init Hook
- Define script to be run for swag-init - Add documentation for swag-init hook
1 parent 6246134 commit 12b3e30

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

Diff for: .pre-commit-hooks.yaml

+11-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,15 @@
55
always_run: false
66
fail_fast: false
77
verbose: false
8-
description: 'Run swag fmt to format Swaggo comments in your code'
8+
description: 'Run swag fmt to format Swag annotations in your code.'
9+
args: []
10+
11+
- id: swag-init
12+
name: swag-init
13+
entry: run-swag-init.sh
14+
language: script
15+
always_run: false
16+
fail_fast: false
17+
verbose: false
18+
description: 'Run swag init to generate Swagger docs from Swag annotations, and generate the required files.'
919
args: []

Diff for: README.md

+20-2
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@ repos:
1313
rev: "main"
1414
hooks:
1515
- id: swag-fmt
16+
- id: swag-init
1617
```
1718
1819
Arguments to `swag` can be passed as `args`.
1920

20-
For example, to pass the `--exclude` flag to `swag fmt`:
21+
The example below shows:
22+
23+
- How to pass the `--exclude` flag to `swag fmt`
24+
- How to run the equivalent of `swag init` with these flags:
25+
26+
```
27+
swag init --parseInternal --requiredByDefault --parseDependency --dir "./cmd/api/" --generalInfo routes.go --output ./cmd/api/docs/
28+
```
2129

2230
```yaml
2331
repos:
@@ -27,7 +35,15 @@ repos:
2735
hooks:
2836
- id: swag-fmt
2937
args:
30-
- --exclude ./a/special/file.go
38+
- --exclude=./a/special/file.go
39+
- id: swag-init
40+
args:
41+
- --parseInternal
42+
- --requiredByDefault
43+
- --parseDependency
44+
- --dir=./cmd/api/
45+
- --generalInfo=routes.go
46+
- --output=./cmd/api/docs/
3147
```
3248

3349
## Available Hooks
@@ -36,3 +52,5 @@ All of these hooks require [swaggo/swag](https://github.com/swaggo/swag#getting-
3652
in your `$PATH`.
3753

3854
- `swag-fmt` - Runs `swag fmt` on all files in the project.
55+
- `swag-init` - Runs `swag init` on the project. Please
56+
reference [available CLI flags in the documentation](https://github.com/swaggo/swag#swag-cli).

Diff for: run-swag-fmt.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ then
66
exit 1
77
fi
88

9-
swag fmt $@
9+
swag fmt "$@"

Diff for: run-swag-init.sh

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env bash
2+
3+
if ! which swag &> /dev/null
4+
then
5+
echo "swag could not be found. Please ensure it is installed and available on your PATH."
6+
exit 1
7+
fi
8+
9+
swag init "$@"

0 commit comments

Comments
 (0)