Skip to content

Commit

Permalink
Merge pull request #482 from pjrm/add-terraform-validate
Browse files Browse the repository at this point in the history
feat: add terraform-validate
  • Loading branch information
domenkozar authored Aug 9, 2024
2 parents 3c977f1 + c2f6ace commit c7012d0
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
- **Trivial integration for Nix projects** (wires up a few things behind the scenes)

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

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

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


## Getting started

### devenv.sh
Expand Down Expand Up @@ -299,6 +298,7 @@ clang-format supports.
### Terraform

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

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

## Contributing hooks

Everyone is encouraged to add new hooks.

<!-- TODO generate option docs -->

Have a look at the [existing hooks](modules/hooks.nix) and the [options](modules/pre-commit.nix).

There's no guarantee the hook will be accepted, but the general guidelines are:
Expand Down
10 changes: 10 additions & 0 deletions modules/hooks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3369,6 +3369,16 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
entry = "${hooks.terraform-format.package}/bin/terraform-fmt";
files = "\\.tf$";
};
terraform-validate =
{
name = "terraform-validate";
description = "Validates terraform configuration files (`.tf`).";
package = tools.terraform-validate;
entry = "${hooks.terraform-validate.package}/bin/terraform-validate";
files = "\\.(tf(vars)?|terraform\\.lock\\.hcl)$";
excludes = [ "\\.terraform/.*$" ];
require_serial = true;
};
tflint =
{
name = "tflint";
Expand Down
13 changes: 13 additions & 0 deletions nix/terraform-validate/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{ writeScriptBin, opentofu }:

writeScriptBin "terraform-validate" ''#!/usr/bin/env bash
set -x
for arg in "$@"; do
dirname "$arg"
done \
| sort \
| uniq \
| while read dir; do
${opentofu}/bin/tofu validate "$dir"
done
''
1 change: 1 addition & 0 deletions nix/tools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ in
hunspell = callPackage ./hunspell { };
purty = callPackage ./purty { purty = nodePackages.purty; };
terraform-fmt = callPackage ./terraform-fmt { };
terraform-validate = callPackage ./terraform-validate { };
tflint = callPackage ./tflint { };
dune-build-opam-files = callPackage ./dune-build-opam-files { dune = dune_3; inherit (pkgsBuildBuild) ocaml; };
dune-fmt = callPackage ./dune-fmt { dune = dune_3; inherit (pkgsBuildBuild) ocaml; };
Expand Down

0 comments on commit c7012d0

Please sign in to comment.