module FeduxOrgStdlib::Command::RunCommand

Run command

Public Instance Methods

run_command(cmd, options = {}) click to toggle source

Execute command

@param [String] cmd

the command

@param [Hash] options

the options for command execution

@option options [Hash] env ({])

the environment variables for the command ('VAR' => 'CONTENT')

@option options [String] stdin (nil)

the string for stdin of the command

@option options [TrueClass,FalseClass] binmode (false)

should the stdin be read a binary or not

@return [CommandResult]

the result of the command execution

@return [CommandResult]

the result of the command execution
# 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