Skip to content

Instantly share code, notes, and snippets.

@dkordik
Created June 6, 2019 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkordik/d6ba61004301459d53d19b1ac6a69b14 to your computer and use it in GitHub Desktop.
Save dkordik/d6ba61004301459d53d19b1ac6a69b14 to your computer and use it in GitHub Desktop.
Safe Install - specify dependencies to install. Verify they don't exist first. Verify they DO exist when we're done.
#!/bin/bash
set -e # exit when a command fails, so we don't continue doing next steps!
function check_exists {
CMD="$1"
# "type" works nicely for checking existence of both shell functions and scripts/bins
if type -t "$CMD" 1> /dev/null 2>/dev/null; then
true
else
false
fi
}
function refresh_profile {
source ~/.bash_profile
}
function safe_install {
NAME="$1"
EXISTING_CMD_TO_CHECK="$2"
INSTALLATION_CMD="$3"
echo "🔍 Checking for existence of $NAME... (Errors here are expected)"
if check_exists "$EXISTING_CMD_TO_CHECK"; then
echo "☑️ Skipping $NAME, found existing installation."
else
echo "⬇️ Installing $NAME..."
if eval $INSTALLATION_CMD; then
echo "Installing $NAME completed."
echo "🔁 Refreshing PATH in this subshell..."
refresh_profile
if check_exists "$EXISTING_CMD_TO_CHECK"; then
echo "✅ Install of $NAME: Verified!"
else
echo "⏺ Installed $NAME, but couldn't verify install. Quitting."
exit 1
fi
else
echo "😢 Error installing $NAME - Exiting before making more changes."
exit 1
fi
fi
}
refresh_profile # load up any existing shell functions/PATH stuff
# syntax:
# safe_install "<readable name>" "<existing command to check if installed>" '<install command>'
# Example: safe_install "SDKMAN" "sdk" 'curl -s "https://get.sdkman.io" | bash'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment