Skip to content

Commit f3b4028

Browse files
authored
Merge pull request #222 from roman/roman/go/add-new-hooks
feat: add new hooks for golang
2 parents a4548c0 + 4924de7 commit f3b4028

File tree

3 files changed

+117
-11
lines changed

3 files changed

+117
-11
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,11 @@ use nix
129129

130130
## Golang
131131

132+
- gofmt: Runs `go fmt`
133+
- gotest: Runs `go test`
132134
- [govet](https://pkg.go.dev/cmd/vet)
133135
- [revive](https://github.com/mgechev/revive)
136+
- [staticcheck](https://github.com/dominikh/go-tools)
134137

135138
## Shell
136139

modules/hooks.nix

Lines changed: 112 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ let
77
lib.optionalString
88
(settings.rust.cargoManifestPath != null)
99
"--manifest-path ${lib.escapeShellArg settings.rust.cargoManifestPath}";
10+
11+
mkCmdArgs = predActionList:
12+
lib.concatStringsSep
13+
" "
14+
(builtins.foldl'
15+
(acc: entry:
16+
acc ++ lib.optional (builtins.elemAt entry 0) (builtins.elemAt entry 1))
17+
[ ]
18+
predActionList);
19+
1020
in
1121
{
1222
options.settings =
@@ -761,23 +771,86 @@ in
761771
files = "\\.go$";
762772
};
763773

774+
gotest = {
775+
enable = true;
776+
name = "gotest";
777+
description = "Run go tests";
778+
entry =
779+
let
780+
script = pkgs.writeShellScript "precommit-gotest" ''
781+
set -e
782+
# find all directories that contain tests
783+
dirs=()
784+
for file in "$@"; do
785+
# either the file is a test
786+
if [[ "$file" = *_test.go ]]; then
787+
dirs+=("$(dirname "$file")")
788+
continue
789+
fi
790+
791+
# or the file has an associated test
792+
filename="''${file%.go}"
793+
test_file="''${filename}_test.go"
794+
if [[ -f "$test_file" ]]; then
795+
dirs+=("$(dirname "$test_file")")
796+
continue
797+
fi
798+
done
799+
800+
# ensure we are not duplicating dir entries
801+
IFS=$'\n' sorted_dirs=($(sort -u <<<"''${dirs[*]}")); unset IFS
802+
803+
# test each directory one by one
804+
for dir in "''${sorted_dirs[@]}"; do
805+
${tools.go}/bin/go test "./$dir"
806+
done
807+
'';
808+
in
809+
builtins.toString script;
810+
files = "\\.go$";
811+
raw = {
812+
# to avoid multiple invocations of the same directory input, provide
813+
# all file names in a single run.
814+
require_serial = true;
815+
};
816+
};
817+
818+
gofmt =
819+
{
820+
name = "gofmt";
821+
description = "A tool that automatically formats Go source code";
822+
entry =
823+
let
824+
script = pkgs.writeShellScript "precommit-gofmt" ''
825+
set -e
826+
failed=false
827+
for file in "$@"; do
828+
# redirect stderr so that violations and summaries are properly interleaved.
829+
if ! ${tools.go}/bin/gofmt -l -w "$file" 2>&1
830+
then
831+
failed=true
832+
fi
833+
done
834+
if [[ $failed == "true" ]]; then
835+
exit 1
836+
fi
837+
'';
838+
in
839+
builtins.toString script;
840+
files = "\\.go$";
841+
};
842+
764843
revive =
765844
{
766845
name = "revive";
767846
description = "A linter for Go source code.";
768847
entry =
769848
let
770849
cmdArgs =
771-
lib.concatStringsSep
772-
" "
773-
(builtins.concatLists [
774-
[ "-set_exit_status" ]
775-
(if settings.revive.configPath != "" then
776-
[ "-config ${settings.revive.configPath}" ]
777-
else
778-
[ ])
779-
]
780-
);
850+
mkCmdArgs [
851+
[ true "-set_exit_status" ]
852+
[ (settings.revive.configPath != "") "-config ${settings.revive.configPath}" ]
853+
];
781854
# revive works with both files and directories; however some lints
782855
# may fail (e.g. package-comment) if they run on an individual file
783856
# rather than a package/directory scope; given this let's get the
@@ -798,6 +871,35 @@ in
798871
};
799872
};
800873

874+
staticcheck =
875+
{
876+
enable = true;
877+
name = "staticcheck";
878+
description = "State of the art linter for the Go programming language";
879+
# staticheck works with directories.
880+
entry =
881+
let
882+
script = pkgs.writeShellScript "precommit-staticcheck" ''
883+
err=0
884+
for dir in $(echo "$@" | xargs -n1 dirname | sort -u); do
885+
${tools.go-tools}/bin/staticcheck ./"$dir"
886+
code="$?"
887+
if [[ "$err" -eq 0 ]]; then
888+
err="$code"
889+
fi
890+
done
891+
exit $err
892+
'';
893+
in
894+
builtins.toString script;
895+
files = "\\.go$";
896+
raw = {
897+
# to avoid multiple invocations of the same directory input, provide
898+
# all file names in a single run.
899+
require_serial = true;
900+
};
901+
};
902+
801903
editorconfig-checker =
802904
{
803905
name = "editorconfig-checker";

nix/tools.nix

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
, writeScript
4343
, writeText
4444
, go
45+
, go-tools
4546
, revive ? null
4647
}:
4748

@@ -52,7 +53,7 @@ let
5253
};
5354
in
5455
{
55-
inherit actionlint alejandra cabal-fmt cabal2nix cargo clang-tools clippy deadnix dhall editorconfig-checker hadolint hindent hlint hpack html-tidy nix-linter nixfmt nixpkgs-fmt ormolu rustfmt shellcheck shfmt statix stylish-haskell stylua typos go mdsh revive;
56+
inherit actionlint alejandra cabal-fmt cabal2nix cargo clang-tools clippy deadnix dhall editorconfig-checker hadolint hindent hlint hpack html-tidy nix-linter nixfmt nixpkgs-fmt ormolu rustfmt shellcheck shfmt statix stylish-haskell stylua typos go mdsh revive go-tools;
5657
inherit (elmPackages) elm-format elm-review elm-test;
5758
# TODO: these two should be statically compiled
5859
inherit (haskellPackages) brittany fourmolu;

0 commit comments

Comments
 (0)