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: add goimports as a hook #518

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions modules/hooks.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a way to do with args instead of manually specifying flags here and setting them above?

Copy link
Member

Choose a reason for hiding this comment

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

Don't think so, since $@ is already used for the files.

Copy link
Member

Choose a reason for hiding this comment

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

If the tool is not too complicated, the general pattern is that we add dedicated options to make them show up in the docs of downstream projects (flake-parts, devenv).
Some hooks will have an extraFlags/extraOptions setting to capture everything. Use the existing hooks for inspiration.

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.";
Expand Down
2 changes: 2 additions & 0 deletions nix/tools.nix
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
, go-tools
, golangci-lint
, golines
, gotools
hmajid2301 marked this conversation as resolved.
Show resolved Hide resolved
, revive ? null
, vale
}:
Expand Down Expand Up @@ -125,6 +126,7 @@ in
go-tools
golangci-lint
golines
gotools
gptcommit
hadolint
hindent
Expand Down
Loading