diff --git a/README.md b/README.md index 50586c8..09a770b 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/golangci-lint-mod.sh b/golangci-lint-mod.sh index bec73eb..8fe3d3b 100755 --- a/golangci-lint-mod.sh +++ b/golangci-lint-mod.sh @@ -1,3 +1,4 @@ #!/usr/bin/env bash cmd=(golangci-lint run) +cmd_cwd_arg="--path-prefix={{sub}}" . "$(dirname "${0}")/lib/cmd-mod.bash" diff --git a/golangci-lint-repo-mod.sh b/golangci-lint-repo-mod.sh index 3319f2a..ba6b9f2 100755 --- a/golangci-lint-repo-mod.sh +++ b/golangci-lint-repo-mod.sh @@ -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" diff --git a/lib/cmd-mod.bash b/lib/cmd-mod.bash index 4879221..6fb3fd3 100644 --- a/lib/cmd-mod.bash +++ b/lib/cmd-mod.bash @@ -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 diff --git a/lib/cmd-repo-mod.bash b/lib/cmd-repo-mod.bash index c44b9d5..b35271b 100644 --- a/lib/cmd-repo-mod.bash +++ b/lib/cmd-repo-mod.bash @@ -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 diff --git a/lib/common.bash b/lib/common.bash index 9d9da65..f1d8c54 100644 --- a/lib/common.bash +++ b/lib/common.bash @@ -2,6 +2,7 @@ : "${use_dot_dot_dot:=1}" : "${error_on_output:=0}" +: "${use_path_prefix:=0}" : "${ignore_file_pattern_array:=}" ## @@ -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