Module: HalfShell

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

Defined Under Namespace

Classes: ExecuteError, Result

Constant Summary collapse

VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.execute(*args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/half_shell.rb', line 19

def execute(*args)
  opts = args.last.is_a?(Hash) ? args.pop : {}
  opts[:raise] = true unless opts.has_key? :raise
  
  command = args.join(' ')
  
  stdout, stderr = nil
  status = Open4.popen4(command) do |pid, _stdin, _stdout, _stderr|
    stdout = _stdout.read
    stderr = _stderr.read
  end
  
  if opts[:raise] && status.exitstatus != 0
    raise ExecuteError.new command, status.exitstatus
  end
  
  Result.new command, status.exitstatus, stdout, stderr
end

.execute!(*args) ⇒ Object



38
39
40
41
42
# File 'lib/half_shell.rb', line 38

def execute!(*args)
  command = args.join(' ')
  `#{command}`
  $?.success?
end