Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Change terraform-format to support both Terraform and OpenTofu packages #419

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
23 changes: 20 additions & 3 deletions modules/hooks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3225,9 +3225,26 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol
terraform-format =
{
name = "terraform-format";
description = "Format terraform (`.tf`) files.";
package = tools.terraform-fmt;
entry = "${hooks.terraform-format.package}/bin/terraform-fmt";
description = "Format Terraform (`.tf`) files.";
package = tools.opentofu;
entry =
let
terraform-fmt = pkgs.writeScriptBin "terraform-fmt" ''
#!/usr/bin/env bash

opentofu_or_terraform() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@totoroot Based on our discussion, I made a few changes to the existing terraform-format. For the time being, I feel it would be easier to alter the existing hook to work with both OpenTofu and Terraform. End users can choose whether to use OpenTofu (the default) or alter the package to Terraform, as shown in this example:

https://github.com/sestrella/test-pre-commit-hooks.nix/blob/main/devenv.nix

I'm planning to update the PR title and the issue accordingly.

local bin_dir=${hooks.terraform-format.package}
if [ -f "''${bin_dir}/bin/tofu" ]; then
''${bin_dir}/bin/tofu "$@"
else
''${bin_dir}/bin/terraform "$@"
fi
}

opentofu_or_terraform fmt -check -diff "$@"
'';
in
"${terraform-fmt}/bin/terraform-fmt";
files = "\\.tf$";
};
tflint =
Expand Down
12 changes: 0 additions & 12 deletions nix/terraform-fmt/default.nix

This file was deleted.

3 changes: 2 additions & 1 deletion nix/tools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
, nodePackages
, ocamlPackages
, opam
, opentofu
, ormolu
, pkgsBuildBuild
, poetry
Expand Down Expand Up @@ -125,6 +126,7 @@ in
nil
nixfmt
nixpkgs-fmt
opentofu
ormolu
pre-commit-hook-ensure-sops
poetry
Expand Down Expand Up @@ -168,7 +170,6 @@ in
hpack-dir = callPackage ./hpack-dir { };
hunspell = callPackage ./hunspell { };
purty = callPackage ./purty { purty = nodePackages.purty; };
terraform-fmt = callPackage ./terraform-fmt { };
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