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 all 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={{sub}}"
. "$(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={{sub}}"
. "$(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\}\}/"$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
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\}\}/"$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