Module: LVM::External

Defined in:
lib/lvm/external.rb

Defined Under Namespace

Classes: ExternalFailure

Class Method Summary collapse

Class Method Details

.cmd(cmd) ⇒ Object



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
# File 'lib/lvm/external.rb', line 8

def cmd(cmd)
  output = []
  error = nil
  stat = Open4.popen4(cmd) do |pid, stdin, stdout, stderr|
    while (line = stdout.gets)
      output << line
    end
    error = stderr.read.strip
  end
  if stat.exited?
    if stat.exitstatus > 0
      raise ExternalFailure, "Fatal error, `#{cmd}` returned #{stat.exitstatus} with '#{error}'"
    end
  elsif stat.signaled?
    raise ExternalFailure, "Fatal error, `#{cmd}` got signal #{stat.termsig} and terminated"
  elsif stat.stopped?
    raise ExternalFailure, "Fatal error, `#{cmd}` got signal #{stat.stopsig} and is stopped"
  end

  if block_given?
    output.each { |l| yield l }
  else
    output.join
  end
end