Skip to content

Instantly share code, notes, and snippets.

@nicholaides
Created February 25, 2018 18:59
Show Gist options
  • Save nicholaides/1ad0b65ebeb11a76ea806fbb7434d002 to your computer and use it in GitHub Desktop.
Save nicholaides/1ad0b65ebeb11a76ea806fbb7434d002 to your computer and use it in GitHub Desktop.
Like FileUtils#sh but lets you specify a file to redirect output to. Helpful for building rake tasks.
# Like sh, but
# 1. lets you specify a file to redirect output to and
# 2. removes the file if there's an error
def sh_redirect(*args, out:)
sh(*args, { out: out }, {}) do |ok, status|
unless ok
rm_f out
raise "Command failed: `#{args.join(' ')}`\nDeleted `#{out}`"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment