From 8e6c4d5bc21cd076528179bdbc637fff56c7ef1d Mon Sep 17 00:00:00 2001 From: Haseeb Majid Date: Thu, 17 Oct 2024 19:22:00 +0100 Subject: [PATCH 1/2] feat: add goimports as a hook --- modules/hooks.nix | 39 +++++++++++++++++++++++++++++++++++++++ nix/tools.nix | 2 ++ 2 files changed, 41 insertions(+) diff --git a/modules/hooks.nix b/modules/hooks.nix index 7159fc44..76ebfaa3 100644 --- a/modules/hooks.nix +++ b/modules/hooks.nix @@ -563,6 +563,20 @@ in }; }; }; + goimports = mkOption { + description = "goimports hook"; + type = types.submodule { + imports = [ hookModule ]; + options.settings = { + flags = mkOption { + type = types.str; + description = "Flags passed to goimports."; + default = ""; + example = "-local github.com/example/repo -w ."; + }; + }; + }; + }; golines = mkOption { description = "golines hook"; type = types.submodule { @@ -2598,6 +2612,31 @@ lib.escapeShellArgs (lib.concatMap (ext: [ "--ghc-opt" "-X${ext}" ]) hooks.ormol builtins.toString script; files = "\\.go$"; }; + goimports = + { + name = "goimports"; + description = "Go import lines, adding missing ones and removing unreferenced ones."; + package = tools.gotools; + entry = + let + script = pkgs.writeShellScript "precommit-goimports" '' + set -e + failed=false + for file in "$@"; do + # redirect stderr so that violations and summaries are properly interleaved. + if ! ${hooks.gotools.package}/bin/goimports ${hooks.goimports.settings.flags} -w "$file" 2>&1 + then + failed=true + fi + done + if [[ $failed == "true" ]]; then + exit 1 + fi + ''; + in + builtins.toString script; + files = "\\.go$"; + }; golangci-lint = { name = "golangci-lint"; description = "Fast linters runner for Go."; diff --git a/nix/tools.nix b/nix/tools.nix index c21d3987..97e30457 100644 --- a/nix/tools.nix +++ b/nix/tools.nix @@ -85,6 +85,7 @@ , go-tools , golangci-lint , golines +, gotools , revive ? null , vale }: @@ -125,6 +126,7 @@ in go-tools golangci-lint golines + gotools gptcommit hadolint hindent From a7394093a8f70753b37b42c9b48bd1e932e21a01 Mon Sep 17 00:00:00 2001 From: Haseeb Majid Date: Tue, 19 Nov 2024 22:57:30 +0000 Subject: [PATCH 2/2] Update nix/tools.nix Co-authored-by: sander --- nix/tools.nix | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nix/tools.nix b/nix/tools.nix index 97e30457..ade2f02f 100644 --- a/nix/tools.nix +++ b/nix/tools.nix @@ -85,7 +85,7 @@ , go-tools , golangci-lint , golines -, gotools +, gotools ? null , revive ? null , vale }: