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

refactor: Use semantic display_info and display_error #1654

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions bin/asdf
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ asdf_cmd() {
local ASDF_CMD_FILE args_offset

if [ "shell" = "$1" ]; then
printf "Shell integration is not enabled. Please ensure you source asdf in your shell setup." >&2
display_error "Shell integration is not enabled. Please ensure you source asdf in your shell setup."
exit 1
fi

Expand Down Expand Up @@ -96,12 +96,12 @@ asdf_cmd() {
# In those cases, the path to that command is always an absolute path. However, this codepath can also be activated if a user accidentally
# marks files in ./lib/commands/* as executable. This code detects when that happens and prints a useful warning message.
if [[ "$ASDF_CMD_FILE" == ./lib/commands/* ]]; then
printf '%s\n' "----------"
printf '%s\n' "asdf: Warning: You are executing an asdf command from \$ASDF_DIR, but we detected that some files have been"
printf '%s\n' " erroneously marked as executable. All files under '$ASDF_DIR/lib/commands' must NOT be marked"
printf '%s\n' " as executable. Otherwise, asdf will not be able to source its core files"
printf '%s\n' "----------"
fi >&2
display_error "----------"
display_error "asdf: Warning: You are executing an asdf command from \$ASDF_DIR, but we detected that some files have been"
display_error " erroneously marked as executable. All files under '$ASDF_DIR/lib/commands' must NOT be marked"
display_error " as executable. Otherwise, asdf will not be able to source its core files"
display_error "----------"
fi

exec "$ASDF_CMD_FILE" "${@:${args_offset}}"
elif [ -f "$ASDF_CMD_FILE" ]; then
Expand All @@ -111,7 +111,7 @@ asdf_cmd() {
else
local asdf_cmd_dir
asdf_cmd_dir="$(asdf_dir)/lib/commands"
printf "%s\n" "Unknown command: \`asdf ${*}\`" >&2
display_error "Unknown command: \`asdf ${*}\`"
# shellcheck source=lib/commands/command-help.bash
. "$asdf_cmd_dir/command-help.bash" >&2
return 127
Expand Down
12 changes: 6 additions & 6 deletions lib/commands/command-current.bash
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ plugin_current_command() {

if [ -n "$version_not_installed" ]; then
description="Not installed. Run \"asdf install $plugin $version\""
printf "$terminal_format" "$plugin" "$version" "$description" 1>&2
display_error "$terminal_format" "$plugin" "$version" "$description"
return 1
elif [ -z "$full_version" ]; then
description="No version is set. Run \"asdf <global|shell|local> $plugin <version>\""
printf "$terminal_format" "$plugin" "______" "$description" 1>&2
display_error "$terminal_format" "$plugin" "______" "$description"
return 126
else
description="$version_file_path"
printf "$terminal_format" "$plugin" "$full_version" "$description"
display_info "$terminal_format" "$plugin" "$full_version" "$description"
fi
}

Expand All @@ -48,7 +48,7 @@ current_command() {
local exit_status=0
local plugin

# printf "$terminal_format" "PLUGIN" "VERSION" "SET BY CONFIG" # disable this until we release headings across the board
# printf "$terminal_format" "PLUGIN" "VERSION" "SET BY CONFIG" # TODO: disable this until we release headings across the board
if [ $# -eq 0 ]; then
# shellcheck disable=SC2119
for plugin in $(plugin_list_command); do
Expand All @@ -75,8 +75,8 @@ check_for_deprecated_plugin() {
local new_script="${plugin_path}/bin/list-legacy-filenames"

if [ "$legacy_config" = "yes" ] && [ -f "$deprecated_script" ] && [ ! -f "$new_script" ]; then
printf "Heads up! It looks like your %s plugin is out of date. You can update it with:\n\n" "$plugin_name"
printf " asdf plugin-update %s\n\n" "$plugin_name"
display_info "Heads up! It looks like your %s plugin is out of date. You can update it with:\n\n" "$plugin_name"
display_info " asdf plugin-update %s\n\n" "$plugin_name"
fi
}

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-exec.bash
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ shim_exec_command() {
local executable_path="$3"

if [ ! -x "$executable_path" ]; then
printf "No %s executable found for %s %s\n" "$shim_name" "$plugin_name" "$version" >&2
display_error "No %s executable found for %s %s\n" "$shim_name" "$plugin_name" "$version"
exit 2
fi

Expand Down
4 changes: 2 additions & 2 deletions lib/commands/command-export-shell-version.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ shell_command() {
shift

if [ "$#" -lt "2" ]; then
printf "Usage: asdf shell <name> {<version>|--unset}\n" >&2
display_error "Usage: asdf shell <name> {<version>|--unset}\n"
printf "false\n"
exit 1
fi
Expand Down Expand Up @@ -44,7 +44,7 @@ shell_command() {
version=$(latest_command "$plugin")
fi
if ! (check_if_version_exists "$plugin" "$version"); then
version_not_installed_text "$plugin" "$version" 1>&2
display_version_not_installed "$plugin" "$version" >&2
printf "false\n"
exit 1
fi
Expand Down
4 changes: 2 additions & 2 deletions lib/commands/command-help.bash
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ help_command() {
(print_plugin_help "$plugin_path")
fi
else
printf "No documentation for plugin %s\n" "$plugin_name" >&2
display_error "No documentation for plugin %s\n" "$plugin_name"
exit 1
fi
else
printf "No plugin named %s\n" "$plugin_name" >&2
display_error "No plugin named %s\n" "$plugin_name"
exit 1
fi
else
Expand Down
6 changes: 3 additions & 3 deletions lib/commands/command-update.bash
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ update_command() {
cd "$(asdf_dir)" || exit 1

if [ -f asdf_updates_disabled ] || ! git rev-parse --is-inside-work-tree &>/dev/null; then
printf "Update command disabled. Please use the package manager that you used to install asdf to upgrade asdf.\n"
display_info "Update command disabled. Please use the package manager that you used to install asdf to upgrade asdf."
exit 42
else
do_update "$update_to_head"
Expand All @@ -23,7 +23,7 @@ do_update() {
git fetch origin master
git checkout master
git reset --hard origin/master
printf "Updated asdf to latest on the master branch\n"
display_info "Updated asdf to latest on the master branch"
else
# Update to latest release
git fetch origin --tags || exit 1
Expand All @@ -38,7 +38,7 @@ do_update() {

# Update
git checkout "$tag" || exit 1
printf "Updated asdf to release %s\n" "$tag"
display_info "Updated asdf to release %s\n" "$tag"
fi
}

Expand Down
2 changes: 1 addition & 1 deletion lib/commands/command-which.bash
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ which_command() {
local executable_path="$3"

if [ ! -x "$executable_path" ]; then
printf "No %s executable found for %s %s\n" "$shim_name" "$plugin_name" "$version" >&2
display_error "No %s executable found for %s %s\n" "$shim_name" "$plugin_name" "$version"
exit 1
fi

Expand Down
14 changes: 7 additions & 7 deletions lib/functions/installs.bash
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ install_one_local_tool() {
install_tool_version "$plugin_name" "$plugin_version"
done
else
printf "No versions specified for %s in config files or environment\n" "$plugin_name"
display_info "No versions specified for %s in config files or environment\n" "$plugin_name"
exit 1
fi
}
Expand Down Expand Up @@ -97,7 +97,7 @@ install_local_tool_versions() {
fi

if [ -z "$plugins_installed" ]; then
printf "Install plugins first to be able to install tools\n"
display_info "Install plugins first to be able to install tools"
exit 1
fi

Expand All @@ -107,7 +107,7 @@ install_local_tool_versions() {
tools_file=$(strip_tool_version_comments "$tool_versions_path" | cut -d ' ' -f 1)
for plugin_name in $tools_file; do
if ! printf '%s\n' "${plugins_installed[@]}" | grep -q "^$plugin_name\$"; then
printf "%s plugin is not installed\n" "$plugin_name"
display_info "%s plugin is not installed\n" "$plugin_name"
some_plugin_not_installed='yes'
fi
done
Expand All @@ -134,9 +134,9 @@ install_local_tool_versions() {
fi

if [ -z "$some_tools_installed" ]; then
printf "Either specify a tool & version in the command\n"
printf "OR add .tool-versions file in this directory\n"
printf "or in a parent directory\n"
display_info "Either specify a tool & version in the command"
display_info "OR add .tool-versions file in this directory"
display_info "or in a parent directory"
exit 1
fi
}
Expand Down Expand Up @@ -192,7 +192,7 @@ install_tool_version() {
trap 'handle_cancel $install_path' INT

if [ -d "$install_path" ]; then
printf "%s %s is already installed\n" "$plugin_name" "$full_version"
display_info "%s %s is already installed\n" "$plugin_name" "$full_version"
else

if [ -f "${plugin_path}/bin/download" ]; then
Expand Down
6 changes: 3 additions & 3 deletions lib/functions/plugins.bash
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ plugin_add_command() {
mkdir -p "$(asdf_data_dir)/plugins"

if [ -d "$plugin_path" ]; then
printf '%s\n' "Plugin named $plugin_name already added"
display_info "Plugin named $plugin_name already added"
exit 0
else
asdf_run_hook "pre_asdf_plugin_add" "$plugin_name"
Expand Down Expand Up @@ -140,11 +140,11 @@ update_plugin() {
local prev_ref=
local post_ref=
{
printf "Location of %s plugin: %s\n" "$plugin_name" "$plugin_path"
display_info "Location of %s plugin: %s\n" "$plugin_name" "$plugin_path"
asdf_run_hook "pre_asdf_plugin_update" "$plugin_name"
asdf_run_hook "pre_asdf_plugin_update_${plugin_name}"

printf "Updating %s to %s\n" "$plugin_name" "$gitref"
display_info "Updating %s to %s\n" "$plugin_name" "$gitref"

git "${common_git_options[@]}" fetch --prune --update-head-ok origin "$gitref:$gitref"
prev_ref=$(git "${common_git_options[@]}" rev-parse --short HEAD)
Expand Down
12 changes: 6 additions & 6 deletions lib/functions/versions.bash
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ version_command() {
fi

if ! (check_if_version_exists "$plugin_name" "$version"); then
version_not_installed_text "$plugin_name" "$version" 1>&2
display_version_not_installed "$plugin_name" "$version" >&2
exit 1
fi

Expand Down Expand Up @@ -99,9 +99,9 @@ list_all_command() {

if [[ $return_code -ne 0 ]]; then
# Printing all output to allow plugin to handle error formatting
printf "Plugin %s's list-all callback script failed with output:\n" "${plugin_name}" >&2
printf "%s\n" "$(cat "$std_err_file")" >&2
printf "%s\n" "$(cat "$std_out_file")" >&2
display_error "Plugin %s's list-all callback script failed with output:\n" "${plugin_name}"
display_error "%s\n" "$(cat "$std_err_file")"
display_error "%s\n" "$(cat "$std_out_file")"
rm "$std_out_file" "$std_err_file"
exit 1
fi
Expand Down Expand Up @@ -151,7 +151,7 @@ latest_command() {
versions=$("${plugin_path}"/bin/latest-stable "$query")
if [ -z "${versions}" ]; then
# this branch requires this print to mimic the error from the list-all branch
printf "No compatible versions available (%s %s)\n" "$plugin_name" "$query" >&2
display_error "No compatible versions available (%s %s)\n" "$plugin_name" "$query"
exit 1
fi
else
Expand Down Expand Up @@ -208,7 +208,7 @@ latest_all() {
printf "%s\t%s\t%s\n" "$plugin_name" "$version" "$installed_status"
done
else
printf "%s\n" 'No plugins installed'
display_info 'No plugins installed'
fi
exit 0
}
Expand Down
62 changes: 38 additions & 24 deletions lib/utils.bash
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,6 @@ check_if_version_exists() {
fi
}

version_not_installed_text() {
local plugin_name=$1
local version=$2

printf "version %s is not installed for %s\n" "$version" "$plugin_name"
}

get_plugin_path() {
if [ -n "$1" ]; then
printf "%s\n" "$(asdf_data_dir)/plugins/$1"
Expand All @@ -153,7 +146,33 @@ get_plugin_path() {
}

display_error() {
printf "%s\n" "$1" >&2
if (($# == 1)); then
set -- '%s\n' "$@"
fi

# shellcheck disable=SC2059
printf "$@" >&2 # checkstyle-ignore
}

display_info() {
if (($# == 1)); then
set -- '%s\n' "$@"
fi

# shellcheck disable=SC2059
printf "$@"
}

display_no_version_set() {
local plugin_name=$1
printf "No version is set for %s; please run \`asdf <global | shell | local> %s <version>\`\n" "$plugin_name" "$plugin_name"
}

display_version_not_installed() {
local plugin_name=$1
local version=$2

printf "version %s is not installed for %s\n" "$version" "$plugin_name"
}

get_version_in_dir() {
Expand Down Expand Up @@ -229,11 +248,6 @@ find_versions() {
fi
}

display_no_version_set() {
local plugin_name=$1
printf "No version is set for %s; please run \`asdf <global | shell | local> %s <version>\`\n" "$plugin_name" "$plugin_name"
}

get_version_from_env() {
local plugin_name=$1
local upcase_name
Expand Down Expand Up @@ -426,7 +440,7 @@ initialize_or_update_plugin_repository() {

disable_plugin_short_name_repo="$(get_asdf_config_value "disable_plugin_short_name_repository")"
if [ "yes" = "$disable_plugin_short_name_repo" ]; then
printf "Short-name plugin repository is disabled\n" >&2
display_error "Short-name plugin repository is disabled"
exit 1
fi

Expand Down Expand Up @@ -646,7 +660,7 @@ shim_plugin_versions() {
if [ -x "$shim_path" ]; then
grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | uniq
else
printf "asdf: unknown shim %s\n" "$executable_name"
display_info "asdf: unknown shim %s\n" "$executable_name"
return 1
fi
}
Expand All @@ -659,7 +673,7 @@ shim_plugins() {
if [ -x "$shim_path" ]; then
grep "# asdf-plugin: " "$shim_path" 2>/dev/null | sed -e "s/# asdf-plugin: //" | cut -d' ' -f 1 | uniq
else
printf "asdf: unknown shim %s\n" "$executable_name"
display_info "asdf: unknown shim %s\n" "$executable_name"
return 1
fi
}
Expand Down Expand Up @@ -761,7 +775,7 @@ with_shim_executable() {
local shim_exec="${2}"

if [ ! -f "$(asdf_data_dir)/shims/${shim_name}" ]; then
printf "%s %s %s\n" "unknown command:" "${shim_name}." "Perhaps you have to reshim?" >&2
display_error "%s %s %s\n" "unknown command:" "${shim_name}." "Perhaps you have to reshim?"
return 1
fi

Expand Down Expand Up @@ -820,15 +834,15 @@ with_shim_executable() {
done

if [ -n "${preset_plugin_versions[*]}" ]; then
printf "%s %s\n" "No preset version installed for command" "$shim_name"
printf "%s\n\n" "Please install a version by running one of the following:"
display_info "%s %s\n" "No preset version installed for command" "$shim_name"
display_info "%s\n\n" "Please install a version by running one of the following:"
for preset_plugin_version in "${preset_plugin_versions[@]}"; do
printf "%s %s\n" "asdf install" "$preset_plugin_version"
display_info "%s %s\n" "asdf install" "$preset_plugin_version"
done
printf "\n%s %s\n" "or add one of the following versions in your config file at" "$closest_tool_version"
display_info "\n%s %s\n" "or add one of the following versions in your config file at" "$closest_tool_version"
else
printf "%s %s\n" "No version is set for command" "$shim_name"
printf "%s %s\n" "Consider adding one of the following versions in your config file at" "$closest_tool_version"
display_info "%s %s\n" "No version is set for command" "$shim_name"
display_info "%s %s\n" "Consider adding one of the following versions in your config file at" "$closest_tool_version"
fi
shim_plugin_versions "${shim_name}"
) >&2
Expand Down Expand Up @@ -872,7 +886,7 @@ util_validate_no_carriage_returns() {
local file_path="$1"

if grep -qr $'\r' "$file_path"; then
printf '%s\n' "asdf: Warning: File $file_path contains carriage returns. Please remove them." >&2
display_error "asdf: Warning: File $file_path contains carriage returns. Please remove them."
fi
}

Expand Down
Loading