Fix bugs on empty input

This commit is contained in:
dabruh 2024-04-18 20:37:00 +02:00
parent c7145e8b4a
commit 4973b9f4d2
1 changed files with 12 additions and 8 deletions

View File

@ -16,6 +16,7 @@ export CHROME_EXECUTABLE=chromium # For Flutter
export XDG_CACHE_HOME="${XDG_CACHE_HOME:-$HOME/.cache}"
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-$HOME/.config}"
export XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
export XDG_DATA_DIRS="${XDG_DATA_DIRS:-/usr/share}"
# Clean home:
export ANDROID_HOME="$XDG_DATA_HOME/Android/Sdk"
@ -64,26 +65,27 @@ rm_env_path_entry() {
del_entry="${2:?Missing entry to delete}"
new_entries=""
echo "$src_entries" | $_tr ':' '\n' | while read -r entry; do
for entry in $(echo "$src_entries" | $_tr ':' ' '); do
if [ "$entry" != "$del_entry" ]; then
new_entries="$new_entries:$entry"
fi
done
trim "$new_entries" ":"
[ -n "$new_entries" ] && trim "$new_entries" ":"
echo
}
modify_env_path() {
mode="${1:?Missing mode}"
entries="${2:?Missing entries}"
entries="$2"
change="${3:?Missing change}"
entries="$(trim "$entries" ":")"
[ -n "$entries" ] && entries="$(trim "$entries" ":")"
case "$mode" in
prepend)
temp_entries="" # Will hold the new entries in the correct order
echo "$change" | $_tr ':' '\n' | while read -r entry; do
for entry in $(echo "$change" | $_tr ':' ' '); do
[ -z "$entry" ] && continue # Skip empty entries
[ -d "$entry" ] || continue # Skip non-existent directories
temp_entries="$temp_entries:$entry"
@ -94,15 +96,16 @@ modify_env_path() {
entries="${temp_entries#:}:$entries"
;;
append)
echo "$change" | $_tr ':' '\n' | while read -r entry; do
for entry in $(echo "$change" | $_tr ':' ' '); do
[ -z "$entry" ] && continue # Skip empty entries
[ -d "$entry" ] || continue # Skip non-existent directories
entries="$entries:$entry"
done
;;
delete)
echo "$change" | $_tr ':' '\n' | while read -r entry; do
for entry in $(echo "$change" | $_tr ':' ' '); do
[ -z "$entry" ] && continue # Skip empty entries
[ -z "$entries" ] && break # nothing left to remove
entries=$(rm_env_path_entry "$entries" "$entry")
done
;;
@ -112,7 +115,8 @@ modify_env_path() {
;;
esac
trim "$entries" ":"
[ -n "$entries" ] && trim "$entries" ":"
echo
}
prepend_paths="$HOME/.local/bin:$HOME/.cargo/bin:$FLUTTER_HOME/bin:$GOPATH/bin:$KREW_ROOT/bin"