Last active
December 18, 2015 16:29
-
-
Save nu7hatch/5811607 to your computer and use it in GitHub Desktop.
Bash utilities for setup scripts.
This file contains hidden or 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
# _utils.bash --- Utilities used across all the scripts. | |
ERROR_FILE="/tmp/setup_script_error.msg" | |
# Checks if user has cowsay installed, and if it does | |
# then uses it to display the message. | |
cowsay_or_echo() | |
{ | |
cowsay=`which cowsay` | |
cowsay_eyes=$1; shift | |
if [ "$cowsay" != "" ]; then | |
echo $@ | $cowsay -e $cowsay_eyes -W 60 | |
else | |
echo $@ | |
fi | |
} | |
# A wrapper for commands. If command fail, then clear | |
# error message will be displayed and execution stopped. | |
# | |
# Usage: | |
# | |
# setup assert_command ruby git | |
# setup assert_ruby_version 1.9.3 | |
# | |
setup() | |
{ | |
$@ 2>&1 | sed -e "s/^/ /" | |
if [ -f $ERROR_FILE ]; then | |
echo -e "-----> Aborting...\n" | |
echo -e "\033[31m$(cowsay_or_echo xX $(cat $ERROR_FILE))\033[0m\n" | |
rm -f $ERROR_FILE | |
exit 1 | |
fi | |
} | |
# Stores information about the current error. | |
set_error() | |
{ | |
cat $@ > $ERROR_FILE | |
} | |
# Displays final success message. | |
success() | |
{ | |
msg=`cat $@` | |
echo -e "\033[32m$(cowsay_or_echo ^^ $msg)\033[0m\n" | |
} | |
# Checks if command in the first agument is available | |
# in the host system. | |
# | |
# Usage: | |
# | |
# assert_command git | |
# | |
assert_command() | |
{ | |
cmd="$1" | |
err="" | |
command -v $cmd >/dev/null 2>&1 || err=1 | |
if [ $err ]; then | |
echo "$cmd: Command required but not found. Aborting." | |
set_error <<EOF | |
In order to continue you have to install a package which provides | |
the $cmd command. | |
EOF | |
return 1 | |
fi | |
echo "$cmd: $(command -v $cmd)"; | |
} | |
# Checks if all specified commands are available in the | |
# host system. | |
# | |
# Usage: | |
# | |
# assert_command git ruby mysql | |
# | |
assert_commands() | |
{ | |
commands="$@" | |
for cmd in $commands; do | |
assert_command $cmd || break | |
done | |
} | |
# Checks if ruby version is correct. | |
# | |
# Usage: | |
# | |
# assert_ruby_version 1.9.3 | |
# | |
assert_ruby_version() | |
{ | |
version=$1 | |
ruby -e 'RUBY_VERSION >= "$1" or exit 1' 2>&1; err=$? | |
ruby -v | |
if [ $err -ne 0 ]; then | |
set_error <<EOF | |
In order to continue you must install ruby in version $version or higher. | |
EOF | |
return 1 | |
fi | |
} | |
# Works the same as assert_ruby_version but checks the version | |
# of installed python interpreter. | |
assert_python_version() { | |
version=$1 | |
ruby -e '`python --version 2>&1`.split.last >= "$1" or exit 1' 2>&1; err=$? | |
python --version | |
if [ $err -ne 0 ]; then | |
set_error <<EOF | |
In order to continue you must install python in version $version or higher. | |
EOF | |
fi | |
} | |
# Installs ruby dependencies via bundler. | |
install_dependencies_via_bundler() | |
{ | |
bundle install | |
if [ $? -ne 0 ]; then | |
set_error <<EOF | |
Bundler failed to install gem dependencies. | |
EOF | |
fi | |
} | |
# Checks if there's an .env file in the root directory, and if | |
# not, it makes a copy of .env.sample and lets user edit its | |
# content. | |
edit_env_configs() | |
{ | |
if [ "$EDITOR" == "" ]; then | |
echo "Editor is not set." | |
set_error <<-EOF | |
You don't have editor set. Set it like this: | |
$ export EDITOR=emacs | |
You may want to add this line to your ~/.bashrc file. You can also | |
run this script with inline EDITOR variable. | |
EOF | |
return | |
fi | |
if [ ! -f .env ]; then | |
cp .env.sample .env | |
echo "File doesn't exist: .env, copying from .env.sample" | |
echo "Opening editor..." | |
$EDITOR .env || true | |
else | |
echo "File exists: .env" | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment