Skip to content

Commit

Permalink
Improve readability & complexity by nvm_has_non_aliased()
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterDaveHello committed Mar 21, 2017
1 parent 6ed73b0 commit feabe9f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
59 changes: 32 additions & 27 deletions nvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ nvm_has() {
type "${1-}" > /dev/null 2>&1
}

nvm_has_non_aliased() {
nvm_has "${1-}" && ! nvm_is_alias "${1-}"
}

nvm_is_alias() {
# this is intentionally not "command alias" so it works in zsh.
# shellcheck disable=SC1001
Expand Down Expand Up @@ -1059,23 +1063,23 @@ nvm_ls_remote_index_tab() {
}

nvm_get_checksum_alg() {
if nvm_has "sha256sum" && ! nvm_is_alias "sha256sum"; then
if nvm_has_non_aliased "sha256sum"; then
nvm_echo 'sha-256'
elif nvm_has "shasum" && ! nvm_is_alias "shasum"; then
elif nvm_has_non_aliased "shasum"; then
nvm_echo 'sha-256'
elif nvm_has "sha256" && ! nvm_is_alias "sha256"; then
elif nvm_has_non_aliased "sha256"; then
nvm_echo 'sha-256'
elif nvm_has "gsha256sum" && ! nvm_is_alias "gsha256sum"; then
elif nvm_has_non_aliased "gsha256sum"; then
nvm_echo 'sha-256'
elif nvm_has "openssl" && ! nvm_is_alias "openssl"; then
elif nvm_has_non_aliased "openssl"; then
nvm_echo 'sha-256'
elif nvm_has "bssl" && ! nvm_is_alias "bssl"; then
elif nvm_has_non_aliased "bssl"; then
nvm_echo 'sha-256'
elif nvm_has "sha1sum" && ! nvm_is_alias "sha1sum"; then
elif nvm_has_non_aliased "sha1sum"; then
nvm_echo 'sha-1'
elif nvm_has "sha1" && ! nvm_is_alias "sha1"; then
elif nvm_has_non_aliased "sha1"; then
nvm_echo 'sha-1'
elif nvm_has "shasum" && ! nvm_is_alias "shasum"; then
elif nvm_has_non_aliased "shasum"; then
nvm_echo 'sha-1'
else
nvm_err 'Unaliased sha256sum, shasum, sha256, gsha256sum, openssl, or bssl not found.'
Expand All @@ -1095,31 +1099,31 @@ nvm_compute_checksum() {
return 1
fi

if nvm_has "sha256sum" && ! nvm_is_alias "sha256sum"; then
if nvm_has_non_aliased "sha256sum"; then
nvm_err 'Computing checksum with sha256sum'
command sha256sum "${FILE}" | command awk '{print $1}'
elif nvm_has "shasum" && ! nvm_is_alias "shasum"; then
elif nvm_has_non_aliased "shasum"; then
nvm_err 'Computing checksum with shasum -a 256'
command shasum -a 256 "${FILE}" | command awk '{print $1}'
elif nvm_has "sha256" && ! nvm_is_alias "sha256"; then
elif nvm_has_non_aliased "sha256"; then
nvm_err 'Computing checksum with sha256 -q'
command sha256 -q "${FILE}" | command awk '{print $1}'
elif nvm_has "gsha256sum" && ! nvm_is_alias "gsha256sum"; then
elif nvm_has_non_aliased "gsha256sum"; then
nvm_err 'Computing checksum with gsha256sum'
command gsha256sum "${FILE}" | command awk '{print $1}'
elif nvm_has "openssl" && ! nvm_is_alias "openssl"; then
elif nvm_has_non_aliased "openssl"; then
nvm_err 'Computing checksum with openssl dgst -sha256'
command openssl dgst -sha256 "${FILE}" | rev | command awk '{print $1}' | rev
elif nvm_has "bssl" && ! nvm_is_alias "bssl"; then
elif nvm_has_non_aliased "bssl"; then
nvm_err 'Computing checksum with bssl sha256sum'
command bssl sha256sum "${FILE}" | command awk '{print $1}'
elif nvm_has "sha1sum" && ! nvm_is_alias "sha1sum"; then
elif nvm_has_non_aliased "sha1sum"; then
nvm_err 'Computing checksum with sha1sum'
command sha1sum "${FILE}" | command awk '{print $1}'
elif nvm_has "sha1" && ! nvm_is_alias "sha1"; then
elif nvm_has_non_aliased "sha1"; then
nvm_err 'Computing checksum with sha1 -q'
command sha1 -q "${FILE}"
elif nvm_has "shasum" && ! nvm_is_alias "shasum"; then
elif nvm_has_non_aliased "shasum"; then
nvm_err 'Computing checksum with shasum'
command shasum "${FILE}" | command awk '{print $1}'
fi
Expand Down Expand Up @@ -1189,28 +1193,28 @@ nvm_get_checksum() {
nvm_checksum() {
local NVM_CHECKSUM
if [ -z "${3-}" ] || [ "${3-}" = 'sha1' ]; then
if nvm_has "sha1sum" && ! nvm_is_alias "sha1sum"; then
if nvm_has_non_aliased "sha1sum"; then
NVM_CHECKSUM="$(command sha1sum "${1-}" | command awk '{print $1}')"
elif nvm_has "sha1" && ! nvm_is_alias "sha1"; then
elif nvm_has_non_aliased "sha1"; then
NVM_CHECKSUM="$(command sha1 -q "${1-}")"
elif nvm_has "shasum" && ! nvm_is_alias "shasum"; then
elif nvm_has_non_aliased "shasum"; then
NVM_CHECKSUM="$(command shasum "${1-}" | command awk '{print $1}')"
else
nvm_err 'Unaliased sha1sum, sha1, or shasum not found.'
return 2
fi
else
if nvm_has "sha256sum" && ! nvm_is_alias "sha256sum"; then
if nvm_has_non_aliased "sha256sum"; then
NVM_CHECKSUM="$(command sha256sum "${1-}" | command awk '{print $1}')"
elif nvm_has "shasum" && ! nvm_is_alias "shasum"; then
elif nvm_has_non_aliased "shasum"; then
NVM_CHECKSUM="$(command shasum -a 256 "${1-}" | command awk '{print $1}')"
elif nvm_has "sha256" && ! nvm_is_alias "sha256"; then
elif nvm_has_non_aliased "sha256"; then
NVM_CHECKSUM="$(command sha256 -q "${1-}" | command awk '{print $1}')"
elif nvm_has "gsha256sum" && ! nvm_is_alias "gsha256sum"; then
elif nvm_has_non_aliased "gsha256sum"; then
NVM_CHECKSUM="$(command gsha256sum "${1-}" | command awk '{print $1}')"
elif nvm_has "openssl" && ! nvm_is_alias "openssl"; then
elif nvm_has_non_aliased "openssl"; then
NVM_CHECKSUM="$(command openssl dgst -sha256 "${1-}" | rev | command awk '{print $1}' | rev)"
elif nvm_has "bssl" && ! nvm_is_alias "bssl"; then
elif nvm_has_non_aliased "bssl"; then
NVM_CHECKSUM="$(command bssl sha256sum "${1-}" | command awk '{print $1}')"
else
nvm_err 'Unaliased sha256sum, shasum, sha256, gsha256sum, openssl, or bssl not found.'
Expand Down Expand Up @@ -3189,6 +3193,7 @@ nvm() {
nvm_print_default_alias nvm_print_formatted_alias nvm_resolve_local_alias \
nvm_sanitize_path nvm_has_colors nvm_process_parameters \
node_version_has_solaris_binary iojs_version_has_solaris_binary \
nvm_has_non_aliased \
> /dev/null 2>&1
unset RC_VERSION NVM_NODEJS_ORG_MIRROR NVM_IOJS_ORG_MIRROR NVM_DIR \
NVM_CD_FLAGS NVM_BIN NVM_MAKE_JOBS \
Expand Down
24 changes: 24 additions & 0 deletions test/fast/Unit tests/nvm_has_non_aliased
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/sh

cleanup () { unalias foo; unalias grep; }
die () { echo "$@" ; cleanup ; exit 1; }

\. ../../../nvm.sh

alias foo='bar'
! nvm_has_non_aliased foo || die '"nvm_has_non_aliased foo" was not true'

alias grep='grep'
unalias grep || die '"unalias grep" failed'
nvm_has_non_aliased grep || die '"nvm_has_non_aliased grep" with unaliased grep was not false'

alias grep='grep'
! nvm_has_non_aliased grep || die '"nvm_is_alias grep" with aliased grep was not true'

nvm_has_non_aliased cat && type cat > /dev/null || die 'nvm_has_non_aliased locates "cat" properly'

[ "~$(nvm_has_non_aliased foobarbaz 2>&1)" = "~" ] || die "nvm_has_non_aliased does not suppress error output"

! nvm_has_non_aliased foobarbaz && ! type foobarbaz >/dev/null 2>&1 || die "nvm_has_non_aliased does not return a nonzero exit code when not found"

cleanup

0 comments on commit feabe9f

Please sign in to comment.