Module: BobTheHelper::Command

Includes:
Environment
Defined in:
lib/bob_the_helper/command.rb,
lib/bob_the_helper/command/command_result.rb

Defined Under Namespace

Classes: CommandResult

Instance Method Summary collapse

Methods included from Environment

#isolated_environment

Instance Method Details

#run_command(cmd, options = {}) ⇒ CommandResult

Execute command

Parameters:

  • cmd (String)

    the command

  • options (Hash) (defaults to: {})

    the options for command execution

Options Hash (options):

  • env (Hash) — default: {]

    the environment variables for the command (‘VAR’ => ‘CONTENT’)

  • stdin (String) — default: nil

    the string for stdin of the command

  • binmode (TrueClass, FalseClass) — default: false

    should the stdin be read a binary or not

Returns:



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bob_the_helper/command.rb', line 32

def run_command(cmd,options={})
  opts = {
    env: nil,
    stdin: nil,
    binmode: false,
  }.merge options

  env = opts[:env] || ENV.to_hash
  stdin = opts[:stdin] 
  binmode = opts[:binmode]

  result = CommandResult.new
  result.stdout, result.stderr, result.status = Open3.capture3(env, cmd, stdin_data: stdin, chdir: working_directory, binmode: binmode)

  result
end