Method: Geet::Helpers::OsHelper#execute_command

Defined in:
lib/geet/helpers/os_helper.rb

#execute_command(command, description: nil, interactive: false, silent_stderr: false) ⇒ Object

Executes the command.

If the command doesn’t execute successfully, it will raise an error.

On non-interactive runs, the stdout content is returned, stripped of the surrounding whitespaces.

description: optional string, to make the error clearer. interactive: set when required; in this case, a different API will be used (‘system()`

instead of `popen3`).

silent_stderr: don’t print the stderr output



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/geet/helpers/os_helper.rb', line 30

def execute_command(command, description: nil, interactive: false, silent_stderr: false)
  description_message = " on #{description}" if description

  if interactive
    system(command)

    if !$CHILD_STATUS.success?
      raise "Error#{description_message} (exit status: #{$CHILD_STATUS.exitstatus})"
    end
  else
    Open3.popen3(command) do |_, stdout, stderr, wait_thread|
      stdout_content = stdout.read
      stderr_content = stderr.read

      puts stderr_content if stderr_content != '' && !silent_stderr

      if !wait_thread.value.success?
        error_message = stderr_content.lines.first.strip
        raise "Error#{description_message}: #{error_message}"
      end

      stdout_content.strip
    end
  end
end