Skip to content
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

Merged
merged 2 commits into from Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 7 additions & 10 deletions lib/commands/command-help.bash
Expand Up @@ -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
Copy link
Member Author

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 and sed from this line and replace the sed command with a simpler basename command. I'm considering adding ls 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 like find, grep, and the occasional globbing.

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/##"
)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here. Was able to replace a ls and sed pipeline with just basename.

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
Expand Down
21 changes: 19 additions & 2 deletions lib/utils.bash
Expand Up @@ -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
Expand Down Expand Up @@ -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")

Expand Down Expand Up @@ -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"}"
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit more code now, but this is much more robust than sed because it can handle any character in the three input strings and will handle escaping for us.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this Bash specific?

Copy link
Member Author

Choose a reason for hiding this comment

The 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