Skip to content

feat: pass module dir as --path-prefix arg to golangci-lint #32

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ You can copy/paste the following snippet into your `.pre-commit-config.yaml` fil
# - Can be configured to replace MOST other hooks
# - Supports repo config file for configuration
# - https://github.com/golangci/golangci-lint
# - Use arg `--hook:path-prefix` to indicate that the repository root
# directory should be passed to the tool (for `*-mod` hooks)
#
- id: golangci-lint
- id: golangci-lint-mod
Expand Down
1 change: 1 addition & 0 deletions golangci-lint-mod.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
cmd=(golangci-lint run)
cmd_cwd_arg="--path-prefix"
. "$(dirname "${0}")/lib/cmd-mod.bash"
1 change: 1 addition & 0 deletions golangci-lint-repo-mod.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/usr/bin/env bash
cmd=(golangci-lint run)
cmd_cwd_arg="--path-prefix"
. "$(dirname "${0}")/lib/cmd-repo-mod.bash"
3 changes: 3 additions & 0 deletions lib/cmd-mod.bash
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ error_code=0
#
for sub in $(find_module_roots "${FILES[@]}" | sort -u); do
pushd "${sub}" > /dev/null || exit 1
if [ "${use_path_prefix:-}" -eq 1 ] && [ "${cmd_cwd_arg:-}" ]; then
OPTIONS+=("${cmd_cwd_arg}=${sub}")
Copy link
Owner

Choose a reason for hiding this comment

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

Ok I've been thinking on this for a while now ... the thing that is holding up is the =${sub} ...

I don't like assuming that the target tool will understand arg=val (vs arg val) ...

SOO, I'm wondering if the quick fix is to treat cmd_cwd_arg as a template ... ie.

For golangic-lint, the value would be:

cmd_cwd_arg="--path-prefix={{sub}}"

And our insertion script would know to replace {{sub}} with the value of ${sub} ...

This would make it so we don't have to assume the format of argument+value ...

Copy link
Author

Choose a reason for hiding this comment

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

Hey, thanks a lot for this feedback - apologies for taking so long to circle back on this. Saying "life happened" would be an understatement... 😓

Anyway, this all makes perfect sense - I've adjusted my PR accordingly with the templating concept. This also allows for {{sub}} to be used multiple times in the argument, if the directory name ever needed to be set more than once. See what you think and let me know if anything else is needed.

fi
if [ "${error_on_output:-}" -eq 1 ]; then
output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1)
if [ -n "${output}" ]; then
Expand Down
3 changes: 3 additions & 0 deletions lib/cmd-repo-mod.bash
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ error_code=0
#
for sub in $(find . -name go.mod -not -path '*/vendor/*' -exec dirname "{}" ';' | sort -u); do
pushd "${sub}" > /dev/null || exit 1
if [ "${use_path_prefix:-}" -eq 1 ] && [ "${cmd_cwd_arg:-}" ]; then
OPTIONS+=("${cmd_cwd_arg}=${sub}")
fi
if [ "${error_on_output:-}" -eq 1 ]; then
output=$(/usr/bin/env "${ENV_VARS[@]}" "${cmd[@]}" "${OPTIONS[@]}" 2>&1)
if [ -n "${output}" ]; then
Expand Down
5 changes: 5 additions & 0 deletions lib/common.bash
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

: "${use_dot_dot_dot:=1}"
: "${error_on_output:=0}"
: "${use_path_prefix:=0}"
: "${ignore_file_pattern_array:=}"

##
Expand Down Expand Up @@ -66,6 +67,10 @@ function parse_file_hook_args {
fi
shift
;;
--hook:path-prefix)
use_path_prefix=1
shift
;;
--hook:*)
printf "ERROR: Unknown hook option: '%s'\n" "${1}" >&2
exit 1
Expand Down