New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Sed improvements #1087
fix: Sed improvements #1087
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/##" | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same thing here. Was able to replace a |
||
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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"}" | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bit more code now, but this is much more robust than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this Bash specific? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I believe so. But this is a Bash file and should only be executed by Bash. |
||
|
||
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" | ||
} | ||
jthegedus marked this conversation as resolved.
Show resolved
Hide resolved
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was able to remove both
ls
andsed
from this line and replace thesed
command with a simplerbasename
command. I'm considering addingls
to the banned commands list as I see it used in some anti-patterns in our codebase. To me it seems like listing files is done better with commands likefind
,grep
, and the occasional globbing.