Class: TTY::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/tty/command.rb,
lib/tty/command/cmd.rb,
lib/tty/command/result.rb,
lib/tty/command/version.rb,
lib/tty/command/truncator.rb,
lib/tty/command/dry_runner.rb,
lib/tty/command/exit_error.rb,
lib/tty/command/child_process.rb,
lib/tty/command/printers/null.rb,
lib/tty/command/printers/quiet.rb,
lib/tty/command/process_runner.rb,
lib/tty/command/printers/pretty.rb,
lib/tty/command/printers/abstract.rb,
lib/tty/command/printers/progress.rb

Defined Under Namespace

Modules: ChildProcess, Printers Classes: Cmd, DryRunner, ExitError, ProcessRunner, Result, Truncator

Constant Summary collapse

ExecuteError =
Class.new(StandardError)
TimeoutExceeded =
Class.new(StandardError)
RUBY =

Path to the current Ruby

ENV["RUBY"] || ::File.join(
  RbConfig::CONFIG["bindir"],
  RbConfig::CONFIG["ruby_install_name"] + RbConfig::CONFIG["EXEEXT"]
)
WIN_PLATFORMS =
/cygwin|mswin|mingw|bccwin|wince|emx/.freeze
VERSION =
"0.10.1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Command

Initialize a Command object

Parameters:

  • options (Hash)

Options Hash (**options):

  • :output (IO)

    the stream to which printer prints, defaults to stdout

  • :printer (Symbol)

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

  • :dry_run (Symbol)

    the mode for executing command



54
55
56
57
58
59
60
61
62
63
64
65
66
# 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

Instance Attribute Details

#printerObject (readonly)

Returns the value of attribute printer.



41
42
43
# File 'lib/tty/command.rb', line 41

def printer
  @printer
end

Class Method Details

.record_separatorObject



29
30
31
# File 'lib/tty/command.rb', line 29

def self.record_separator
  @record_separator ||= $/
end

.record_separator=(sep) ⇒ Object



33
34
35
# File 'lib/tty/command.rb', line 33

def self.record_separator=(sep)
  @record_separator = sep
end

.windows?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/tty/command.rb', line 37

def self.windows?
  !!(RbConfig::CONFIG["host_os"] =~ WIN_PLATFORMS)
end

Instance Method Details

#dry_run?Boolean

Check if in dry mode

Returns:

  • (Boolean)


168
169
170
# File 'lib/tty/command.rb', line 168

def dry_run?
  @dry_run
end

#ruby(*args, &block) ⇒ Object

Run Ruby interperter with the given arguments

Examples:

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


154
155
156
157
158
159
160
161
# 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) {|out, err| ... } ⇒ Object

Start external executable in a child process

Examples:

cmd.run(command, [argv1, ..., argvN], [options])
cmd.run(command, ...) do |result|
  ...
end

Parameters:

  • command (String)

    the command to run

  • argv (Array[String])

    an array of string arguments

  • options (Hash)

    hash of operations to perform

Yields:

  • (out, err)

    Yields stdout and stderr output whenever available

Raises:

  • (ExitError)

    raised when command exits with non-zero code



102
103
104
105
106
107
108
109
# 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) ⇒ Object

Start external executable without raising ExitError

Examples:

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


117
118
119
120
# File 'lib/tty/command.rb', line 117

def run!(*args, &block)
  cmd = command(*args)
  execute_command(cmd, &block)
end

#test(*args) ⇒ Object

Execute shell test command



144
145
146
# File 'lib/tty/command.rb', line 144

def test(*args)
  run!(:test, *args).success?
end

#wait(*args) ⇒ Object

Wait on long running script until output matches a specific pattern

Examples:

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


128
129
130
131
132
133
134
135
136
137
138
139
# 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