From 1c8648dfe346fb2ee38e97d4d6f70ec61a1395d4 Mon Sep 17 00:00:00 2001 From: Trevor Brown Date: Tue, 17 May 2022 09:30:06 -0400 Subject: [PATCH] Don't allow unset variables in new Bash files --- lib/commands/command-shim-versions.bash | 2 +- lib/commands/command-uninstall.bash | 12 ++++++------ lib/commands/command-update.bash | 4 ++-- lib/commands/command-where.bash | 4 ++-- lib/commands/command-which.bash | 1 + lib/commands/reshim.bash | 24 ++++++++++++------------ lib/functions/installs.bash | 16 ++++++++-------- lib/functions/plugins.bash | 14 +++++++------- lib/functions/versions.bash | 16 ++++++++-------- 9 files changed, 47 insertions(+), 46 deletions(-) diff --git a/lib/commands/command-shim-versions.bash b/lib/commands/command-shim-versions.bash index ee73cf80..65b2c405 100644 --- a/lib/commands/command-shim-versions.bash +++ b/lib/commands/command-shim-versions.bash @@ -2,7 +2,7 @@ set -o nounset shim_versions_command() { - local shim_name=${1:-} + local shim_name="${1:-}" shim_plugin_versions "$shim_name" } diff --git a/lib/commands/command-uninstall.bash b/lib/commands/command-uninstall.bash index 75c0e565..6424eea2 100644 --- a/lib/commands/command-uninstall.bash +++ b/lib/commands/command-uninstall.bash @@ -5,10 +5,10 @@ set -o nounset . "$(dirname "$ASDF_CMD_FILE")/reshim.bash" uninstall_command() { - local plugin_name=${1:-} - local full_version=$2 + local plugin_name="${1:-}" + local full_version="${2:-}" local plugin_path - plugin_path=$(get_plugin_path "$plugin_name") + plugin_path="$(get_plugin_path "$plugin_name")" check_if_plugin_exists "$plugin_name" @@ -22,7 +22,7 @@ uninstall_command() { fi local install_path - install_path=$(get_install_path "$plugin_name" "$install_type" "$version") + install_path="$(get_install_path "$plugin_name" "$install_type" "$version")" if [ ! -d "$install_path" ]; then display_error "No such version" @@ -47,8 +47,8 @@ uninstall_command() { } remove_shims_for_version() { - local plugin_name=${1:-} - local full_version=$2 + local plugin_name="${1:-}" + local full_version="${2:-}" for shim_path in $(plugin_shims "$plugin_name" "$full_version"); do remove_shim_for_version "$plugin_name" "$full_version" "$shim_path" done diff --git a/lib/commands/command-update.bash b/lib/commands/command-update.bash index 005e100d..58190a03 100644 --- a/lib/commands/command-update.bash +++ b/lib/commands/command-update.bash @@ -2,7 +2,7 @@ set -o nounset update_command() { - local update_to_head=${1:-} + local update_to_head="${1:-}" ( cd "$(asdf_dir)" || exit 1 @@ -17,7 +17,7 @@ update_command() { } do_update() { - local update_to_head=${1:-} + local update_to_head="${1:-}" if [ "$update_to_head" = "--head" ]; then # Update to latest on the master branch diff --git a/lib/commands/command-where.bash b/lib/commands/command-where.bash index d472e0c6..8d831c56 100644 --- a/lib/commands/command-where.bash +++ b/lib/commands/command-where.bash @@ -2,8 +2,8 @@ set -o nounset where_command() { - local plugin_name=${1:-} - local full_version=${2:-} + local plugin_name="${1:-}" + local full_version="${2:-}" check_if_plugin_exists "$plugin_name" local version diff --git a/lib/commands/command-which.bash b/lib/commands/command-which.bash index 9f20e4d2..3f6a05a6 100644 --- a/lib/commands/command-which.bash +++ b/lib/commands/command-which.bash @@ -1,4 +1,5 @@ # -*- sh -*- + set -o nounset which_command() { diff --git a/lib/commands/reshim.bash b/lib/commands/reshim.bash index 294d6124..2dbe3e03 100644 --- a/lib/commands/reshim.bash +++ b/lib/commands/reshim.bash @@ -27,8 +27,8 @@ remove_shim_for_version() { } reshim_command() { - local plugin_name=$1 - local full_version=$2 + local plugin_name="${1:-}" + local full_version="${2:-}" if [ -z "$plugin_name" ]; then local plugins_path @@ -76,9 +76,9 @@ ensure_shims_dir() { } write_shim_script() { - local plugin_name=$1 - local version=$2 - local executable_path=$3 + local plugin_name="${1:-}" + local version="${2:-}" + local executable_path="${3:-}" if ! is_executable "$executable_path"; then return 0 @@ -107,8 +107,8 @@ EOF } generate_shim_for_executable() { - local plugin_name=$1 - local executable=$2 + local plugin_name="${1:-}" + local executable="${2:-}" check_if_plugin_exists "$plugin_name" @@ -124,8 +124,8 @@ generate_shim_for_executable() { } generate_shims_for_version() { - local plugin_name=$1 - local full_version=$2 + local plugin_name="${1:-}" + local full_version="${2:-}" local all_executable_paths IFS=$'\n' read -rd '' -a all_executable_paths <<<"$(plugin_executables "$plugin_name" "$full_version")" for executable_path in "${all_executable_paths[@]}"; do @@ -134,8 +134,8 @@ generate_shims_for_version() { } remove_obsolete_shims() { - local plugin_name=$1 - local full_version=$2 + local plugin_name="${1:-}" + local full_version="${2:-}" local shims shims=$(plugin_shims "$plugin_name" "$full_version" | xargs -IX basename X | sort) @@ -148,7 +148,7 @@ remove_obsolete_shims() { local formatted_exec_names local temp_dir - temp_dir=${TMPDIR:-/tmp} + temp_dir="${TMPDIR:-/tmp}" # comm only takes to files, so we write this data to temp files so we can # pass it to comm. diff --git a/lib/functions/installs.bash b/lib/functions/installs.bash index 6a1ab251..33fd92f3 100644 --- a/lib/functions/installs.bash +++ b/lib/functions/installs.bash @@ -1,18 +1,18 @@ handle_failure() { - local install_path="$1" + local install_path="${1:-}" rm -rf "$install_path" exit 1 } handle_cancel() { - local install_path="$1" + local install_path="${1:-}" printf "\\nreceived sigint, cleaning up" handle_failure "$install_path" } install_command() { - local plugin_name=$1 - local full_version=$2 + local plugin_name="${1:-}" + local full_version="${2:-}" local extra_args="${*:3}" if [ "$plugin_name" = "" ] && [ "$full_version" = "" ]; then @@ -37,7 +37,7 @@ get_concurrency() { } install_one_local_tool() { - local plugin_name=$1 + local plugin_name="${1:-}" local search_path search_path=$(pwd) @@ -131,9 +131,9 @@ install_local_tool_versions() { } install_tool_version() { - local plugin_name=$1 - local full_version=$2 - local flags=$3 + local plugin_name="${1:-}" + local full_version="${2:-}" + local flags="${3:-}" local keep_download local plugin_path diff --git a/lib/functions/plugins.bash b/lib/functions/plugins.bash index e9e848c3..6d8c3903 100644 --- a/lib/functions/plugins.bash +++ b/lib/functions/plugins.bash @@ -6,7 +6,7 @@ plugin_list_command() { local show_ref while [ -n "$*" ]; do - case "$1" in + case "${1:-}" in "--urls") show_repo=true shift @@ -109,8 +109,8 @@ plugin_update_command() { exit 1 fi - local plugin_name="$1" - local gitref="${2}" + local plugin_name="${1:-}" + local gitref="${2:-}" local plugins= if [ "$plugin_name" = "--all" ]; then @@ -130,10 +130,10 @@ plugin_update_command() { } update_plugin() { - local plugin_name=$1 - local plugin_path=$2 - plugin_remote_default_branch=$(git --git-dir "$plugin_path/.git" --work-tree "$plugin_path" ls-remote --symref origin HEAD | awk '{ sub(/refs\/heads\//, ""); print $2; exit }') - local gitref=${3:-${plugin_remote_default_branch}} + local plugin_name="${1:-}" + local plugin_path="${2:-}" + plugin_remote_default_branch="$(git --git-dir "$plugin_path/.git" --work-tree "$plugin_path" ls-remote --symref origin HEAD | awk '{ sub(/refs\/heads\//, ""); print $2; exit }')" + local gitref="${3:-${plugin_remote_default_branch}}" logfile=$(mktemp) local common_git_options=(--git-dir "$plugin_path/.git" --work-tree "$plugin_path") diff --git a/lib/functions/versions.bash b/lib/functions/versions.bash index 394d4959..d1f2b46e 100644 --- a/lib/functions/versions.bash +++ b/lib/functions/versions.bash @@ -1,6 +1,6 @@ version_command() { - local cmd=$1 - local plugin_name=$2 + local cmd="${1:-}" + local plugin_name="${2:-}" if [ "$#" -lt "3" ]; then if [ "$cmd" = "global" ]; then @@ -65,17 +65,17 @@ version_command() { } list_all_command() { - local plugin_name=$1 - local query=$2 + local plugin_name="${1:-}" + local query="${2:-}" local plugin_path local std_out_file local std_err_file local output - plugin_path=$(get_plugin_path "$plugin_name") + plugin_path="$(get_plugin_path "$plugin_name")" check_if_plugin_exists "$plugin_name" local temp_dir - temp_dir=${TMPDIR:-/tmp} + temp_dir="${TMPDIR:-/tmp}" # Capture return code to allow error handling std_out_file="$(mktemp "$temp_dir/asdf-command-list-all-${plugin_name}.stdout.XXXXXX")" @@ -117,8 +117,8 @@ list_all_command() { latest_command() { DEFAULT_QUERY="[0-9]" - local plugin_name=$1 - local query=$2 + local plugin_name="${1:-}" + local query="${2:-}" local plugin_path if [ "$plugin_name" == "--all" ]; then