Module: Jazzy::Executable

Defined in:
lib/jazzy/executable.rb

Defined Under Namespace

Classes: IO

Class Method Summary collapse

Class Method Details

.execute_command(executable, args, raise_on_failure, env: {}) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jazzy/executable.rb', line 20

def execute_command(executable, args, raise_on_failure, env: {})
  require 'shellwords'
  bin = `which #{executable.to_s.shellescape}`.strip
  raise "Unable to locate the executable `#{executable}`" if bin.empty?

  require 'open4'

  stdout = IO.new
  stderr = IO.new($stderr)

  options = { stdout: stdout, stderr: stderr, status: true }
  status  = Open4.spawn(env, bin, *args, options)
  unless status.success?
    full_command = "#{bin.shellescape} #{args.map(&:shellescape)}"
    output = stdout.to_s << stderr.to_s
    if raise_on_failure
      raise "#{full_command}\n\n#{output}"
    else
      warn("[!] Failed: #{full_command}")
    end
  end
  [stdout.to_s, status]
end