Simple but effective executioner of commands, which will deal correctly with failed commands.
There are two versions of the Dotanuki gem dotanuki which uses posix-spawn and a pure ruby version dotanuki-ruby which uses open4 instead.
Note that if the pure ruby version can load posix-spawn it will use it.
In the following example, if the mkdir
fails, none of the other commands will be executed.
require "dotanuki" class Example include Dotanuki def test commands = [ "mkdir /tmp/foo", "cp /etc/hosts /tmp/foo", "cp /etc/passwd /tmp/foo" ] result = execute(commands) if result.failed? puts "execution failed: #{result.fail_message}" end end end
It can also be used with a guard
block, which will raise an ExecError
if a command fails.
require "dotanuki" class Example include Dotanuki def test guard do execute "mkdir /tmp/foo" execute "cp /etc/hosts /tmp/foo" execute "cp /etc/passwd /tmp/foo" end end end
If you want to use dotanuki in a class method, you have to use the module method
require "dotanuki" class Example def self.test? Dotanuki.execute("mkdir /tmp/foo").ok? end end