Module: Cinnabar::Command::ArrExt

Defined in:
lib/cinnabar/cmd_runner.rb

Overview

The foundation of ArrRefin and ArrMixin

See Also:

Instance Method Summary collapse

Instance Method Details

#async_run(env_hash = nil, opts: {}) ⇒ Array(IO, Process::Waiter)

Note:

self [Array<String>]: The command and its arguments (e.g., %w[printf hello]).

Starts a command asynchronously using this Array<String>.

Examples:

pass stdin data


using Cinnabar::Command::ArrRefin
# OR: include Cinnabar::Command::ArrMixin

opts = {stdin_data: "Hello\nWorld\n"}
io_and_waiter = %w[wc -l].async_run(opts:)
output, status = Cinnabar::Command.wait_with_output *io_and_waiter
output.to_i == 2 #=> true

Parameters:

  • env_hash (#to_h) (defaults to: nil)

    Optional environment variables.

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

Returns:

  • (Array(IO, Process::Waiter))

    a pair [stdout_io, waiter]

See Also:



350
351
352
# File 'lib/cinnabar/cmd_runner.rb', line 350

def async_run(env_hash = nil, opts: {})
  Cinnabar::Command.async_run(self, env_hash, opts:)
end

#run(env_hash = nil, opts: {}) ⇒ String?

Note:

self [Array<String>]: The command and its arguments (e.g., %w[printf hello]).

Note:

This method blocks until the process completes.

Executes the command synchronously (blocking) and returns its standard output.

Examples:

pass stdin data


using Cinnabar::Command::ArrRefin
# OR: include Cinnabar::Command::ArrMixin

opts = {allow_failure: true, stdin_data: "Hello\nWorld\n"}
output = %w[wc -l].run(opts:)
output.to_i == 2 unless output.nil? #=> true

Parameters:

  • env_hash (#to_h) (defaults to: nil)

    Environment variables to pass to the command.

  • opts (Hash) (defaults to: {})
    • Only the :allow_failure is extracted and handled explicitly;
    • all other keys are passed through to Open3.capture2 unchanged.

Returns:

  • (String, nil)

    the standard output of the command.

Raises:

  • (StandardError)

    when allow_failure: false and the process exits with non-zero status

See Also:



326
327
328
# File 'lib/cinnabar/cmd_runner.rb', line 326

def run(env_hash = nil, opts: {})
  Cinnabar::Command.run(self, env_hash, opts:)
end

#run_cmd(env_hash = nil, opts: {}) ⇒ Boolean

Note:

self [Array<String>]: The command and its arguments (e.g., %w[printf hello]).

Examples:

pwd


using Cinnabar::Command::ArrRefin
# OR: include Cinnabar::Command::ArrMixin

opts = {chdir: '/tmp', allow_failure: true}
status = %w[pwd].run_cmd(opts:)

pass env


using Cinnabar::Command::ArrRefin

env_hash = {WW: 2}
status =
   %w[sh -c]
     .push('printf $WW')
     .run_cmd(env_hash)

status == true

Returns:

  • (Boolean)

See Also:



378
379
380
# File 'lib/cinnabar/cmd_runner.rb', line 378

def run_cmd(env_hash = nil, opts: {}) # rubocop:disable Style/OptionalBooleanParameter
  Cinnabar::Command.run_cmd(self, env_hash, opts:)
end