V8.00.000.2025.06.17
All checks were successful
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Successful in 1m52s
All checks were successful
🛡️ Shell Script Linting / 🛡️ Shell Script Linting (push) Successful in 1m52s
Signed-off-by: Marc S. Weidner <msw@coresecret.dev>
This commit is contained in:
@@ -16,23 +16,23 @@
|
||||
# Globals:
|
||||
# BASH_SOURCE
|
||||
# Arguments:
|
||||
# 1: Explicitly provided Argument: filename of the caller LIB. (Better let the guard_sourcing() determine dynamically.)
|
||||
# 1: Explicitly provided Argument: filename of the caller LIB. (Better let the guard_sourcing || return "${ERR_GUARD_SOURCE}"() determine dynamically.)
|
||||
# Returns:
|
||||
# 0: Returns '0' in both cases as they are intended to be successful.
|
||||
#######################################
|
||||
guard_sourcing() {
|
||||
guard_sourcing || return "${ERR_GUARD_SOURCE}"() {
|
||||
### Determine the caller script (the library being sourced).
|
||||
declare var_src="${1:-${BASH_SOURCE[1]}}"
|
||||
declare var_src="${1:-${BASH_SOURCE[1]}}"
|
||||
### Strip path, keep only the filename
|
||||
declare var_file_name="${var_src##*/}"
|
||||
declare var_file_name="${var_src##*/}"
|
||||
### Sanitize to valid var name.
|
||||
declare var_safe_name="${var_file_name//[^a-zA-Z0-9_]/_}"
|
||||
declare var_safe_name="${var_file_name//[^a-zA-Z0-9_]/_}"
|
||||
### Build guard-variable name.
|
||||
declare var_guard_var="_${var_safe_name}_LOADED"
|
||||
declare var_guard_var="_${var_safe_name}_LOADED"
|
||||
|
||||
### If already loaded, abort sourcing
|
||||
if [[ -n "${!var_guard_var:-}" ]]; then
|
||||
return 0
|
||||
return "${ERR_GUARD_SOURCE}"
|
||||
fi
|
||||
|
||||
### Mark as loaded (readonly + exported)
|
||||
@@ -41,5 +41,5 @@ guard_sourcing() {
|
||||
}
|
||||
### Prevents accidental 'unset -f'.
|
||||
# shellcheck disable=SC2034
|
||||
readonly -f guard_sourcing
|
||||
readonly -f guard_sourcing || return "${ERR_GUARD_SOURCE}"
|
||||
# vim: number et ts=2 sw=2 sts=2 ai tw=128 ft=sh
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
# SPDX-PackageName: CISS.debian.installer
|
||||
# SPDX-Security-Contact: security@coresecret.eu
|
||||
|
||||
guard_sourcing
|
||||
guard_sourcing || return "${ERR_GUARD_SOURCE}"
|
||||
|
||||
#######################################
|
||||
# Generic safe wrapper for external commands.
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
# SPDX-PackageName: CISS.debian.installer
|
||||
# SPDX-Security-Contact: security@coresecret.eu
|
||||
|
||||
guard_sourcing
|
||||
guard_sourcing || return "${ERR_GUARD_SOURCE}"
|
||||
|
||||
#######################################
|
||||
# Converts the value of a passed variable to lowercase.
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
# SPDX-PackageName: CISS.debian.installer
|
||||
# SPDX-Security-Contact: security@coresecret.eu
|
||||
|
||||
guard_sourcing
|
||||
guard_sourcing || return "${ERR_GUARD_SOURCE}"
|
||||
|
||||
#######################################
|
||||
# Function to log a module success message and change pwd to script root.
|
||||
|
||||
@@ -19,7 +19,7 @@ sourcing, which are frequent sources of bugs and side effects in modular Bash-ba
|
||||
## 2.1. Purpose and Functionality
|
||||
|
||||
- **0005_guard_sourcing.sh**
|
||||
Defines the `guard_sourcing()` function, which programmatically prevents a Bash library file from being sourced multiple
|
||||
Defines the `guard_sourcing || return "${ERR_GUARD_SOURCE}"()` function, which programmatically prevents a Bash library file from being sourced multiple
|
||||
times. It constructs a uniquely scoped read-only environment variable guard to mark the sourced state.
|
||||
|
||||
- **0006_source_guard.sh**
|
||||
@@ -36,7 +36,7 @@ sourcing, which are frequent sources of bugs and side effects in modular Bash-ba
|
||||
- Bash version ≥ 5.1
|
||||
- Active error trapping via `trap 'trap_err' ERR`
|
||||
- `do_log()` must be declared elsewhere in the global context
|
||||
- All libraries must be sourced via `source_guard()` and invoke `guard_sourcing` on top
|
||||
- All libraries must be sourced via `source_guard()` and invoke `guard_sourcing || return "${ERR_GUARD_SOURCE}"` on top
|
||||
|
||||
## 2.3. Dependencies
|
||||
|
||||
|
||||
Reference in New Issue
Block a user