Skip to content

Commit 4696ed5

Browse files
committed
feat: Create different hooks for Terraform and OpenTofu format
1 parent 6f4e2a2 commit 4696ed5

File tree

3 files changed

+22
-16
lines changed

3 files changed

+22
-16
lines changed

modules/hooks.nix

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3632,9 +3632,26 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
36323632
terraform-format =
36333633
{
36343634
name = "terraform-format";
3635-
description = "Format terraform (`.tf`) files.";
3636-
package = tools.terraform-fmt;
3637-
entry = "${hooks.terraform-format.package}/bin/terraform-fmt";
3635+
description = "Format Terraform (`.tf`) files.";
3636+
package = tools.opentofu;
3637+
entry =
3638+
let
3639+
terraform-fmt = pkgs.writeScriptBin "terraform-fmt" ''
3640+
#!/usr/bin/env bash
3641+
3642+
opentofu_or_terraform() {
3643+
local bin_dir=${hooks.terraform-format.package}
3644+
if [ -f "''${bin_dir}/bin/tofu" ]; then
3645+
''${bin_dir}/bin/tofu "$@"
3646+
else
3647+
''${bin_dir}/bin/terraform "$@"
3648+
fi
3649+
}
3650+
3651+
opentofu_or_terraform fmt -check -diff "$@"
3652+
'';
3653+
in
3654+
"${terraform-fmt}/bin/terraform-fmt";
36383655
files = "\\.tf$";
36393656
};
36403657
terraform-validate =

nix/terraform-fmt/default.nix

Lines changed: 0 additions & 12 deletions
This file was deleted.

nix/tools.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
, nodePackages
5252
, ocamlPackages
5353
, opam
54+
, opentofu
5455
, ormolu
5556
, pkgsBuildBuild
5657
, poetry
@@ -137,6 +138,7 @@ in
137138
mdsh
138139
nil
139140
nixpkgs-fmt
141+
opentofu
140142
ormolu
141143
pre-commit-hook-ensure-sops
142144
poetry
@@ -185,7 +187,6 @@ in
185187
hpack-dir = callPackage ./hpack-dir { };
186188
hunspell = callPackage ./hunspell { };
187189
purty = callPackage ./purty { purty = nodePackages.purty; };
188-
terraform-fmt = callPackage ./terraform-fmt { };
189190
terraform-validate = callPackage ./terraform-validate { };
190191
tflint = callPackage ./tflint { };
191192
dune-build-opam-files = callPackage ./dune-build-opam-files { dune = dune_3; inherit (pkgsBuildBuild) ocaml; };

0 commit comments

Comments
 (0)