Last active
August 29, 2015 14:09
-
-
Save StasKoval/fa2c2d038b7217cb6056 to your computer and use it in GitHub Desktop.
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
# Run external command and abort on failure | |
def shell(*cmd) | |
return if cmd.empty? | |
prog = cmd.first.to_s.split.first | |
abort "#{prog}: command not found" if `which #{prog}`.empty? | |
system *cmd | |
abort "#{prog}: interrupted, signal #{$?.termsig}" if $?.signaled? | |
abort "#{prog}: execution failed, exit code #{$?.exitstatus}" unless $?.success? | |
end | |
# Example of task using method shell() | |
task :test_shell do | |
# print something | |
shell "echo run command from a string" | |
shell *%W{echo run command from an array, and preserve #{'s p a c e s'}} | |
# can be interrupted with Ctrl-C | |
shell 'sleep 10' | |
# should fail with code 51 | |
shell *%W{sh -c #{"exit 51"}} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment