Last active
November 3, 2020 22:40
-
-
Save ericyd/334678cb647a070a6177e58ab91bde7b to your computer and use it in GitHub Desktop.
Helpful shell functions. Should work with Bash or Zsh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh --login | |
LOG_FILE="$(pwd)/qa-setup-$(date +%Y-%m-%d_%H.%M.%S%Z).log" | |
############################# | |
# | |
# Utilities and setup | |
# | |
############################# | |
warnings="\n\nResources / References\n======================\n\n" | |
errors="\n\nErrors\n======================\n\n" | |
success() { echo "✅ $1"; } | |
error() { echo "❗ $1"; } | |
add_error() { error "$1"; errors="$(echo $errors)\n\n❗ $1"; } | |
add_warning() { warnings="$(echo $warnings)\n\n• $1"; } | |
print_messages() { echo -e "$warnings"; echo -e "$errors"; } | |
# Usage: | |
# if [ ! $(is_program_installed "npm") ]; then echo "npm not installed"; fi | |
# if [ $(is_program_installed "npm") ]; then echo "npm is installed"; fi | |
is_program_installed () { command -v $1; } | |
# Usage: | |
# yes_no "my prompt" "function_yes" "function_no" | |
# where "my prompt" is shown to the user, appended with "[y/n]" | |
# and function_yes and function_no are available commands or shell functions | |
yes_no () { | |
echo -n "$1 [y/n] "; read proceed | |
if [[ "$proceed" =~ [Yy] ]]; then | |
eval ${2} | |
elif [[ "$proceed" =~ [Nn] ]]; then | |
eval ${3} | |
else | |
yes_no "$1" "$2" "$3" | |
fi | |
} | |
############################ | |
# | |
# Homebrew | |
# | |
############################ | |
install_homebrew() { | |
echo "Installing Homebrew. This may take a while..." | |
# from https://brew.sh/ | |
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" | |
} | |
exit_no_homebrew() { | |
error "Cannot proceed without Homebrew" | |
exit 1 | |
} | |
setup_homebrew() { | |
if [ ! "$(is_program_installed 'brew')" ]; then | |
yes_no "Homebrew is required. Do you want to install?" "install_homebrew" "exit_no_homebrew" | |
fi | |
success "Homebrew installed" | |
} | |
############################ | |
# | |
# Postgres | |
# | |
############################ | |
configure_postgresql() { | |
# set PGDATA for current session and future | |
export PGDATA=/usr/local/pgsql/data && echo "export PGDATA=/usr/local/pgsql/data" >> ~/.zshrc | |
# make data directory | |
sudo mkdir -p $PGDATA | |
# assign ownership | |
sudo chown $(whoami) $PGDATA | |
# | |
# If you want to verify ownership, you can run this | |
# ls -l $PGDATA/.. | |
# | |
# initialize database | |
initdb | |
# start database in detached mode | |
postgres & | |
success "PostgreSQL configured" | |
} | |
install_postgresql() { | |
echo "Installing PostgreSQL. This may take a while..." | |
brew install postgresql | |
success "PostgreSQL installed" | |
configure_postgresql | |
} | |
setup_postgresql() { | |
if [ "$(is_program_installed 'psql')" ]; then | |
success "PostgreSQL installed" | |
else | |
yes_no "PostgreSQL is required. Do you want to install via Homebrew?" "install_postgresql" "add_warning 'Please install PostgreSQL another way'" | |
fi | |
} | |
############################# | |
# | |
# Git | |
# | |
############################# | |
git_ssh_resources() { | |
add_warning "For help creating or configuring an SSH key for git, please visit: | |
https://help.github.com/en/github/authenticating-to-github/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent | |
https://help.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account" | |
} | |
configure_ssh_key() { | |
echo "You are about to generate an SSH key. You should accept the default file location" | |
git_email=$(git config --global user.email) | |
ssh-keygen -t rsa -b 4096 -C "$git_email" | |
eval "$(ssh-agent -s)" | |
ssh-add -K ~/.ssh/id_rsa | |
add_warning "Please add your SSH key to your Github account. | |
Navigate to https://github.com/settings/ssh/new, | |
give the key a name, | |
and copy/paste the following text into the text box: | |
$(cat ~/.ssh/id_rsa.pub)" | |
success "Created SSH key for Github" | |
} | |
setup_git() { | |
if [ ! "$(is_program_installed 'git')" ]; then | |
brew install git | |
fi | |
if [ ! "$(git config --global user.name)" ]; then | |
echo -n "What is your full name for your github account? "; read git_name | |
git config --global user.name "$git_name" | |
fi | |
if [ ! "$(git config --global user.email)" ]; then | |
echo -n "What is your email address for your github account? "; read git_email | |
git config --global user.email "$git_email" | |
fi | |
add_warning "To update git config in the future, run 'git config --global user.name' or 'git config --global user.email'" | |
yes_no "Would you like to create an SSH key for Git now (recommended)?" "configure_ssh_key" "git_ssh_resources" | |
} | |
############################ | |
# | |
# Ruby version manager | |
# | |
############################ | |
DEFAULT_RUBY_VERSION=$(< .ruby-version) | |
set_rvm_version() { | |
# install ruby version if needed | |
VERSION_INSTALLED=$(rvm list | grep "$DEFAULT_RUBY_VERSION") | |
if [ -z "$VERSION_INSTALLED" ]; then | |
echo "Installing ruby-$DEFAULT_RUBY_VERSION" | |
rvm install "$DEFAULT_RUBY_VERSION" | |
fi | |
# set as local option | |
rvm use "$DEFAULT_RUBY_VERSION" | |
success "rvm configured" | |
} | |
rvm_install() { | |
# from https://rvm.io/ | |
\curl -sSL https://get.rvm.io | bash -s stable | |
success "rvm installed" | |
set_rvm_version | |
} | |
set_rbenv_version() { | |
# install ruby version if needed | |
VERSION_INSTALLED=$(rbenv versions | grep "$DEFAULT_RUBY_VERSION") | |
if [ -z "$VERSION_INSTALLED" ]; then | |
echo "Installing ruby-$DEFAULT_RUBY_VERSION" | |
rbenv install "$DEFAULT_RUBY_VERSION" | |
fi | |
rbenv local "$DEFAULT_RUBY_VERSION" | |
success "rbenv configured" | |
} | |
confirm_rbenv() { | |
PS3="We've noticed issues with rbenv in the past, would you like to proceed with using rbenv or switch to rvm? " | |
select opt in "rvm" "rbenv"; do | |
case $opt in | |
"rvm") | |
rvm_install | |
break ;; | |
"rbenv") | |
set_rbenv_version | |
break ;; | |
*) echo "'$REPLY' is not a valid option. Please select 1 or 2";; | |
esac | |
done | |
} | |
setup_version_manager() { | |
RVM_INSTALLED=$(is_program_installed "rvm") | |
RBENV_INSTALLED=$(is_program_installed "rbenv") | |
if [ ! "$RVM_INSTALLED" ] && [ ! "$RBENV_INSTALLED" ]; then | |
rvm_install | |
elif [ "$RVM_INSTALLED" ]; then | |
set_rvm_version | |
elif [ "$RBENV_INSTALLED" ]; then | |
confirm_rbenv | |
fi | |
} | |
############################# | |
# | |
# Node / npm | |
# | |
############################# | |
install_node() { | |
nvm install $(< .nvmrc) | |
success "Node $(< .nvmrc) installed" | |
} | |
configure_nvm() { | |
cat >> ~/.zshrc << 'NVM_CONFIG' | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm | |
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion | |
NVM_CONFIG | |
# Allow running nvm immediately from login shell | |
# | |
export NVM_DIR="$HOME/.nvm" | |
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" | |
} | |
install_nvm() { | |
brew install nvm | |
success "nvm Installed" | |
configure_nvm | |
install_node | |
} | |
setup_node() { | |
if [ ! "$(is_program_installed 'nvm')" ]; then | |
install_nvm | |
else | |
install_node | |
fi | |
} | |
############################# | |
# | |
# Appium | |
# | |
############################# | |
# Append some ENV vars to your shell profile | |
append_to_profile() { | |
echo -e "$(cat $1) | |
export JAVA_HOME=$(/usr/libexec/java_home) | |
export ANDROID_HOME=~/Library/Android/sdk | |
export PATH=\$ANDROID_HOME/build-tools:\$ANDROID_HOME/platform-tools:\$ANDROID_HOME/tools:\$JAVA_HOME/bin:\$PATH | |
" > $1 | |
} | |
install_appium() { | |
# accomodate bash and zsh users | |
append_to_profile ~/.bash_profile | |
append_to_profile ~/.zshrc | |
npm uninstall -g appium | |
success "Appium installed" | |
} | |
setup_appium() { | |
if [ ! "$(is_program_installed 'appium')" ]; then | |
yes_no "Would you like to install Appium and configure your ~/.bash_profile (or ~/.zshrc)?" "install_appium" "echo 'Skipping appium install'" | |
fi | |
} | |
############################# | |
# | |
# iOS | |
# | |
############################# | |
install_ios() { | |
if [ ! "$(is_program_installed 'carthage')" ]; then | |
brew install carthage | |
fi | |
carthage update | |
cocoapods_installed=$(gem list cocoapods) | |
if [ -z "$cocoapods_installed" ]; then | |
sudo gem install cocoapods | |
fi | |
if [ ! "$(is_program_installed 'apollo')" ]; then | |
npm install -g [email protected] | |
fi | |
success "iOS toolset installed" | |
} | |
setup_ios() { | |
yes_no "Would you like to install utilities for iOS/XCode?" "install_ios" "echo 'Skipping iOS utily install'" | |
} | |
############################# | |
# | |
# JMeter / Taurus | |
# | |
############################# | |
setup_jmeter_taurus() { | |
if [ ! "$(is_program_installed 'jmeter')" ]; then | |
brew install jmeter | |
fi | |
success "JMeter installed" | |
if [ ! "$(is_program_installed 'bzt')" ]; then | |
brew install bzt | |
fi | |
success "Taurus (bzt) installed" | |
} | |
############################# | |
# | |
# AWS CLI | |
# | |
############################# | |
AWS_CREDENTIALS_PATH=~/.aws/credentials | |
DEFAULT_AWS_CREDENTIALS_FILE=$(cat <<'AWS' | |
[default] | |
aws_access_key_id = my_access_key_id | |
aws_secret_access_key = my_secret_access_key | |
region = us-east-1 | |
AWS | |
) | |
copy_default_aws_config() { | |
# if file doesn't exist or it is empty, copy default file contents to the path | |
if [ ! -f "$AWS_CREDENTIALS_PATH" ] || [ -z "$(cat $AWS_CREDENTIALS_PATH)" ]; then | |
echo -e "$DEFAULT_AWS_CREDENTIALS_FILE" > "$AWS_CREDENTIALS_PATH" | |
add_warning "Fetch your AWS credentials, then run 'aws configure'. | |
Ensure your $AWS_CREDENTIALS_PATH file is set up correctly" | |
else | |
add_warning "Your $AWS_CREDENTIALS_PATH file is non-empty. | |
Please ensure it is set up correctly" | |
fi | |
success "AWS credential defaults set up" | |
} | |
configure_aws_credentials() { | |
aws configure | |
copy_default_aws_config | |
success "AWS CLI configured" | |
} | |
install_aws() { | |
brew install awscli | |
yes_no "Do you know your AWS credentials and have them handy?" "configure_aws_credentials" "copy_default_aws_config" | |
} | |
setup_aws() { | |
if [ ! "$(is_program_installed 'aws')" ]; then | |
yes_no "Do you want to install the AWS CLI (recommended)?" "install_aws" "echo 'Skipping AWS CLI install'" | |
fi | |
} | |
############################# | |
# | |
# .env files | |
# | |
############################# | |
setup_dotenv() { | |
DEFAULT_ENV_FILE="DATABASE_URL=postgres:///" | |
for filename in ".env.development" ".env.test"; do | |
if [[ -s "$filename" ]]; then # file exists and has size greater than zero | |
success "$filename exists and has content" | |
else | |
# file does not exist or is empty and needs to be filled | |
echo $DEFAULT_ENV_FILE > $filename | |
success "$filename created" | |
fi | |
done | |
} | |
############################# | |
# | |
# Execute script | |
# | |
# This business | |
# {...} 2>&1 | tee -a $LOG_FILE | |
# prints the output to the terminal, and also appends it to a log file. | |
# References: https://stackoverflow.com/a/315113, https://stackoverflow.com/a/418899 | |
# | |
############################# | |
{ | |
setup_homebrew || add_error "Issue setting up homebrew. Please visit https://brew.sh/" | |
setup_postgresql || add_error "Issue setting up PostgreSQL." | |
setup_git || add_error "Issue setting up git. Please visit https://git-scm.com or https://formulae.brew.sh/formula/git" | |
setup_version_manager || add_error "Issue setting up Ruby version manager" | |
setup_node || add_error "Issue setting up node. Please visit https://nodejs.org/" | |
setup_appium || add_error "Issue setting up Appium" | |
setup_ios || add_error "Issue setting up iOS" | |
setup_jmeter_taurus || add_error "Issue setting up JMeter/Taurus" | |
setup_aws || add_error "Issue setting up AWS CLI" | |
setup_dotenv || add_error "Issue setting .env files" | |
print_messages | |
} 2>&1 | tee -a $LOG_FILE | |
echo | |
echo "Setup complete! Logs copied to $LOG_FILE" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment