class TTY::Command

Constants

ExecuteError
RUBY

Path to the current Ruby

TimeoutExceeded
VERSION
WIN_PLATFORMS

Attributes

printer[R]

Public Class Methods

new(**options) click to toggle source

Initialize a Command object

@param [Hash] options @option options [IO] :output

the stream to which printer prints, defaults to stdout

@option options [Symbol] :printer

the printer to use for output logging, defaults to :pretty

@option options [Symbol] :dry_run

the mode for executing command

@api public

# File lib/tty/command.rb, line 54
def initialize(**options)
  @output = options.fetch(:output) { $stdout }
  @color   = options.fetch(:color) { true }
  @uuid    = options.fetch(:uuid) { true }
  @printer_name = options.fetch(:printer) { :pretty }
  @dry_run = options.fetch(:dry_run) { false }
  @printer = use_printer(@printer_name, color: @color, uuid: @uuid)
  @cmd_options = {}
  @cmd_options[:verbose] = options.fetch(:verbose, true)
  @cmd_options[:pty] = true if options[:pty]
  @cmd_options[:binmode] = true if options[:binmode]
  @cmd_options[:timeout] = options[:timeout] if options[:timeout]
end
record_separator() click to toggle source
# File lib/tty/command.rb, line 29
def self.record_separator
  @record_separator ||= $/
end
record_separator=(sep) click to toggle source
# File lib/tty/command.rb, line 33
def self.record_separator=(sep)
  @record_separator = sep
end
windows?() click to toggle source
# File lib/tty/command.rb, line 37
def self.windows?
  !!(RbConfig::CONFIG['host_os'] =~ WIN_PLATFORMS)
end

Public Instance Methods

dry_run?() click to toggle source

Check if in dry mode

@return [Boolean]

@public

# File lib/tty/command.rb, line 168
def dry_run?
  @dry_run
end
ruby(*args, &block) click to toggle source

Run Ruby interperter with the given arguments

@example

ruby %q{-e "puts 'Hello world'"}

@api public

# File lib/tty/command.rb, line 154
def ruby(*args, &block)
  options = args.last.is_a?(Hash) ? args.pop : {}
  if args.length > 1
    run(*([RUBY] + args + [options]), &block)
  else
    run("#{RUBY} #{args.first}", options, &block)
  end
end
run(*args, &block) click to toggle source

Start external executable in a child process

@example

cmd.run(command, [argv1, ..., argvN], [options])

@example

cmd.run(command, ...) do |result|
  ...
end

@param [String] command

the command to run

@param [Array] argv

an array of string arguments

@param [Hash] options

hash of operations to perform

@option options [String] :chdir

The current directory.

@option options [Integer] :timeout

Maximum number of seconds to allow the process
to run before aborting with a TimeoutExceeded
exception.

@option options [Symbol] :signal

Signal used on timeout, SIGKILL by default

@yield [out, err]

Yields stdout and stderr output whenever available

@raise [ExitError]

raised when command exits with non-zero code

@api public

# File lib/tty/command.rb, line 102
def run(*args, &block)
  cmd = command(*args)
  result = execute_command(cmd, &block)
  if result && result.failure?
    raise ExitError.new(cmd.to_command, result)
  end
  result
end
run!(*args, &block) click to toggle source

Start external executable without raising ExitError

@example

cmd.run!(command, [argv1, ..., argvN], [options])

@api public

# File lib/tty/command.rb, line 117
def run!(*args, &block)
  cmd = command(*args)
  execute_command(cmd, &block)
end
test(*args) click to toggle source

Execute shell test command

@api public

# File lib/tty/command.rb, line 144
def test(*args)
  run!(:test, *args).success?
end
wait(*args) click to toggle source

Wait on long running script until output matches a specific pattern

@example

cmd.wait 'tail -f /var/log/php.log', /something happened/

@api public

# File lib/tty/command.rb, line 128
def wait(*args)
  pattern = args.pop
  unless pattern
    raise ArgumentError, 'Please provide condition to wait for'
  end

  run(*args) do |out, _|
    raise if out =~ /#{pattern}/
  end
rescue ExitError
  # noop
end

Private Instance Methods

command(*args) click to toggle source

@api private

# File lib/tty/command.rb, line 175
def command(*args)
  cmd = Cmd.new(*args)
  cmd.update(**@cmd_options)
  cmd
end
execute_command(cmd, &block) click to toggle source

@api private

# File lib/tty/command.rb, line 182
def execute_command(cmd, &block)
  dry_run = @dry_run || cmd.options[:dry_run] || false
  @runner = select_runner(dry_run).new(cmd, @printer, &block)
  @runner.run!
end
find_printer_class(name) click to toggle source

Find printer class or fail

@raise [ArgumentError]

@api private

# File lib/tty/command.rb, line 206
def find_printer_class(name)
  const_name = name.to_s.split('_').map(&:capitalize).join.to_sym
  if const_name.empty? || !TTY::Command::Printers.const_defined?(const_name)
    raise ArgumentError, %(Unknown printer type "#{name}")
  end
  TTY::Command::Printers.const_get(const_name)
end
select_runner(dry_run) click to toggle source

@api private

# File lib/tty/command.rb, line 215
def select_runner(dry_run)
  if dry_run
    DryRunner
  else
    ProcessRunner
  end
end
use_printer(class_or_name, options) click to toggle source

@api private

# File lib/tty/command.rb, line 189
def use_printer(class_or_name, options)
  if class_or_name.is_a?(TTY::Command::Printers::Abstract)
    return class_or_name
  end

  if class_or_name.is_a?(Class)
    class_or_name
  else
    find_printer_class(class_or_name)
  end.new(@output, options)
end