mirror of
https://github.com/ivuorinen/dotfiles.git
synced 2026-02-05 21:49:21 +00:00
refactor(path): consolidate x-path-{append,prepend,remove} as thin wrappers
Add source guard to x-path so its functions can be loaded without executing the main logic. Rewrite standalone path scripts to source x-path and call the appropriate function directly, eliminating code duplication while preserving source-ability for shell integration.
This commit is contained in:
@@ -227,6 +227,9 @@ do_check()
|
||||
fi
|
||||
}
|
||||
|
||||
# If sourced, provide functions without executing main logic
|
||||
(return 0 2> /dev/null) && return
|
||||
|
||||
#######################################
|
||||
# Main routine: Parse subcommand and arguments, normalize PATH,
|
||||
# and dispatch to the appropriate functionality.
|
||||
|
||||
@@ -1,44 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Optimized script to append directories to PATH.
|
||||
# For each given directory, it removes all duplicate occurrences from PATH
|
||||
# and then appends it if the directory exists.
|
||||
# Thin wrapper — delegates to x-path append.
|
||||
# Can be sourced (PATH changes propagate) or executed.
|
||||
#
|
||||
# Usage: x-path-append <directory1> [<directory2> ...]
|
||||
#
|
||||
# Enable verbose output by setting the environment variable VERBOSE=1.
|
||||
#
|
||||
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2024
|
||||
# License: MIT
|
||||
|
||||
VERBOSE="${VERBOSE:-0}"
|
||||
|
||||
# Ensure that at least one directory is provided.
|
||||
[ "$#" -lt 1 ] && {
|
||||
echo "Usage: $0 <directory> [<directory> ...]"
|
||||
exit 1
|
||||
}
|
||||
# shellcheck source=./x-path
|
||||
. "$(dirname "${BASH_SOURCE[0]:-$0}")/x-path"
|
||||
|
||||
for dir in "$@"; do
|
||||
# Check if the specified directory exists.
|
||||
if [ ! -d "$dir" ]; then
|
||||
[ "$VERBOSE" -eq 1 ] && echo "(?) Directory '$dir' does not exist. Skipping."
|
||||
continue
|
||||
fi
|
||||
|
||||
# Remove all duplicate occurrences of the directory from PATH.
|
||||
case ":$PATH:" in
|
||||
*":$dir:"*)
|
||||
PATH=":${PATH}:"
|
||||
PATH="${PATH//:$dir:/:}"
|
||||
PATH="${PATH#:}"
|
||||
PATH="${PATH%:}"
|
||||
[ "$VERBOSE" -eq 1 ] && echo "Removed previous occurrences of '$dir' from PATH."
|
||||
;;
|
||||
*) ;;
|
||||
esac
|
||||
|
||||
# Append the directory to PATH.
|
||||
export PATH="${PATH:+$PATH:}$dir"
|
||||
[ "$VERBOSE" -eq 1 ] && echo "Appended '$dir' to PATH."
|
||||
done
|
||||
normalize_path_var
|
||||
do_append "$@"
|
||||
|
||||
@@ -1,50 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Optimized script to batch prepend directories to PATH.
|
||||
# For each given directory, it removes all duplicate occurrences from PATH
|
||||
# and then prepends it. Directories that do not exist are skipped.
|
||||
# Thin wrapper — delegates to x-path prepend.
|
||||
# Can be sourced (PATH changes propagate) or executed.
|
||||
#
|
||||
# Usage: x-path-prepend <directory1> [<directory2> ...]
|
||||
#
|
||||
# Enable verbose output by setting the environment variable VERBOSE=1.
|
||||
#
|
||||
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2024
|
||||
# License: MIT
|
||||
|
||||
VERBOSE="${VERBOSE:-0}"
|
||||
|
||||
# Ensure that at least one argument is provided.
|
||||
[ "$#" -lt 1 ] && {
|
||||
echo "Usage: $0 <directory> [<directory> ...]"
|
||||
exit 1
|
||||
}
|
||||
# shellcheck source=./x-path
|
||||
. "$(dirname "${BASH_SOURCE[0]:-$0}")/x-path"
|
||||
|
||||
# Save the arguments in an array.
|
||||
dirs=("$@")
|
||||
|
||||
# Process the directories in reverse order so that the first argument ends up leftmost in PATH.
|
||||
for ((idx = ${#dirs[@]} - 1; idx >= 0; idx--)); do
|
||||
dir="${dirs[idx]}"
|
||||
|
||||
# Check if the specified directory exists.
|
||||
if [ ! -d "$dir" ]; then
|
||||
[ "$VERBOSE" -eq 1 ] && echo "(?) Directory '$dir' does not exist. Skipping."
|
||||
continue
|
||||
fi
|
||||
|
||||
# Remove all duplicate occurrences of the directory from PATH using built-in string operations.
|
||||
case ":$PATH:" in
|
||||
*":$dir:"*)
|
||||
PATH=":${PATH}:"
|
||||
PATH="${PATH//:$dir:/:}"
|
||||
PATH="${PATH#:}"
|
||||
PATH="${PATH%:}"
|
||||
[ "$VERBOSE" -eq 1 ] && echo "Removed duplicate occurrences of '$dir' from PATH."
|
||||
;;
|
||||
*) ;;
|
||||
esac
|
||||
|
||||
# Prepend the directory to PATH.
|
||||
export PATH="$dir${PATH:+":$PATH"}"
|
||||
[ "$VERBOSE" -eq 1 ] && echo "Prepended '$dir' to PATH."
|
||||
done
|
||||
normalize_path_var
|
||||
do_prepend "$@"
|
||||
|
||||
@@ -1,41 +1,17 @@
|
||||
#!/usr/bin/env bash
|
||||
#
|
||||
# Optimized script to remove directories from PATH.
|
||||
# For each specified directory, all occurrences are removed from PATH.
|
||||
# Thin wrapper — delegates to x-path remove.
|
||||
# Can be sourced (PATH changes propagate) or executed.
|
||||
#
|
||||
# Usage: x-path-remove <directory1> [<directory2> ...]
|
||||
#
|
||||
# Enable verbose output by setting the environment variable VERBOSE=1.
|
||||
#
|
||||
# Author: Ismo Vuorinen <https://github.com/ivuorinen> 2024
|
||||
# License: MIT
|
||||
|
||||
VERBOSE="${VERBOSE:-0}"
|
||||
|
||||
# Ensure that at least one directory is provided.
|
||||
[ "$#" -lt 1 ] && {
|
||||
echo "Usage: $0 <directory> [<directory> ...]"
|
||||
exit 1
|
||||
}
|
||||
# shellcheck source=./x-path
|
||||
. "$(dirname "${BASH_SOURCE[0]:-$0}")/x-path"
|
||||
|
||||
for dir in "$@"; do
|
||||
# Remove trailing slash if present, unless the directory is "/"
|
||||
[ "$dir" != "/" ] && dir="${dir%/}"
|
||||
|
||||
# Check if the directory is present in PATH.
|
||||
case ":$PATH:" in
|
||||
*":$dir:"*)
|
||||
# Remove all occurrences of the directory from PATH using parameter expansion.
|
||||
PATH=":${PATH}:"
|
||||
PATH="${PATH//:$dir:/:}"
|
||||
PATH="${PATH#:}"
|
||||
PATH="${PATH%:}"
|
||||
[ "$VERBOSE" -eq 1 ] && echo "Removed '$dir' from PATH."
|
||||
;;
|
||||
*)
|
||||
[ "$VERBOSE" -eq 1 ] && echo "(?) '$dir' is not in PATH."
|
||||
;;
|
||||
esac
|
||||
done
|
||||
|
||||
export PATH
|
||||
normalize_path_var
|
||||
do_remove "$@"
|
||||
|
||||
Reference in New Issue
Block a user