versionCheckHook: add versionCheckKeepEnvironment parameter

This replaces the `versionCheckDontIgnoreEnvironment` parameter. Keeping
all environment variables is still possible by using
`versionCheckKeepEnvironment = "*";`.
This commit is contained in:
Defelo 2025-05-28 02:16:32 +02:00
parent 0b7fd52371
commit 23bd27a21c
No known key found for this signature in database
GPG key ID: 2A05272471204DD3
2 changed files with 15 additions and 5 deletions

View file

@ -33,6 +33,7 @@ The variables that this phase control are:
- `dontVersionCheck`: Disable adding this hook to the [`preInstallCheckHooks`](#ssec-installCheck-phase). Useful if you do want to load the bash functions of the hook, but run them differently.
- `versionCheckProgram`: The full path to the program that should print the `${version}` string. Defaults roughly to `${placeholder "out"}/bin/${pname}`. Using `$out` in the value of this variable won't work, as environment variables from this variable are not expanded by the hook. Hence using `placeholder` is unavoidable.
- `versionCheckProgramArg`: The argument that needs to be passed to `versionCheckProgram`. If undefined the hook tries first `--help` and then `--version`. Examples: `version`, `-V`, `-v`.
- `versionCheckKeepEnvironment`: A list of environment variables to keep and pass to the command. Only those variables should be added to this list that are actually required for the version command to work. If it is not feasible to explicitly list all these environment variables you can set this parameter to the special value `"*"` to disable the `--ignore-environment` flag and thus keep all environment variables.
- `preVersionCheck`: A hook to run before the check is done.
- `postVersionCheck`: A hook to run after the check is done.

View file

@ -1,10 +1,19 @@
_handleCmdOutput(){
local command=("$1" "$2")
local versionOutput
local envArgs=()
if [[ "$3" != "*" ]]; then
envArgs+=("--ignore-environment")
for var in $3; do
envArgs+=("$var=${!var}")
done
fi
versionOutput="$(env \
--chdir=/ \
--argv0="$(basename "${command[0]}")" \
$( [[ -z "$3" || "$3" = "0" ]] && echo --ignore-environment ) \
"${envArgs[@]}" \
"${command[@]}" 2>&1 \
| sed -e 's|@storeDir@/[^/ ]*/|{{storeDir}}/|g' \
|| true)"
@ -25,8 +34,8 @@ versionCheckHook(){
runHook preVersionCheck
echo Executing versionCheckPhase
# Enable --ignore-environment by default unless explicitly disabled
: "${versionCheckDontIgnoreEnvironment:=0}"
# Don't keep any environment variables by default
: "${versionCheckKeepEnvironment:=}"
local cmdProgram cmdArg echoPrefix
if [[ -z "${versionCheckProgram-}" ]]; then
@ -47,14 +56,14 @@ versionCheckHook(){
fi
if [[ -z "${versionCheckProgramArg}" ]]; then
for cmdArg in "--help" "--version"; do
echoPrefix="$(_handleCmdOutput "$cmdProgram" "$cmdArg" "$versionCheckDontIgnoreEnvironment")"
echoPrefix="$(_handleCmdOutput "$cmdProgram" "$cmdArg" "$versionCheckKeepEnvironment")"
if [[ "$echoPrefix" == "Successfully managed to" ]]; then
break
fi
done
else
cmdArg="$versionCheckProgramArg"
echoPrefix="$(_handleCmdOutput "$cmdProgram" "$cmdArg" "$versionCheckDontIgnoreEnvironment")"
echoPrefix="$(_handleCmdOutput "$cmdProgram" "$cmdArg" "$versionCheckKeepEnvironment")"
fi
if [[ "$echoPrefix" == "Did not" ]]; then
exit 2