Skip to content

Commit c7012d0

Browse files
authored
Merge pull request #482 from pjrm/add-terraform-validate
feat: add terraform-validate
2 parents 3c977f1 + c2f6ace commit c7012d0

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
- **Trivial integration for Nix projects** (wires up a few things behind the scenes)
88

99
- Provide a low-overhead build of all the tooling available for the hooks to use
10-
(naive implementation of calling nix-shell does bring some latency when committing)
10+
(naive implementation of calling nix-shell does bring some latency when committing)
1111

1212
- **Common hooks for languages** like Python, Haskell, Elm, etc. [see all hook options](https://devenv.sh/?q=pre-commit.hooks)
1313

1414
- Run hooks **as part of development** and **on during CI**
1515

16-
1716
## Getting started
1817

1918
### devenv.sh
@@ -299,6 +298,7 @@ clang-format supports.
299298
### Terraform
300299

301300
- `terraform-format`: built-in formatter (using [OpenTofu](https://opentofu.org/)'s [`fmt`](https://opentofu.org/docs/cli/commands/fmt/))
301+
- `terraform-validate`: built-in validator (using [OpenTofu](https://opentofu.org/)'s [`validate`](https://opentofu.org/docs/cli/commands/validate/))
302302
- [tflint](https://github.com/terraform-linters/tflint)
303303

304304
### YAML
@@ -435,12 +435,12 @@ Example configuration:
435435
Custom hooks are defined with the same schema as [pre-defined
436436
hooks](modules/pre-commit.nix).
437437

438-
439438
## Contributing hooks
440439

441440
Everyone is encouraged to add new hooks.
442441

443442
<!-- TODO generate option docs -->
443+
444444
Have a look at the [existing hooks](modules/hooks.nix) and the [options](modules/pre-commit.nix).
445445

446446
There's no guarantee the hook will be accepted, but the general guidelines are:

modules/hooks.nix

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3369,6 +3369,16 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
33693369
entry = "${hooks.terraform-format.package}/bin/terraform-fmt";
33703370
files = "\\.tf$";
33713371
};
3372+
terraform-validate =
3373+
{
3374+
name = "terraform-validate";
3375+
description = "Validates terraform configuration files (`.tf`).";
3376+
package = tools.terraform-validate;
3377+
entry = "${hooks.terraform-validate.package}/bin/terraform-validate";
3378+
files = "\\.(tf(vars)?|terraform\\.lock\\.hcl)$";
3379+
excludes = [ "\\.terraform/.*$" ];
3380+
require_serial = true;
3381+
};
33723382
tflint =
33733383
{
33743384
name = "tflint";

nix/terraform-validate/default.nix

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{ writeScriptBin, opentofu }:
2+
3+
writeScriptBin "terraform-validate" ''#!/usr/bin/env bash
4+
set -x
5+
for arg in "$@"; do
6+
dirname "$arg"
7+
done \
8+
| sort \
9+
| uniq \
10+
| while read dir; do
11+
${opentofu}/bin/tofu validate "$dir"
12+
done
13+
''

nix/tools.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ in
181181
hunspell = callPackage ./hunspell { };
182182
purty = callPackage ./purty { purty = nodePackages.purty; };
183183
terraform-fmt = callPackage ./terraform-fmt { };
184+
terraform-validate = callPackage ./terraform-validate { };
184185
tflint = callPackage ./tflint { };
185186
dune-build-opam-files = callPackage ./dune-build-opam-files { dune = dune_3; inherit (pkgsBuildBuild) ocaml; };
186187
dune-fmt = callPackage ./dune-fmt { dune = dune_3; inherit (pkgsBuildBuild) ocaml; };

0 commit comments

Comments
 (0)