Class: Rye::Rap

Inherits:
Array
  • Object
show all
Defined in:
lib/rye/rap.rb

Overview

Rye::Rap

This class is a modified Array which is returned by all command methods. The command output is split by line into an instance of this class. If there is only a single element it will act like a String.

This class also contains a reference to the instance of Rye::Box or Rye::Set that the command was executed on.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, *args) ⇒ Rap

  • obj an instance of Rye::Box or Rye::Set

  • args anything that can sent to Array#new



33
34
35
36
37
38
# File 'lib/rye/rap.rb', line 33

def initialize(obj, *args)
  @obj = obj
  @exit_status = -1
  @stderr = []
  super *args
end

Instance Attribute Details

#cmdObject

The command that was executed.



29
30
31
# File 'lib/rye/rap.rb', line 29

def cmd
  @cmd
end

#exit_signalObject

Returns the value of attribute exit_signal.



26
27
28
# File 'lib/rye/rap.rb', line 26

def exit_signal
  @exit_signal
end

#exit_statusObject (readonly)

Returns the value of attribute exit_status.



23
24
25
# File 'lib/rye/rap.rb', line 23

def exit_status
  @exit_status
end

#objObject (readonly) Also known as: box, set

A reference to the Rye object instance the command was executed by (Rye::Box or Rye::Set)



19
20
21
# File 'lib/rye/rap.rb', line 19

def obj
  @obj
end

#pidObject (readonly)

Only populated when calling via Rye::Shell



25
26
27
# File 'lib/rye/rap.rb', line 25

def pid
  @pid
end

#stderrObject (readonly)

An array containing any STDERR output



22
23
24
# File 'lib/rye/rap.rb', line 22

def stderr
  @stderr
end

Instance Method Details

#>(path) ⇒ Object

Output STDOUT content to (remote) path This works like a shell redirect so the file contents are cleared before outputting.

rbox.ps('aux') > 'processes.log'


108
109
110
111
# File 'lib/rye/rap.rb', line 108

def >(path)
  self.obj.unsafely { rm path }
  self.obj.file_append(path, self)
end

#>>(path) ⇒ Object

Output STDOUT content to (remote) path This works like a shell redirect so if the target file exists the STDOUT content will be appended.

rbox.ps('aux') >> 'processes.log'


119
120
121
# File 'lib/rye/rap.rb', line 119

def >>(path)
  self.obj.file_append(path, self)
end

#add_exit_status(code) ⇒ Object

Parse the exit code.

  • code an exit code string or integer or Process::Status object

For example, when running a command via Rye.shell, this method is send $? which is Process::Status object. Via Rye::Box.run_command it’s just an exit code returned by Net::SSH.

In JRuby, if code is a Process::Status object, @pid will be set to -1 (JRuby doesn’t return the pid).

Returns the exit code as an Integer.



86
87
88
89
90
91
92
93
94
# File 'lib/rye/rap.rb', line 86

def add_exit_status(code)
  code = 0 if code.nil?
  if code.is_a?(Process::Status)
    @exit_status = code.exitstatus.to_i
    @pid = Rye.sysinfo.vm == :java ? '-1' : code.pid
  else
    @exit_status = code.to_i
  end
end

#add_stderr(*args) ⇒ Object

Add STDERR output from the command executed via SSH.



56
57
58
59
60
61
62
63
# File 'lib/rye/rap.rb', line 56

def add_stderr(*args)
  args = args.flatten.compact
  args = args.first.split($/) if args.size == 1
  @stderr ||= []
  @stderr << args
  @stderr.flatten!
  self
end

#add_stdout(*args) ⇒ Object

Add STDOUT output from the command executed via SSH. This is available to maintain consistency with the add_stderr method. Otherwise there’s no need to use this method (treat the Rye::Rap object like an Array).



69
70
71
72
73
74
# File 'lib/rye/rap.rb', line 69

def add_stdout(*args)
  args = args.flatten.compact
  args = args.first.split($/) if args.size == 1
  self << args
  self.flatten!
end

#codeObject



95
# File 'lib/rye/rap.rb', line 95

def code; @exit_status; end

#inspectObject



43
44
45
# File 'lib/rye/rap.rb', line 43

def inspect
  "[%s, %s, %s, %s]" % [self.join("").tr("\r", ''), @stderr.join("; ").tr("\r", ''), exit_status, exit_signal]
end

#stdoutObject

Returns a reference to the Rye::Rap object (which acts like an Array that contains the STDOUT from the command executed over SSH). This is available to maintain consistency with the stderr method.



51
52
53
# File 'lib/rye/rap.rb', line 51

def stdout
  self
end

#to_sObject

Returns the first element if there’s only the one, an empty String if there’s none. Returns the value of self.join($/) otherwise.



100
# File 'lib/rye/rap.rb', line 100

def to_s; self.join $/; end