refactor: extend source_file with optional export mode for ~/.env
Add 'export' flag to source_file that wraps the source call in set -a/+a, auto-exporting all variables to child processes. Use it for ~/.env instead of a separate inline block. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -28,12 +28,16 @@ add_to_path() {
|
||||
}
|
||||
|
||||
# Source a file with existence and readability check (zsh-friendly)
|
||||
# Usage: source_file /path/to/file
|
||||
# Usage: source_file /path/to/file [export]
|
||||
# Pass 'export' as second arg to auto-export all variables via set -a/+a.
|
||||
source_file() {
|
||||
local file="$1"
|
||||
local export_vars="${2:-}"
|
||||
|
||||
if [[ -f "$file" && -r "$file" ]]; then
|
||||
[[ "$export_vars" == "export" ]] && set -a
|
||||
source "$file"
|
||||
[[ "$export_vars" == "export" ]] && set +a
|
||||
fi
|
||||
}
|
||||
|
||||
@@ -83,19 +87,6 @@ for expr in "${eval_expressions[@]}"; do
|
||||
done
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# ENVIRONMENT VARIABLES
|
||||
# =============================================================================
|
||||
|
||||
# Source ~/.env with set -a so every variable is automatically exported
|
||||
# and inherited by child processes (scripts, subshells, etc.).
|
||||
if [[ -f "$HOME/.env" && -r "$HOME/.env" ]]; then
|
||||
set -a
|
||||
source "$HOME/.env"
|
||||
set +a
|
||||
fi
|
||||
|
||||
|
||||
# =============================================================================
|
||||
# CONFIGURATION FILES TO SOURCE
|
||||
# =============================================================================
|
||||
@@ -108,6 +99,9 @@ source_files=(
|
||||
"$HOME/.p10k.zsh"
|
||||
)
|
||||
|
||||
# Source ~/.env with auto-export so variables reach child processes
|
||||
source_file "$HOME/.env" export
|
||||
|
||||
# Source zsh-compatible files (skip if missing, no warnings)
|
||||
for config_file in "${source_files[@]}"; do
|
||||
source_file "$config_file"
|
||||
|
||||
Reference in New Issue
Block a user