Module: Guns

Defined in:
lib/guns.rb

Defined Under Namespace

Classes: Failure

Class Method Summary collapse

Class Method Details

.sh(cmd, *args) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/guns.rb', line 5

def sh(cmd, *args)
  env = args.pop if args.last.kind_of?(Hash)

  rout, wout = IO.pipe
  rerr, werr = IO.pipe

  # Disable GC before forking in an attempt to get some advantage
  # out of COW.

  GC.disable

  if fork
    # Parent
    wout.close
    werr.close

    Process.wait

    exitstatus = $?.exitstatus
    out = rout.read
    err = rerr.read

    [out, err, exitstatus]
  else
    # Child

    if env
      env.each {|k,v| ENV[k] = v}
    end

    STDOUT.reopen(wout)
    STDERR.reopen(werr)

    $0 = "#$0 -> [guns] #{cmd}"

    system(cmd, *args)

    exit! $?.exitstatus
  end

ensure
  GC.enable
end

.sh!(*args) ⇒ Object

Raises:



50
51
52
53
# File 'lib/guns.rb', line 50

def sh!(*args)
  out, err, code = sh(*args)
  raise Failure, err if code != 0
end