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_code = 0
  @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_codeObject (readonly)

Returns the value of attribute exit_code.



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

def exit_code
  @exit_code
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

#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'


104
105
106
107
# File 'lib/rye/rap.rb', line 104

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'


115
116
117
# File 'lib/rye/rap.rb', line 115

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

#add_exit_code(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.



82
83
84
85
86
87
88
89
90
# File 'lib/rye/rap.rb', line 82

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

#add_stderr(*args) ⇒ Object

Add STDERR output from the command executed via SSH.



52
53
54
55
56
57
58
59
# File 'lib/rye/rap.rb', line 52

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).



65
66
67
68
69
70
# File 'lib/rye/rap.rb', line 65

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

#codeObject



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

def code; @exit_code; 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.



47
48
49
# File 'lib/rye/rap.rb', line 47

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.



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

def to_s; self.join $/; end