Fix macOS compatibility: replace Bash 4 associative array with string tracking
macOS ships with Bash 3.2 which doesn't support 'declare -A' (associative arrays). Replace the 'seen' associative array with a simple delimited string 'seen_keys' to track which environment variables have been processed. The logic remains identical: - String concatenation instead of array assignment - Substring match instead of array key lookup Tested on macOS with Bash 3.2.57 and the upsert_env function works correctly.
This commit is contained in:
parent
9688454a30
commit
2cdff9d3c1
@ -124,7 +124,7 @@ upsert_env() {
|
|||||||
local -a keys=("$@")
|
local -a keys=("$@")
|
||||||
local tmp
|
local tmp
|
||||||
tmp="$(mktemp)"
|
tmp="$(mktemp)"
|
||||||
declare -A seen=()
|
local seen_keys=""
|
||||||
|
|
||||||
if [[ -f "$file" ]]; then
|
if [[ -f "$file" ]]; then
|
||||||
while IFS= read -r line || [[ -n "$line" ]]; do
|
while IFS= read -r line || [[ -n "$line" ]]; do
|
||||||
@ -133,7 +133,7 @@ upsert_env() {
|
|||||||
for k in "${keys[@]}"; do
|
for k in "${keys[@]}"; do
|
||||||
if [[ "$key" == "$k" ]]; then
|
if [[ "$key" == "$k" ]]; then
|
||||||
printf '%s=%s\n' "$k" "${!k-}" >>"$tmp"
|
printf '%s=%s\n' "$k" "${!k-}" >>"$tmp"
|
||||||
seen["$k"]=1
|
seen_keys="${seen_keys}${k};"
|
||||||
replaced=true
|
replaced=true
|
||||||
break
|
break
|
||||||
fi
|
fi
|
||||||
@ -145,7 +145,7 @@ upsert_env() {
|
|||||||
fi
|
fi
|
||||||
|
|
||||||
for k in "${keys[@]}"; do
|
for k in "${keys[@]}"; do
|
||||||
if [[ -z "${seen[$k]:-}" ]]; then
|
if [[ "$seen_keys" != *"${k};"* ]]; then
|
||||||
printf '%s=%s\n' "$k" "${!k-}" >>"$tmp"
|
printf '%s=%s\n' "$k" "${!k-}" >>"$tmp"
|
||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user