Skip to content

Instantly share code, notes, and snippets.

@erawhctim
Created January 16, 2017 16:03
Show Gist options
  • Save erawhctim/83bbe6a49d49461c0bb662f323b3753d to your computer and use it in GitHub Desktop.
Save erawhctim/83bbe6a49d49461c0bb662f323b3753d to your computer and use it in GitHub Desktop.
Bash script starter file - colors, status messages, custom functions, etc
#!/bin/sh
# Originally pulled from: https://robots.thoughtbot.com/shell-script-suggestions-for-speedy-setups
# Exit if any subcommand fails
set -e
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NO_COLOR='\033[0m'
CLEAR_LINE='\r\033[K'
printf "[1/6]🔎 checking dependencies"
# Check if a commmand exists in the path/has been installed.
if ! command -v node > /dev/null; then
# Set a specific color on a per-line basis
printf "${CLEAR_LINE}💀${RED} You must install node on your system before setup can continue${NO_COLOR}\n"
printf "ℹ️ On macOS🍎 you should 'brew install node'\n"
exit -1
fi
# Check command output, e.g. version number verification
if [[ $(node --version) != "v4.6.0" ]]; then
printf "${CLEAR_LINE}⚠️${YELLOW} You are not using a known working version of node.${NO_COLOR}\n"
printf "ℹ️ This might not be a problem but if you're having issues, try installing 4.6.0\n"
printf "[1/6]🔎 checking dependencies"
fi
# Run a script or install something, redirecting output to /dev/null for a silent installation
# (Errors printed to stderr will still be output, this just supresses regular output from stdout
printf "${CLEAR_LINE}[2/6]⏳ Installing yarn packages"
yarn > /dev/null
# Example of a custom function, this one checks out git dependencies
function clone {
printf "⏳ $1 is being cloned"
cd $DIR/..
if [ ! -d $1 ]; then
# don't stop the script if there is an error cloning from github
set +e
git clone $2 > /dev/null
# if the last command gives a non-zero exit code we should warn the user
if [[ $? != 0 ]]; then
printf "${CLEAR_LINE}❌${RED} $1 failed to clone! You might not have permissions.${NO_COLOR}\n"
fi
set -e
fi
cd $DIR
}
clone dependency_one git@github.com:my_org/dependency_one.git
clone dependency_two git@github.com:my_org/dependency_two.git
# Divide scripts into subcommands with a specific purpose, then
# call them all at once in the main script to keep things clean
. ./bin/colors
./bin/setup_steps/check_dependencies
./bin/setup_steps/install_plugins
./bin/setup_steps/setup_ssl
./bin/setup_steps/pull_environment_vars_from_heroku
printf "${CLEAR_LINE}[6/6]🎉${GREEN} Finished!${NO_COLOR}\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment