Skip to content

Instantly share code, notes, and snippets.

@michaelward82
Last active March 5, 2016 22:47
Show Gist options
  • Save michaelward82/c1903f2b37a76975740e to your computer and use it in GitHub Desktop.
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
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
@michaelward82
Copy link
Author

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.

@michaelward82
Copy link
Author

Example output using exe function, with no errors:
Example output using exe function, with  errors

Example output using exe function, with error:
Example output using exe function, with error

Default output from executing commands directly, no actual errors:
Default output from executing commands directly, no actual errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment