support FreeBSD's sha1(1) for integrity checks

In the absense of shasum(1) (which on FreeBSD is provided by Perl,
from ports) nvm falls back to sha1sum(1) which does not exist on
FreeBSD.  But FreeBSD does have sha1(1) so look for sha1(1) and use
it if present.

As part of this change, refactor the execution of the checksum
program down into nvm_checksum and also clean up some special-casing
of empty dist checksums, which is already handled by nvm_checksum.
master
Fraser Tweedale 2013-08-10 14:30:47 +10:00
parent 12d7b6fa0c
commit 0c8410fcc3
1 changed files with 11 additions and 8 deletions

19
nvm.sh
View File

@ -125,7 +125,15 @@ nvm_ls_remote() {
}
nvm_checksum() {
if [ "$1" = "$2" ]; then
if which shasum >/dev/null 2>&1; then
checksum=$(shasum $1 | awk '{print $1}')
elif which sha1 >/dev/null 2>&1; then
checksum=$(sha1 -q $1)
else
checksum=$(sha1sum $1 | awk '{print $1}')
fi
if [ "$checksum" = "$2" ]; then
return
elif [ -z $2 ]; then
echo 'Checksums empty' #missing in raspberry pi binary
@ -220,17 +228,12 @@ nvm() {
local url
local sum
local tarball
local shasum='shasum'
local nobinary
if ! has "curl"; then
echo 'NVM Needs curl to proceed.' >&2;
fi
if ! has "shasum"; then
shasum='sha1sum'
fi
if [ $# -lt 2 ]; then
nvm help
return
@ -283,7 +286,7 @@ nvm() {
if (
mkdir -p "$tmpdir" && \
curl -L -C - --progress-bar $url -o "$tmptarball" && \
nvm_checksum `${shasum} "$tmptarball" | awk '{print $1}'` $sum && \
nvm_checksum "$tmptarball" $sum && \
tar -xzf "$tmptarball" -C "$tmpdir" --strip-components 1 && \
rm -f "$tmptarball" && \
mv "$tmpdir" "$NVM_DIR/$VERSION"
@ -319,7 +322,7 @@ nvm() {
[ ! -z $tarball ] && \
mkdir -p "$tmpdir" && \
curl -L --progress-bar $tarball -o "$tmptarball" && \
if [ "$sum" = "" ]; then : ; else nvm_checksum `${shasum} "$tmptarball" | awk '{print $1}'` $sum; fi && \
nvm_checksum "$tmptarball" $sum && \
tar -xzf "$tmptarball" -C "$tmpdir" && \
cd "$tmpdir/node-$VERSION" && \
./configure --prefix="$NVM_DIR/$VERSION" $ADDITIONAL_PARAMETERS && \