Skip to content

Commit 6671dda

Browse files
author
Roman Gonzalez
committed
feat: add new golang hooks
* gotest * staticcheck * gofmt
1 parent a4548c0 commit 6671dda

File tree

3 files changed

+103
-1
lines changed

3 files changed

+103
-1
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: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,75 @@ in
761761
files = "\\.go$";
762762
};
763763

764+
gotest = {
765+
enable = true;
766+
name = "gotest";
767+
description = "Run go tests";
768+
entry =
769+
let
770+
script = pkgs.writeShellScript "precommit-gotest" ''
771+
set -e
772+
# find all directories that contain tests
773+
dirs=()
774+
for file in "$@"; do
775+
# either the file is a test
776+
if [[ "$file" = *_test.go ]]; then
777+
dirs+=("$(dirname "$file")")
778+
continue
779+
fi
780+
781+
# or the file has an associated test
782+
filename="''${file%.go}"
783+
test_file="''${filename}_test.go"
784+
if [[ -f "$test_file" ]]; then
785+
dirs+=("$(dirname "$test_file")")
786+
continue
787+
fi
788+
done
789+
790+
# ensure we are not duplicating dir entries
791+
IFS=$'\n' sorted_dirs=($(sort -u <<<"''${dirs[*]}")); unset IFS
792+
793+
# test each directory one by one
794+
for dir in "''${sorted_dirs[@]}"; do
795+
${tools.go}/bin/go test "./$dir"
796+
done
797+
'';
798+
in
799+
builtins.toString script;
800+
files = "\\.go$";
801+
raw = {
802+
# to avoid multiple invocations of the same directory input, provide
803+
# all file names in a single run.
804+
require_serial = true;
805+
};
806+
};
807+
808+
gofmt =
809+
{
810+
name = "gofmt";
811+
description = "A tool that automatically formats Go source code";
812+
entry =
813+
let
814+
script = pkgs.writeShellScript "precommit-gofmt" ''
815+
set -e
816+
failed=false
817+
for file in "$@"; do
818+
# redirect stderr so that violations and summaries are properly interleaved.
819+
if ! ${tools.go}/bin/gofmt -l -w "$file" 2>&1
820+
then
821+
failed=true
822+
fi
823+
done
824+
if [[ $failed == "true" ]]; then
825+
exit 1
826+
fi
827+
'';
828+
in
829+
builtins.toString script;
830+
files = "\\.go$";
831+
};
832+
764833
revive =
765834
{
766835
name = "revive";
@@ -798,6 +867,35 @@ in
798867
};
799868
};
800869

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