Skip to content

Commit 8d17ee4

Browse files
authored
chore: add pre-commit hooks and variable ordering check (#7)
* chore: add pre-commit hooks * chore: update trivy action
1 parent d25a1dd commit 8d17ee4

5 files changed

Lines changed: 74 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: Pull request checks
2+
3+
on:
4+
pull_request:
5+
6+
permissions:
7+
contents: read
8+
pull-requests: write
9+
10+
jobs:
11+
lint:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v6
15+
- uses: actions/setup-python@v6
16+
with:
17+
python-version: '3.x'
18+
- uses: pre-commit/action@v3.0.1

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Local .terraform directories
22
**/.terraform/*
33

4+
# Terrform lock files
5+
.terraform.lock.hcl
6+
47
# .tfstate files
58
*.tfstate
69
*.tfstate.*

.pre-commit-config.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
repos:
2+
- repo: local
3+
hooks:
4+
- id: check-tf-variable-order
5+
name: Check OpenTofu variable ordering
6+
language: system
7+
entry: bash scripts/check-tf-variable-order.sh
8+
files: \.tf$

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ tofu init -upgrade
6666

6767
## Contributing
6868

69+
This repository uses [pre-commit](https://pre-commit.com) to enforce code
70+
style checks before each commit. Install it once after cloning:
71+
72+
**1. Install pre-commit**
73+
74+
```sh
75+
brew install pre-commit
76+
```
77+
78+
**2. Install the git hook**
79+
80+
```sh
81+
pre-commit install
82+
```
83+
84+
This symlinks the hook into `.git/hooks/pre-commit`. The checks will now run
85+
automatically on every `git commit`. To run them manually against all files:
86+
87+
```sh
88+
pre-commit run --all-files
89+
```
90+
6991
Follow the [contributing guidelines][contributing] to contribute to this
7092
repository.
7193

scripts/check-tf-variable-order.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
# Check that variable blocks in OpenTofu/Terraform files are in alphabetical order.
3+
4+
set -euo pipefail
5+
6+
failed=0
7+
8+
for file in "$@"; do
9+
vars=$(grep -E '^variable "' "$file" | sed 's/^variable "\([^"]*\)".*/\1/' || true)
10+
11+
[ -z "$vars" ] && continue
12+
13+
sorted=$(echo "$vars" | sort)
14+
15+
if [ "$vars" != "$sorted" ]; then
16+
echo "ERROR: Variables in $file are not in alphabetical order."
17+
echo " Current: $(echo "$vars" | tr '\n' ' ')"
18+
echo " Expected: $(echo "$sorted" | tr '\n' ' ')"
19+
failed=1
20+
fi
21+
done
22+
23+
exit $failed

0 commit comments

Comments
 (0)