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

Shellcheck #223

Merged
merged 13 commits into from
Oct 3, 2017
Merged
11 changes: 10 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
language: c
script: bats test

script:
- bats test
- ./lint.sh

before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install shellcheck; fi

before_script:
- git clone https://github.com/sstephenson/bats.git /tmp/bats
- export PATH=/tmp/bats/bin:$PATH

os:
- linux
- osx
68 changes: 44 additions & 24 deletions bin/asdf
Original file line number Diff line number Diff line change
@@ -1,34 +1,54 @@
#!/usr/bin/env bash

source $(dirname $(dirname $0))/lib/utils.sh

source $(dirname $(dirname $0))/lib/commands/help.sh
source $(dirname $(dirname $0))/lib/commands/update.sh
source $(dirname $(dirname $0))/lib/commands/install.sh
source $(dirname $(dirname $0))/lib/commands/uninstall.sh
source $(dirname $(dirname $0))/lib/commands/current.sh
source $(dirname $(dirname $0))/lib/commands/where.sh
source $(dirname $(dirname $0))/lib/commands/which.sh
source $(dirname $(dirname $0))/lib/commands/version_commands.sh
source $(dirname $(dirname $0))/lib/commands/list.sh
source $(dirname $(dirname $0))/lib/commands/list-all.sh
source $(dirname $(dirname $0))/lib/commands/reshim.sh
source $(dirname $(dirname $0))/lib/commands/plugin-add.sh
source $(dirname $(dirname $0))/lib/commands/plugin-list.sh
source $(dirname $(dirname $0))/lib/commands/plugin-list-all.sh
source $(dirname $(dirname $0))/lib/commands/plugin-update.sh
source $(dirname $(dirname $0))/lib/commands/plugin-remove.sh

source $(dirname $(dirname $0))/lib/commands/plugin-push.sh
source $(dirname $(dirname $0))/lib/commands/plugin-test.sh


# shellcheck source=lib/utils.sh
source "$(dirname "$(dirname "$0")")/lib/utils.sh"

# shellcheck source=lib/commands/help.sh
source "$(dirname "$(dirname "$0")")/lib/commands/help.sh"
# shellcheck source=lib/commands/update.sh
source "$(dirname "$(dirname "$0")")/lib/commands/update.sh"
# shellcheck source=lib/commands/install.sh
source "$(dirname "$(dirname "$0")")/lib/commands/install.sh"
# shellcheck source=lib/commands/uninstall.sh
source "$(dirname "$(dirname "$0")")/lib/commands/uninstall.sh"
# shellcheck source=lib/commands/current.sh
source "$(dirname "$(dirname "$0")")/lib/commands/current.sh"
# shellcheck source=lib/commands/where.sh
source "$(dirname "$(dirname "$0")")/lib/commands/where.sh"
# shellcheck source=lib/commands/which.sh
source "$(dirname "$(dirname "$0")")/lib/commands/which.sh"
# shellcheck source=lib/commands/version_commands.sh
source "$(dirname "$(dirname "$0")")/lib/commands/version_commands.sh"
# shellcheck source=lib/commands/list.sh
source "$(dirname "$(dirname "$0")")/lib/commands/list.sh"
# shellcheck source=lib/commands/list-all.sh
source "$(dirname "$(dirname "$0")")/lib/commands/list-all.sh"
# shellcheck source=lib/commands/reshim.sh
source "$(dirname "$(dirname "$0")")/lib/commands/reshim.sh"
# shellcheck source=lib/commands/plugin-add.sh
source "$(dirname "$(dirname "$0")")/lib/commands/plugin-add.sh"
# shellcheck source=lib/commands/plugin-list.sh
source "$(dirname "$(dirname "$0")")/lib/commands/plugin-list.sh"
# shellcheck source=lib/commands/plugin-list-all.sh
source "$(dirname "$(dirname "$0")")/lib/commands/plugin-list-all.sh"
# shellcheck source=lib/commands/plugin-update.sh
source "$(dirname "$(dirname "$0")")/lib/commands/plugin-update.sh"
# shellcheck source=lib/commands/plugin-remove.sh
source "$(dirname "$(dirname "$0")")/lib/commands/plugin-remove.sh"

# shellcheck source=lib/commands/plugin-push.sh
source "$(dirname "$(dirname "$0")")/lib/commands/plugin-push.sh"
# shellcheck source=lib/commands/plugin-test.sh
source "$(dirname "$(dirname "$0")")/lib/commands/plugin-test.sh"

# shellcheck disable=SC2124
callback_args="${@:2}"

# shellcheck disable=SC2086
case $1 in

"--version")
asdf_version $callback_args;;
asdf_version $callback_args;;

"help")
help_command $callback_args;;
Expand Down
41 changes: 22 additions & 19 deletions bin/private/asdf-exec
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
#!/usr/bin/env bash

source $(dirname $(dirname $(dirname $0)))/lib/utils.sh
# shellcheck source=lib/utils.sh
source "$(dirname "$(dirname "$(dirname "$0")")")/lib/utils.sh"

plugin_name=$1
executable_path=$2

plugin_path=$(get_plugin_path $plugin_name)
check_if_plugin_exists $plugin_name
plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"

full_version=$(get_preset_version_for $plugin_name)
full_version=$(get_preset_version_for "$plugin_name")

if [ "$full_version" == "" ]; then
echo "No version set for ${plugin_name}"
exit -1
fi

# shellcheck disable=SC2162
IFS=' ' read -a versions <<< "$full_version"

for version in "${versions[@]}"; do
install_path=$(find_install_path $plugin_name $version)
install_path=$(find_install_path "$plugin_name" "$version")

if [ $version != "system" ] && [ ! -d "$install_path" ]; then
if [ "$version" != "system" ] && [ ! -d "$install_path" ]; then
echo "$plugin_name $version not installed"
exit 1
fi

full_executable_path=$(get_executable_path $plugin_name $version $executable_path)
if full_executable_path=$(get_executable_path "$plugin_name" "$version" "$executable_path"); then
if [ -f "$full_executable_path" ]; then
if [ -f "${plugin_path}/bin/exec-env" ]; then
export ASDF_INSTALL_TYPE=$install_type
export ASDF_INSTALL_VERSION=$version
export ASDF_INSTALL_PATH=$install_path

if [ $? -eq 0 -a -f "$full_executable_path" ]; then
if [ -f ${plugin_path}/bin/exec-env ]; then
export ASDF_INSTALL_TYPE=$install_type
export ASDF_INSTALL_VERSION=$version
export ASDF_INSTALL_PATH=$install_path
# shellcheck source=/dev/null
source "${plugin_path}/bin/exec-env"

source ${plugin_path}/bin/exec-env
# unset everything, we don't want to pollute
unset ASDF_INSTALL_TYPE
unset ASDF_INSTALL_VERSION
unset ASDF_INSTALL_PATH
fi

# unset everything, we don't want to pollute
unset ASDF_INSTALL_TYPE
unset ASDF_INSTALL_VERSION
unset ASDF_INSTALL_PATH
exec "$full_executable_path" "${@:3}"
fi

exec $full_executable_path "${@:3}"
fi
done

Expand Down
32 changes: 19 additions & 13 deletions completions/asdf.bash
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
#!/usr/bin/env bash

_asdf () {
local cur=${COMP_WORDS[COMP_CWORD]}
local cmd=${COMP_WORDS[1]}
local prev=${COMP_WORDS[COMP_CWORD-1]}
local plugins=$(asdf plugin-list | tr '\n' ' ')
local cur
cur=${COMP_WORDS[COMP_CWORD]}
local cmd
cmd=${COMP_WORDS[1]}
local prev
prev=${COMP_WORDS[COMP_CWORD-1]}
local plugins
plugins=$(asdf plugin-list | tr '\n' ' ')

COMPREPLY=()

case "$cmd" in
plugin-update)
COMPREPLY=($(compgen -W "$plugins --all" -- $cur))
COMPREPLY=($(compgen -W "$plugins --all" -- "$cur"))
;;
plugin-remove|current|list|list-all)
COMPREPLY=($(compgen -W "$plugins" -- $cur))
COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
;;
install)
if [[ "$plugins" == *"$prev"* ]] ; then
local versions=$(asdf list-all $prev)
COMPREPLY=($(compgen -W "$versions" -- $cur))
local versions
versions=$(asdf list-all "$prev")
COMPREPLY=($(compgen -W "$versions" -- "$cur"))
else
COMPREPLY=($(compgen -W "$plugins" -- $cur))
COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
fi
;;
uninstall|where|reshim|local|global)
if [[ "$plugins" == *"$prev"* ]] ; then
local versions=$(asdf list $prev)
COMPREPLY=($(compgen -W "$versions" -- $cur))
local versions
versions=$(asdf list "$prev")
COMPREPLY=($(compgen -W "$versions" -- "$cur"))
else
COMPREPLY=($(compgen -W "$plugins" -- $cur))
COMPREPLY=($(compgen -W "$plugins" -- "$cur"))
fi
;;
*)
local cmds='plugin-add plugin-list plugin-remove plugin-update install uninstall update current where which list list-all local global reshim'
COMPREPLY=($(compgen -W "$cmds" -- $cur))
COMPREPLY=($(compgen -W "$cmds" -- "$cur"))
;;
esac

Expand Down
30 changes: 18 additions & 12 deletions lib/commands/current.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
plugin_current_command() {
local plugin_name=$1

check_if_plugin_exists $plugin_name
check_if_plugin_exists "$plugin_name"

local search_path=$(pwd)
local version_and_path=$(find_version "$plugin_name" "$search_path")
local version=$(cut -d '|' -f 1 <<< "$version_and_path");
local version_file_path=$(cut -d '|' -f 2 <<< "$version_and_path");
local search_path
search_path=$(pwd)
local version_and_path
version_and_path=$(find_version "$plugin_name" "$search_path")
local version
version=$(cut -d '|' -f 1 <<< "$version_and_path");
local version_file_path
version_file_path=$(cut -d '|' -f 2 <<< "$version_and_path");

check_if_version_exists $plugin_name $version
check_for_deprecated_plugin $plugin_name
check_if_version_exists "$plugin_name" "$version"
check_for_deprecated_plugin "$plugin_name"

if [ -z "$version" ]; then
echo "No version set for $plugin_name"
Expand All @@ -22,24 +26,26 @@ plugin_current_command() {
current_command() {
if [ $# -eq 0 ]; then
for plugin in $(plugin_list_command); do
echo "$plugin $(plugin_current_command $plugin)"
echo "$plugin $(plugin_current_command "$plugin")"
done
else
local plugin=$1
plugin_current_command $plugin
plugin_current_command "$plugin"
fi
}

# Warn if the plugin isn't using the updated legacy file api.
check_for_deprecated_plugin() {
local plugin_name=$1

local plugin_path=$(get_plugin_path "$plugin_name")
local legacy_config=$(get_asdf_config_value "legacy_version_file")
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
local legacy_config
legacy_config=$(get_asdf_config_value "legacy_version_file")
local deprecated_script="${plugin_path}/bin/get-version-from-legacy-file"
local new_script="${plugin_path}/bin/list-legacy-filenames"

if [ "$legacy_config" = "yes" ] && [ -f $deprecated_script ] && [ ! -f $new_script ]; then
if [ "$legacy_config" = "yes" ] && [ -f "$deprecated_script" ] && [ ! -f "$new_script" ]; then
echo "Heads up! It looks like your $plugin_name plugin is out of date. You can update it with:"
echo ""
echo " asdf plugin-update $plugin_name"
Expand Down
2 changes: 1 addition & 1 deletion lib/commands/help.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
help_command () {
echo "version: $(asdf_version)"
echo ""
cat $(asdf_dir)/help.txt
cat "$(asdf_dir)/help.txt"
}
38 changes: 22 additions & 16 deletions lib/commands/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,33 +20,36 @@ install_command() {
display_error "You must specify a name and a version to install"
exit 1
else
install_tool_version $plugin_name $full_version
install_tool_version "$plugin_name" "$full_version"
fi
}

get_concurrency() {
if which nproc > /dev/null 2>&1; then
echo $(nproc)
nproc
elif which sysctl > /dev/null 2>&1 && sysctl hw.ncpu > /dev/null 2>&1; then
echo $(sysctl -n hw.ncpu)
sysctl -n hw.ncpu
elif [ -f /proc/cpuinfo ]; then
echo $(grep -c processor /proc/cpuinfo)
grep -c processor /proc/cpuinfo
Copy link
Member

Choose a reason for hiding this comment

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

I think the echos may still be needed here if something is capturing the output from this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not super well versed in bash scripting, so please feel free to let me know if something I suggest is incorrect. That said, I'm not sure why the echo is needed if the output of the grep command is sent to stdout/err already. It was a recommendation from shellcheck which may or may not apply here.

For the rest of comments, feel free to run this with shellcheck yourself to see the error output. I put a lint.sh file in the root of the project that lints the project files.

else
echo "1"
fi
}

install_local_tool_versions() {
if [ -f "$(pwd)/.tool-versions" ]; then
local asdf_versions_path="$(pwd)/.tool-versions"
local asdf_versions_path
asdf_versions_path="$(pwd)/.tool-versions"

while read tool_line; do
IFS=' ' read -a tool_info <<< $tool_line
local tool_name=$(echo "${tool_info[0]}" | xargs)
local tool_version=$(echo "${tool_info[1]}" | xargs)
while read -r tool_line; do
IFS=' ' read -r -a tool_info <<< "$tool_line"
local tool_name
tool_name=$(echo "${tool_info[0]}" | xargs)
local tool_version
tool_version=$(echo "${tool_info[1]}" | xargs)

if ! [[ -z "$tool_name" || -z "$tool_version" ]]; then
install_tool_version $tool_name $tool_version
install_tool_version "$tool_name" "$tool_version"
fi
done < "$asdf_versions_path"
else
Expand All @@ -60,11 +63,12 @@ install_local_tool_versions() {
install_tool_version() {
local plugin_name=$1
local full_version=$2
local plugin_path=$(get_plugin_path $plugin_name)
check_if_plugin_exists $plugin_name
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"


IFS=':' read -a version_info <<< "$full_version"
IFS=':' read -r -a version_info <<< "$full_version"
if [ "${version_info[0]}" = "ref" ]; then
local install_type="${version_info[0]}"
local version="${version_info[1]}"
Expand All @@ -74,8 +78,10 @@ install_tool_version() {
fi


local install_path=$(get_install_path $plugin_name $install_type $version)
local concurrency=$(get_concurrency)
local install_path
install_path=$(get_install_path "$plugin_name" "$install_type" "$version")
local concurrency
concurrency=$(get_concurrency)
trap 'handle_cancel $install_path' INT

if [ -d "$install_path" ]; then
Expand All @@ -92,7 +98,7 @@ install_tool_version() {

local exit_code=$?
if [ $exit_code -eq 0 ]; then
reshim_command $plugin_name $full_version
reshim_command "$plugin_name" "$full_version"
else
handle_failure "$install_path"
fi
Expand Down
10 changes: 6 additions & 4 deletions lib/commands/list-all.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
list_all_command() {
local plugin_name=$1
local plugin_path=$(get_plugin_path $plugin_name)
check_if_plugin_exists $plugin_name
local plugin_path
plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"

local versions=$(bash ${plugin_path}/bin/list-all)
local versions
versions=$(bash "${plugin_path}/bin/list-all")

IFS=' ' read -a versions_list <<< "$versions"
IFS=' ' read -r -a versions_list <<< "$versions"

for version in "${versions_list[@]}"
do
Expand Down
Loading