Skip to content

Commit 5ffa2e2

Browse files
committed
task: add golang precommit hooks
1 parent 3dabaed commit 5ffa2e2

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

.lintstagedrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"**/*.{js,jsx,ts,tsx,json,md}": ["prettier --write"],
3-
"**/*.js": ["jest --findRelatedTests --passWithNoTests"]
3+
"**/*.js": ["jest --findRelatedTests --passWithNoTests"],
4+
"**/*.go": ["./tools/hooks/golang-precommit.sh"]
45
}

CONTRIBUTING.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Node.js v20 or later
88
- npm v8 or later
99
- Git
10+
- Go 1.21 or later (for CLI development)
1011

1112
### Installation
1213

@@ -21,11 +22,17 @@
2122
npm install
2223
```
2324

24-
3. Set up Git hooks (optional):
25+
3. Set up Git hooks (recommended):
2526
```bash
2627
npm run precommit
2728
```
2829

30+
4. If working on the Go CLI, set up the Go environment:
31+
```bash
32+
cd tools/cli
33+
make setup
34+
```
35+
2936
This will install Husky, which manages Git hooks for the project. The hooks ensure code quality by automatically running various checks before commits.
3037

3138
## Development Workflow
@@ -34,16 +41,27 @@ This will install Husky, which manages Git hooks for the project. The hooks ensu
3441

3542
This project uses the following Git hooks:
3643

37-
- **pre-commit**: Automatically formats your code using Prettier and runs tests for staged JavaScript files to ensure code quality before each commit.
44+
- **pre-commit**:
45+
- Automatically formats your code using Prettier and runs tests for staged JavaScript files
46+
- For Go files, runs formatting, linting, and tests using the project's Makefile
3847

3948
### Available Scripts
4049

50+
#### JavaScript/Node.js
4151
- `npm run format` - Format all files with Prettier
4252
- `npm run format-check` - Check formatting without modifying files
4353
- `npm run lint-js` - Lint JavaScript files
4454
- `npm run test` - Run all tests
4555

4656
#### IPA specific targets
47-
4857
- `npm run gen-ipa-docs` - Generate IPA ruleset documentation (see `./tools` folder for more information)
4958
- `npm run ipa-validation` - Run OpenAPI validation with Spectral
59+
60+
#### Go OpenAPI CLI (internal)
61+
When working in the `tools/cli` directory:
62+
- `make fmt` - Format Go code
63+
- `make lint` - Run golangci-lint
64+
- `make unit-test` - Run Go unit tests
65+
- `make e2e-test` - Run end-to-end tests
66+
- `make build` - Build the CLI binary
67+
- `make gen-docs` - Generate CLI documentation

tools/hooks/golang-precommit.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# Get the root directory of the project
5+
ROOT_DIR=$(git rev-parse --show-toplevel)
6+
7+
# Change to the CLI directory where the Makefile is located
8+
cd "$ROOT_DIR/tools/cli"
9+
10+
# Format Go files
11+
echo "Formatting Go files..."
12+
make fmt
13+
14+
# Run golangci-lint
15+
echo "Running golangci-lint..."
16+
make lint
17+
18+
# Run unit tests for Go files
19+
echo "Running Go unit tests..."
20+
make unit-test
21+
22+
# Return to the original directory
23+
cd -
24+
25+
# Return 0 to indicate success
26+
exit 0

0 commit comments

Comments
 (0)