Last active
March 5, 2016 22:47
-
-
Save michaelward82/c1903f2b37a76975740e to your computer and use it in GitHub Desktop.
Helper function to create prettier and more meaningful vagrant provisioning script output. https://github.com/michaelward82/vagrant-provisioning-shell-function-helper for project
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
exe () { | |
MESSAGE_PREFIX="\b\b\b\b\b\b\b\b\b\b" | |
echo -e "$MESSAGE_PREFIX Execute: $1" | |
LOOP=0 | |
while true; | |
do | |
if ! [ $LOOP == 0 ]; then echo -e "$MESSAGE_PREFIX ... "; fi; | |
sleep 3; | |
LOOP=$((LOOP+1)) | |
done & ERROR=$("${@:2}" 2>&1) | |
status=$? | |
kill $!; trap 'kill $!' SIGTERM | |
if [ $status -ne 0 ]; | |
then | |
echo -e "$MESSAGE_PREFIX ✖ Error" >&2 | |
echo -e "$ERROR" >&2 | |
else | |
echo -e "$MESSAGE_PREFIX ✔ Success" | |
fi | |
return $status | |
} | |
# Use as: | |
# exe "Update apt indexes" sudo apt-get update | |
# exe "Disable default vhost" sudo a2dissite 000-default | |
# | |
# Output: | |
# | |
# ==> Execute: Update apt indexes | |
# ==> ... | |
# ==> ... | |
# ==> ... | |
# ==> ✔ Success | |
# ==> Execute: Disable default vhost | |
# ==> ✔ Success |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The intention of this script is to reduce inaccurate red messages shown by vagrant when commands output to stderr. As not all output to stderr is an error, it can prove difficult to spot genuine errors in your provisioning script.
The exe function allows a command to be executed (coupled with a 3 second looping progress indicator for slower processes), with all progress output sent to stdout and showing green in vagrant. In the event of a command returning a non zero status, an indicator of failure in unix, the output of the command is send to stderr - showing red in vagrant.