From 7cfb8b9ca2e66a79bb30e4762277dc3020aec70e Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Sun, 15 Jun 2014 02:45:53 -0700 Subject: Refactor code and improve warning logic. --- rpath.sh | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/rpath.sh b/rpath.sh index 7fa43d1..0efaf44 100755 --- a/rpath.sh +++ b/rpath.sh @@ -62,21 +62,23 @@ _match() { return 1 } -_get() { - local dir dirs succeed=false +_populate_selected() { + local dir dirs + unset selected IFS=':' read -a dirs <<< "${PATH}" for dir in "${dirs[@]}"; do if [[ "${dir}" == "${RUBIES_PATH}/"* ]]; then dir="${dir%/bin}" - echo "${dir##*/}" - - succeed=true + selected+=("${dir##*/}") fi done - $succeed || return 1 + [[ -n ${selected} ]] || return 1 + + [[ "${#selected[@]}" -eq 1 ]] \ + || _warn 'warning: multiple rubies found in PATH.' } _set() { @@ -108,26 +110,25 @@ _clear() { rpath_ls() { _populate_dirs + _populate_selected counter=0 - current=$(_get | head -n 1) for dir in "${dirs[@]}"; do str=" [$((++counter))] ${dir##*/}" - [[ "${dir##*/}" == "${current}" ]] && str="${str/ /*}" + [[ "${dir##*/}" == "${selected}" ]] && str="${str/ /*}" _echo "${str}" done } rpath_get() { - current="$(_get)" || _die 'no rubies found in PATH.' - - [[ $(wc -l <<< "${current}") -gt 1 ]] \ - && _warn 'warning: multiple rubies found in PATH.' + _populate_selected || _die 'no rubies found in PATH.' - _echo "${current}" + for dir in "${selected[@]}"; do + _echo "${dir}" + done } rpath_set() { -- cgit v1.2.3-70-g09d2