From fc8db1f5b5e6bb77701938e61be9e2d6c842488e Mon Sep 17 00:00:00 2001 From: Ismo Vuorinen Date: Thu, 5 Feb 2026 20:51:52 +0200 Subject: [PATCH] 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. --- local/bin/x-path | 3 +++ local/bin/x-path-append | 39 ++++++---------------------------- local/bin/x-path-prepend | 45 ++++++---------------------------------- local/bin/x-path-remove | 36 ++++++-------------------------- 4 files changed, 21 insertions(+), 102 deletions(-) diff --git a/local/bin/x-path b/local/bin/x-path index 9b6bb6e..f161b7e 100755 --- a/local/bin/x-path +++ b/local/bin/x-path @@ -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. diff --git a/local/bin/x-path-append b/local/bin/x-path-append index c5d6570..4ce1e1a 100755 --- a/local/bin/x-path-append +++ b/local/bin/x-path-append @@ -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 [ ...] # -# Enable verbose output by setting the environment variable VERBOSE=1. -# # Author: Ismo Vuorinen 2024 # License: MIT VERBOSE="${VERBOSE:-0}" -# Ensure that at least one directory is provided. -[ "$#" -lt 1 ] && { - echo "Usage: $0 [ ...]" - 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 "$@" diff --git a/local/bin/x-path-prepend b/local/bin/x-path-prepend index 3dd0b4f..ccf92bd 100755 --- a/local/bin/x-path-prepend +++ b/local/bin/x-path-prepend @@ -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 [ ...] # -# Enable verbose output by setting the environment variable VERBOSE=1. -# # Author: Ismo Vuorinen 2024 # License: MIT VERBOSE="${VERBOSE:-0}" -# Ensure that at least one argument is provided. -[ "$#" -lt 1 ] && { - echo "Usage: $0 [ ...]" - 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 "$@" diff --git a/local/bin/x-path-remove b/local/bin/x-path-remove index fca0390..242d606 100755 --- a/local/bin/x-path-remove +++ b/local/bin/x-path-remove @@ -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 [ ...] # -# Enable verbose output by setting the environment variable VERBOSE=1. -# # Author: Ismo Vuorinen 2024 # License: MIT VERBOSE="${VERBOSE:-0}" -# Ensure that at least one directory is provided. -[ "$#" -lt 1 ] && { - echo "Usage: $0 [ ...]" - 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 "$@"