From 7119efbbe0539aface477644f93ea21886af2513 Mon Sep 17 00:00:00 2001 From: Sebastian Estrella <2049686+sestrella@users.noreply.github.com> Date: Wed, 27 Mar 2024 00:21:46 -0500 Subject: [PATCH 1/6] feat: Create different hooks for Terraform and OpenTofu format --- modules/hooks.nix | 23 ++++++++++++++++++++--- nix/terraform-fmt/default.nix | 12 ------------ nix/tools.nix | 3 ++- 3 files changed, 22 insertions(+), 16 deletions(-) delete mode 100644 nix/terraform-fmt/default.nix diff --git a/modules/hooks.nix b/modules/hooks.nix index 43509afd..62ceeb96 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3655,9 +3655,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() { + 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$"; }; terraform-validate = diff --git a/nix/terraform-fmt/default.nix b/nix/terraform-fmt/default.nix deleted file mode 100644 index 2e18714a..00000000 --- a/nix/terraform-fmt/default.nix +++ /dev/null @@ -1,12 +0,0 @@ -{ writeScriptBin, opentofu }: - -writeScriptBin "terraform-fmt" ''#!/usr/bin/env bash - for arg in "$@"; do - dirname "$arg" - done \ - | sort \ - | uniq \ - | while read dir; do - ${opentofu}/bin/tofu fmt "$dir" - done -'' diff --git a/nix/tools.nix b/nix/tools.nix index 7bcfda95..c509f8d3 100644 --- a/nix/tools.nix +++ b/nix/tools.nix @@ -51,6 +51,7 @@ , nodePackages , ocamlPackages , opam +, opentofu , ormolu , pkgsBuildBuild , poetry @@ -137,6 +138,7 @@ in mdsh nil nixpkgs-fmt + opentofu ormolu pre-commit-hook-ensure-sops poetry @@ -185,7 +187,6 @@ in hpack-dir = callPackage ./hpack-dir { }; 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; }; From 4c20aab2a3881f9335dbe12fcbd1c8ca8ec83bba Mon Sep 17 00:00:00 2001 From: Sebastian Estrella <2049686+sestrella@users.noreply.github.com> Date: Wed, 4 Dec 2024 23:38:24 -0500 Subject: [PATCH 2/6] Print help message --- modules/hooks.nix | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 62ceeb96..b6b93574 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3662,16 +3662,16 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol terraform-fmt = pkgs.writeScriptBin "terraform-fmt" '' #!/usr/bin/env bash - opentofu_or_terraform() { - local bin_dir=${hooks.terraform-format.package} - if [ -f "''${bin_dir}/bin/tofu" ]; then - ''${bin_dir}/bin/tofu "$@" - else - ''${bin_dir}/bin/terraform "$@" - fi + print_help() { + echo "Run `$1 fmt -check -diff -recursive` to format the code" + exit 1 } - opentofu_or_terraform fmt -check -diff "$@" + if [ -f "${hooks.terraform-format.package}/bin/tofu" ]; then + ${hooks.terraform-format.package}/bin/tofu fmt -check -diff "$@" || print_help "tofu" + else + ${hooks.terraform-format.package}/bin/terraform fmt -check -diff "$@" || print_help "terraform" + fi ''; in "${terraform-fmt}/bin/terraform-fmt"; From 096efd380b13e7fe270aea4fa9d58e856f41bada Mon Sep 17 00:00:00 2001 From: Sebastian Estrella <2049686+sestrella@users.noreply.github.com> Date: Wed, 4 Dec 2024 23:41:32 -0500 Subject: [PATCH 3/6] Handle errors --- modules/hooks.nix | 2 ++ 1 file changed, 2 insertions(+) diff --git a/modules/hooks.nix b/modules/hooks.nix index b6b93574..e24dd943 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3662,6 +3662,8 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol terraform-fmt = pkgs.writeScriptBin "terraform-fmt" '' #!/usr/bin/env bash + set -euo pipefail + print_help() { echo "Run `$1 fmt -check -diff -recursive` to format the code" exit 1 From d6c0b7566872e4fbe31ef26284f00f295aeeba7c Mon Sep 17 00:00:00 2001 From: Sebastian Estrella <2049686+sestrella@users.noreply.github.com> Date: Wed, 4 Dec 2024 23:42:33 -0500 Subject: [PATCH 4/6] Properly quote command --- modules/hooks.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index e24dd943..19f4e2b8 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3665,7 +3665,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol set -euo pipefail print_help() { - echo "Run `$1 fmt -check -diff -recursive` to format the code" + echo "Run '$1 fmt -check -diff -recursive' to format the code" exit 1 } From 580f2de16d4d8fa3fa5c6aabc169cd9fd1333488 Mon Sep 17 00:00:00 2001 From: Sebastian Estrella <2049686+sestrella@users.noreply.github.com> Date: Wed, 4 Dec 2024 23:44:53 -0500 Subject: [PATCH 5/6] Update suggested command --- modules/hooks.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 19f4e2b8..4b4c60d0 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3665,7 +3665,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol set -euo pipefail print_help() { - echo "Run '$1 fmt -check -diff -recursive' to format the code" + echo "Run '$1 fmt -recursive' to format the code" exit 1 } From b1fa50729d92e35a0cbfdf8c949cdaf0b5bf3847 Mon Sep 17 00:00:00 2001 From: Sebastian Estrella <2049686+sestrella@users.noreply.github.com> Date: Sat, 4 Jan 2025 22:43:02 -0500 Subject: [PATCH 6/6] Refactor entry script --- modules/hooks.nix | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/modules/hooks.nix b/modules/hooks.nix index 4b4c60d0..d58931b5 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -3657,26 +3657,7 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol name = "terraform-format"; description = "Format Terraform (`.tf`) files."; package = tools.opentofu; - entry = - let - terraform-fmt = pkgs.writeScriptBin "terraform-fmt" '' - #!/usr/bin/env bash - - set -euo pipefail - - print_help() { - echo "Run '$1 fmt -recursive' to format the code" - exit 1 - } - - if [ -f "${hooks.terraform-format.package}/bin/tofu" ]; then - ${hooks.terraform-format.package}/bin/tofu fmt -check -diff "$@" || print_help "tofu" - else - ${hooks.terraform-format.package}/bin/terraform fmt -check -diff "$@" || print_help "terraform" - fi - ''; - in - "${terraform-fmt}/bin/terraform-fmt"; + entry = "${lib.getExe hooks.terraform-format.package} fmt -check -diff"; files = "\\.tf$"; }; terraform-validate =