Module: FeduxOrgStdlib::Command::RunCommand

Includes:
Environment
Included in:
FeduxOrgStdlib::Command
Defined in:
lib/fedux_org_stdlib/command/run_command.rb

Overview

Run command

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:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/fedux_org_stdlib/command/run_command.rb', line 36

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

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

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

  result
end