Improvements to install script [skip ci] (#870)

- Use `mktemp -d` to generate a temporary directory, only if user
  doesn't use `--download-dir`
- Clean-up the temporary directory using `trap`. Remove
  other `rm` calls from code since they're not necessary
  anymore
- Run download in a subshell, so after finishing it the
  script is still on the directory where the user ran
  the script. Fix `./install --dir .` usage
- Fix backup of old Babashka install
This commit is contained in:
Thiago Kenji Okada 2021-05-28 12:07:52 -03:00 committed by GitHub
parent 1c1d4b531f
commit e955926ee9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

43
install
View file

@ -7,8 +7,7 @@ checksum=""
static_binary="false" static_binary="false"
default_install_dir="/usr/local/bin" default_install_dir="/usr/local/bin"
install_dir="$default_install_dir" install_dir="$default_install_dir"
default_download_dir=${TMPDIR:-"/tmp"} download_dir=""
download_dir="$default_download_dir"
print_help() { print_help() {
echo "Installs latest (or specific) version of babashka. Installation directory defaults to /usr/local/bin." echo "Installs latest (or specific) version of babashka. Installation directory defaults to /usr/local/bin."
@ -18,7 +17,7 @@ print_help() {
echo -e echo -e
echo "Defaults:" echo "Defaults:"
echo " * Installation directory: ${default_install_dir}" echo " * Installation directory: ${default_install_dir}"
echo " * Download directory: ${default_download_dir}" echo " * Download directory: temporary"
if [[ -z "$checksum" ]]; then if [[ -z "$checksum" ]]; then
echo " * Checksum: no" echo " * Checksum: no"
else else
@ -64,6 +63,11 @@ do
esac esac
done done
if [[ -z "$download_dir" ]]; then
download_dir="$(mktemp -d)"
trap 'rm -rf "$download_dir"' EXIT
fi
if [[ "$checksum" != "" ]] && [[ "$version" == "" ]]; then if [[ "$checksum" != "" ]] && [[ "$version" == "" ]]; then
>&2 echo "Options --checksum and --version should be provided together!" >&2 echo "Options --checksum and --version should be provided together!"
exit 1 exit 1
@ -81,7 +85,7 @@ esac
case "$(uname -m)" in case "$(uname -m)" in
aarch64) arch=aarch64;; aarch64) arch=aarch64;;
esac esac
arch=${arch:-amd64} arch="${arch:-amd64}"
# Ugly ugly conversion of version to a comparable number # Ugly ugly conversion of version to a comparable number
IFS='.' read -ra VER <<< "$version" IFS='.' read -ra VER <<< "$version"
@ -106,29 +110,30 @@ else
fi fi
download_url="https://github.com/babashka/babashka/releases/download/v$version/$filename" download_url="https://github.com/babashka/babashka/releases/download/v$version/$filename"
mkdir -p "$download_dir" # Running this part in a subshell so when it finishes we go back to the previous directory
cd "$download_dir" mkdir -p "$download_dir" && (
echo -e "Downloading $download_url to $download_dir" cd "$download_dir"
echo -e "Downloading $download_url to $download_dir"
rm -rf "$filename" curl -o "$filename" -sL "$download_url"
rm -rf "bb" if [[ -n "$checksum" ]]; then
curl -o "$filename" -sL "$download_url" if ! echo "$checksum *$filename" | shasum -a 256 --check --status; then
if [[ -n "$checksum" ]]; then >&2 echo "Failed checksum on $filename"
if ! echo "$checksum *$filename" | shasum -a 256 --check --status; then >&2 echo "Got: $(shasum -a 256 "$filename" | cut -d' ' -f1)"
>&2 echo "Failed checksum on $filename" >&2 echo "Expected: $checksum"
>&2 echo "Got: $(shasum -a 256 "$filename" | cut -d' ' -f1)" exit 1
>&2 echo "Expected: $checksum" fi
exit 1
fi fi
fi $util "$filename"
$util "$filename" rm -f "$filename"
rm "$filename" )
if [[ "$download_dir" != "$install_dir" ]] if [[ "$download_dir" != "$install_dir" ]]
then then
mkdir -p "$install_dir" mkdir -p "$install_dir"
if [ -f "$install_dir/bb" ]; then if [ -f "$install_dir/bb" ]; then
echo "Moving $install_dir/bb to $install_dir/bb.old" echo "Moving $install_dir/bb to $install_dir/bb.old"
mv -f "$install_dir/bb" "$install_dir/bb.old"
fi fi
mv -f "$download_dir/bb" "$install_dir/bb" mv -f "$download_dir/bb" "$install_dir/bb"
fi fi