Module: Libis::Tools::Command

Defined in:
lib/libis/tools/command.rb

Class Method Summary collapse

Class Method Details

.run(cmd, *opts) ⇒ Hash

Run an external program and return status, stdout and stderr.

Parameters:

  • cmd (String)

    program name

  • opts (Array<String>)

    optional list of command line arguments

Returns:

  • (Hash)

    a Hash with:

    • :status : the exit status of the command

    • :out : the stdout output of the command

    • :err : the stderr output of the command



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/libis/tools/command.rb', line 18

def self.run(cmd, *opts)
  result = {
      status: 999,
      out: [],
      err: []
  }
  begin
    Open3.popen3(cmd, *opts) do |_, output, error, thread|
      output = output.read
      error = error.read
      result[:out] = output.split("\n").map(&:chomp)
      result[:err] = error.split("\n").map(&:chomp)
      result[:status] = thread.value.exitstatus rescue nil
    end

  rescue StandardError => e
    result[:err] = [e.class.name, e.message]

  end

  result

end