Module: Diffend::Shell

Defined in:
lib/diffend/shell.rb

Class Method Summary collapse

Class Method Details

.call(command_with_options, raise_on_invalid_exit: true) ⇒ Hash

Allows to execute shell commands and handle errors, etc later

(won't raise any errors but instead will catch all things)

Examples:

Run ls

SupportEngine::Shell.('ls') =>
{ stdout: "test.rb\n", stderr: '', exit_code: 0}

Parameters:

  • command_with_options (String)

    command that should be executed with all the arguments and options

  • raise_on_invalid_exit (Boolean) (defaults to: true)

    raise exception when exit code is not 0

Returns:

  • (Hash)

    hash with 3 keys describing output (stdout, stderr, exit_code)

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/diffend/shell.rb', line 19

def call(command_with_options, raise_on_invalid_exit: true)
  stdout_str, stderr_str, status = Open3.capture3(command_with_options)

  result = {
    stdout: stdout_str,
    stderr: stderr_str,
    exit_code: status.exitstatus
  }

  raise Diffend::Errors::FailedShellCommand, result.values.join(': ') \
    if raise_on_invalid_exit && result[:exit_code] != 0

  result
end

.call_in_path(path, command, raise_on_invalid_exit: true) ⇒ Hash

Returns hash with 3 keys describing output (stdout, stderr, exit_code).

Parameters:

  • path (String, Pathname)

    to a place where git repo is

  • command (String)

    that we want to execute in path context

  • raise_on_invalid_exit (Boolean) (defaults to: true)

    raise exception when exit code is not 0

Returns:

  • (Hash)

    hash with 3 keys describing output (stdout, stderr, exit_code)



38
39
40
41
# File 'lib/diffend/shell.rb', line 38

def call_in_path(path, command, raise_on_invalid_exit: true)
  command = ['cd', path.to_s, '&&', command]
  call(command.join(' '), raise_on_invalid_exit: raise_on_invalid_exit)
end