module SafeShell

Constants

VERSION

Public Class Methods

execute(command, *args) click to toggle source
# File lib/safe_shell.rb, line 4
def self.execute(command, *args)
  opts = args.last.kind_of?(Hash) ? args.pop : {}
  read_end, write_end = IO.pipe
  new_stdout = opts[:stdout] ? File.open(opts[:stdout], "w+") : write_end
  new_stderr = opts[:stderr] ? File.open(opts[:stderr], "w+") : write_end
  env = opts[:env]
  opts = {:in => read_end, :out => new_stdout, :err => new_stderr}

  pid = if env
    spawn(env, command, *(args.map { |a| a.to_s }), opts)
  else
    spawn(command, *(args.map { |a| a.to_s }), opts)
  end

  write_end.close
  output = read_end.read
  Process.waitpid(pid)
  read_end.close
  output
end
execute!(*args) click to toggle source
# File lib/safe_shell.rb, line 30
def self.execute!(*args)
  execute(*args).tap do
    raise_command_failed_exception(*args) unless $?.success?
  end
end
execute?(*args) click to toggle source
# File lib/safe_shell.rb, line 25
def self.execute?(*args)
  execute(*args)
  $?.success?
end

Private Class Methods

raise_command_failed_exception(*args) click to toggle source
# File lib/safe_shell.rb, line 38
def self.raise_command_failed_exception(*args)
  raise CommandFailedException.new("Shell command #{args.inspect} failed with status #{$?}")
end