Module: SafeShell

Defined in:
lib/safe_shell.rb,
lib/safe_shell/version.rb

Defined Under Namespace

Classes: CommandFailedException

Constant Summary collapse

VERSION =
"1.0.3"

Class Method Summary collapse

Class Method Details

.execute(command, *args) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# 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
  opts = {:in => read_end, :out => new_stdout, :err => new_stderr}
  pid = spawn(command, *(args.map { |a| a.to_s }), opts)
  write_end.close
  output = read_end.read
  Process.waitpid(pid)
  read_end.close
  output
end

.execute!(*args) ⇒ Object



23
24
25
26
27
# File 'lib/safe_shell.rb', line 23

def self.execute!(*args)
  execute(*args).tap do
    raise_command_failed_exception(*args) unless $?.success?
  end
end

.execute?(*args) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
# File 'lib/safe_shell.rb', line 18

def self.execute?(*args)
  execute(*args)
  $?.success?
end