From 4b93bc81aa982b72621cd09e71eeea71ee009185 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Mon, 15 Nov 2021 03:03:53 -0500 Subject: [PATCH] fix: Sed improvements (#1087) --- lib/commands/command-help.bash | 17 +++++++---------- lib/utils.bash | 21 +++++++++++++++++++-- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/lib/commands/command-help.bash b/lib/commands/command-help.bash index 535a4852..d9c5e20f 100644 --- a/lib/commands/command-help.bash +++ b/lib/commands/command-help.bash @@ -14,20 +14,17 @@ EOF } asdf_extension_cmds() { - local plugins_path ext_cmd_path ext_cmds plugin + local plugins_path plugin_path ext_cmd_path ext_cmds plugin plugins_path="$(get_plugin_path)" - # use find instead of ls -1 - # shellcheck disable=SC2012 - for plugin in $(ls -1 "$plugins_path" 2>/dev/null | sed "s#^$plugins_path/##"); do - ext_cmd_path="$plugins_path/$plugin/lib/commands" - ext_cmds=$( - ls -1 "$ext_cmd_path"/command*.bash 2>/dev/null | - sed "s#$ext_cmd_path/##" - ) + for plugin_path in "$plugins_path"/*; do + plugin="$(basename "$plugin_path")" + ext_cmd_path="$plugin_path/lib/commands" + ext_cmds="$(find "$ext_cmd_path" -name "command*.bash")" if [[ -n $ext_cmds ]]; then printf "\\nPLUGIN %s\\n" "$plugin" for ext_cmd in $ext_cmds; do - sed "s/-/ /g;s/.bash//;s/command-*/ asdf $plugin/;" <<<"$ext_cmd" + ext_cmd_name="$(basename "$ext_cmd")" + sed "s/-/ /g;s/.bash//;s/command-*/ asdf $plugin/;" <<<"$ext_cmd_name" done | sort fi done diff --git a/lib/utils.bash b/lib/utils.bash index 8e95f507..5f4af3ae 100644 --- a/lib/utils.bash +++ b/lib/utils.bash @@ -289,7 +289,7 @@ get_executable_path() { check_if_version_exists "$plugin_name" "$version" if [ "$version" = "system" ]; then - path=$(sed -e "s|$(asdf_data_dir)/shims||g; s|::|:|g" <<<"$PATH") + path=$(remove_path_from_path "$PATH" "$(asdf_data_dir)/shims") cmd=$(basename "$executable_path") cmd_path=$(PATH=$path command -v "$cmd" 2>&1) # shellcheck disable=SC2181 @@ -755,7 +755,7 @@ with_shim_executable() { run_within_env() { local path - path=$(sed -e "s|$(asdf_data_dir)/shims||g; s|::|:|g" <<<"$PATH") + path=$(remove_path_from_path "$PATH" "$(asdf_data_dir)/shims") executable_path=$(PATH=$path command -v "$shim_name") @@ -806,3 +806,20 @@ with_shim_executable() { return 126 } + +substitute() { + # Use Bash substituion rather than sed as it will handle escaping of all + # strings for us. + local input=$1 + local find_str=$2 + local replace=$3 + printf "%s" "${input//"$find_str"/"$replace"}" +} + +remove_path_from_path() { + # A helper function for removing an arbitrary path from the PATH variable. + # Output is a new string suitable for assignment to PATH + local PATH=$1 + local path=$2 + substitute "$PATH" "$path" "" | sed -e "s|::|:|g" +}