Module: Aggkit::Exec

Extended by:
Exec
Included in:
Exec
Defined in:
lib/aggkit/exec.rb

Instance Method Summary collapse

Instance Method Details

#capture(cmd) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/aggkit/exec.rb', line 14

def capture(cmd)
  puts "Capturing: #{cmd}"
  io = IO.popen(cmd)
  io.read.strip
ensure
  io.close rescue nil
end

#capture!(cmd, error = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/aggkit/exec.rb', line 22

def capture!(cmd, error = nil)
  output = capture(cmd)
  error = error ? "#{error}: Execution failed" : 'Execution failed'
  die(error) unless ($?.success? rescue nil)

  output
rescue Exception => e
  error = error ? "#{error}: #{e.inspect}" : "Execution failed: #{e.inspect}"
  die(error)
end

#die(message, code: 1) ⇒ Object



9
10
11
12
# File 'lib/aggkit/exec.rb', line 9

def die(message, code: 1)
  error(message)
  exit!(code)
end

#error(message) ⇒ Object



5
6
7
# File 'lib/aggkit/exec.rb', line 5

def error(message)
  STDERR.puts "Error: #{message}"
end

#execute!(cmd, error = nil) ⇒ Object



33
34
35
36
37
# File 'lib/aggkit/exec.rb', line 33

def execute!(cmd, error = nil)
  puts "Executing: #{cmd}"
  error = error ? "#{error}: Execution failed" : 'Execution failed'
  die(error) unless system(cmd)
end