Skip to content

Commit

Permalink
Wsl functions fixes
Browse files Browse the repository at this point in the history
Facade calls function on invalid action
command's short/long description overridable by callback function

tag: 2.3.3

# Breaking changes

wslvar and wslpath cache invalidation is done when calling Linux::Wsl::requireWsl
instead of calling it each time Linux::Wsl::cachedWslpath or Linux::Wsl::cachedVar
are called

# Bug fixes

- Cache::getPropertyValue now works with callback named with :: separator
- Linux::Wsl::initEnv fixed PATH initialization from Windows PATH
- Linux::Wsl::initEnv more reliable way of computing WSL_INTEROP
- Linux::Wsl::originalWslvar remove empty lines after the output
- Log::rotate not relying on seq anymore

# Compiler changes

N/A

# Binaries changes

N/A

# Updated Bash framework functions

N/A

# New Bash framework functions

N/A

# Documentation

N/A

# Validation/Tooling

N/A
  • Loading branch information
fchastanet committed Mar 18, 2024
1 parent 66581a9 commit c109f94
Show file tree
Hide file tree
Showing 22 changed files with 73 additions and 49 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ repos:
)
- repo: https://github.com/fchastanet/bash-tools-framework
rev: 2.3.2
rev: 2.3.3
hooks:
- id: fixShebangExecutionBit
- id: fixShebangExecutionBitGithubActions
Expand Down Expand Up @@ -128,7 +128,7 @@ repos:
stages: [manual]

- repo: https://github.com/fchastanet/bash-tools-framework
rev: 2.3.2
rev: 2.3.3
hooks:
- id: megalinterCheckVersion
- id: megalinterGithubAction
3 changes: 2 additions & 1 deletion bin/awkLint
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/buildBinFiles
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/buildPushDockerImage
Original file line number Diff line number Diff line change
Expand Up @@ -647,7 +647,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/definitionLint
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/doc
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/dockerLint
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/findShebangFiles
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/frameworkLint
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/installFacadeExample
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/megalinter
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/plantuml
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/runBuildContainer
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/shellcheckLint
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
3 changes: 2 additions & 1 deletion bin/test
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/getPropertyValue.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Cache::getPropertyValue() {
grep -E "^${key}=" "${propertyFile}" | cut -d'=' -f2
return 0
elif [[ "$(type -t "${propertyNotFoundCallback}")" = "function" ]]; then
value="$(${propertyNotFoundCallback} "$@")" || return $?
value="$("${propertyNotFoundCallback}" "$@")" || return $?
if [[ -n "${value}" ]]; then
echo -E "${key}=${value}" >>"${propertyFile}"
fi
Expand Down
4 changes: 1 addition & 3 deletions src/Linux/Wsl/cachedWslpath.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
Linux::Wsl::cachedWslpath() {
local -a args=("$@")
local tempEnvFile key

tempEnvFile="${WSL_TMPDIR:-${TMPDIR:-/tmp}}/bash_tools_wslpath_$(id -un)"
File::garbageCollect "${tempEnvFile}" "1"
tempEnvFile="${WSL_TMPDIR:-${PERSISTENT_TMPDIR:-/tmp}}/bash_tools_wslpath_$(id -un)"

key="$(Linux::Wsl::getKeyFromWslpathOptions "$@")"
Cache::getPropertyValue "${tempEnvFile}" "${key}" Linux::Wsl::originalWslpath "${args[@]}"
Expand Down
3 changes: 1 addition & 2 deletions src/Linux/Wsl/cachedWslvar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
# @feature cache
Linux::Wsl::cachedWslvar() {
local tempEnvFile
tempEnvFile="${WSL_TMPDIR:-${TMPDIR:-/tmp}}/bash_tools_wslvar_$(id -un)"
tempEnvFile="${WSL_TMPDIR:-${PERSISTENT_TMPDIR:-/tmp}}/bash_tools_wslvar_$(id -un)"

File::garbageCollect "${tempEnvFile}" "1"
Cache::getPropertyValue "${tempEnvFile}" "$1" Linux::Wsl::originalWslvar "$@"
}
55 changes: 31 additions & 24 deletions src/Linux/Wsl/initEnv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,43 @@
# when wsl installation is run under wsl is in progress
# @warning initialization skipped if script not run under wsl
# @noargs
# @env WINDOW_PATH String use this env var to populate PATH with WINDOWS_PATH values
# @set PATH String populated with WINDOW_PATH paths
# @set WSL_INTEROP String with current used process
# @set WSL_DISTRO_NAME String initialized using wslpath -m /
# @stderr diagnostics information is displayed
# @require Linux::Wsl::requireWsl
# @feature cache
Linux::Wsl::initEnv() {
if Assert::wsl; then
# Here we are parsing WINDOW_PATH env variable
# for each path we remove potential control character
# that could break PATH variable
# using "${dir}" we ensure path with spaces will be
# correctly handled
local dir, windowsDirs
# shellcheck disable=SC2207,SC2116
IFS=':' windowsDirs=($(echo "${WINDOW_PATH}"))
for dir in "${windowsDirs[@]}"; do
dir="$(echo "${dir}" | tr -d '[:cntrl:]')"
Env::pathPrepend "${dir}"
done
export PATH

local i
for i in $(pstree -np -s $$ | grep -o -E '[0-9]+'); do
if [[ -e "/run/WSL/${i}_interop" ]]; then
export WSL_INTEROP=/run/WSL/${i}_interop
fi
done
WSL_DISTRO_NAME="$(Linux::Wsl::cachedWslpath -m / | sed -E 's#.*\$/([^/]+)/$#\1#')"
export WSL_DISTRO_NAME
if ! Assert::wsl; then
return 0
fi
# Here we are parsing WINDOW_PATH env variable
# for each path we remove potential control character
# that could break PATH variable
# using "${dir}" we ensure path with spaces will be
# correctly handled
local dir windowsDirs
WINDOWS_PATH="$(wslvar PATH)"
# shellcheck disable=SC2207,SC2116
IFS=';' windowsDirs=($(echo "${WINDOWS_PATH}"))
for dir in "${windowsDirs[@]}"; do
dir="$(echo "${dir}" | tr -d '[:cntrl:]')"
Env::pathPrepend "${dir}"
done
export PATH

local p
while IFS="" read -r p || [[ -n "${p}" ]]; do
if [[ -e "/run/WSL/${p}_interop" ]]; then
export WSL_INTEROP=/run/WSL/${p}_interop
break
fi
done < <(
pstree --numeric-sort --show-pids --show-parents $$ |
head -1 |
grep -o -E '[0-9]+' |
tac
)
WSL_DISTRO_NAME="$(Linux::Wsl::cachedWslpath -m / | sed -E 's#.*\$/([^/]+)/$#\1#')"
export WSL_DISTRO_NAME
}
2 changes: 1 addition & 1 deletion src/Linux/Wsl/originalWslvar.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
# @stdout wslvar stdout
# @require Linux::Wsl::requireWsl
Linux::Wsl::originalWslvar() {
wslvar "$@"
wslvar "$@" | sed -z '$ s/[\r\n]$//'
}
7 changes: 6 additions & 1 deletion src/Linux/Wsl/requireWsl.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@
# @description ensure linux runs under wsl
# @exitcode 1 if linux does not run under wsl
Linux::Wsl::requireWsl() {
Assert::wsl
Assert::wsl || return 1
local tempEnvDir
tempEnvDir="${WSL_TMPDIR:-${PERSISTENT_TMPDIR:-/tmp}}"
File::garbageCollect "${tempEnvDir}/bash_tools_wslpath_$(id -un)" "1"
tempEnvDir="${WSL_TMPDIR:-${PERSISTENT_TMPDIR:-/tmp}}"
File::garbageCollect "${tempEnvDir}/bash_tools_wslvar_$(id -un)" "1"
}
3 changes: 2 additions & 1 deletion src/Log/rotate.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Log::rotate() {
Log::displaySkipped "Log file ${file} doesn't exist yet"
return 0
fi
for i in $(seq $((maxLogFilesCount - 1)) -1 1); do
local i
for ((i = maxLogFilesCount - 1; i > 0; i--)); do
Log::displayInfo "Log rotation ${file}.${i} to ${file}.$((i + 1))"
mv "${file}."{"${i}","$((i + 1))"} &>/dev/null || true
done
Expand Down

0 comments on commit c109f94

Please sign in to comment.