Class: Arh
- Inherits:
-
Object
- Object
- Arh
- Defined in:
- lib/arh.rb
Constant Summary collapse
- VERSION =
'0.1.0'
Instance Attribute Summary collapse
-
#cmd ⇒ Object
readonly
Returns the value of attribute cmd.
-
#end_at ⇒ Object
readonly
Returns the value of attribute end_at.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#exit_level ⇒ Object
readonly
Returns the value of attribute exit_level.
-
#output ⇒ Object
readonly
Returns the value of attribute output.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#start_at ⇒ Object
readonly
Returns the value of attribute start_at.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Class Method Summary collapse
-
.execute(shell_command, options = {}) ⇒ Object
Arh.execute.
- .execute!(shell_command, options = {}) ⇒ Object
Instance Method Summary collapse
- #execute ⇒ Object
- #execute! ⇒ Object
-
#initialize(shell_command, options = {}) ⇒ Arh
constructor
A new instance of Arh.
- #success? ⇒ Boolean
Constructor Details
#initialize(shell_command, options = {}) ⇒ Arh
Returns a new instance of Arh.
59 60 61 62 63 |
# File 'lib/arh.rb', line 59 def initialize(shell_command, = {}) raise ArgumentError, "no command given" unless shell_command @cmd = shell_command @pipe = [:pipe] end |
Instance Attribute Details
#cmd ⇒ Object (readonly)
Returns the value of attribute cmd.
12 13 14 |
# File 'lib/arh.rb', line 12 def cmd @cmd end |
#end_at ⇒ Object (readonly)
Returns the value of attribute end_at.
11 12 13 |
# File 'lib/arh.rb', line 11 def end_at @end_at end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
14 15 16 |
# File 'lib/arh.rb', line 14 def errors @errors end |
#exit_level ⇒ Object (readonly)
Returns the value of attribute exit_level.
15 16 17 |
# File 'lib/arh.rb', line 15 def exit_level @exit_level end |
#output ⇒ Object (readonly)
Returns the value of attribute output.
13 14 15 |
# File 'lib/arh.rb', line 13 def output @output end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
16 17 18 |
# File 'lib/arh.rb', line 16 def pid @pid end |
#start_at ⇒ Object (readonly)
Returns the value of attribute start_at.
10 11 12 |
# File 'lib/arh.rb', line 10 def start_at @start_at end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
17 18 19 |
# File 'lib/arh.rb', line 17 def status @status end |
Class Method Details
.execute(shell_command, options = {}) ⇒ Object
Arh.execute
Arh executes command with open4. Arh instance is returned with this attributes
- start_at: Time instance storing when command was started
- end_at: Time instance storing when command was ended
- cmd: Command executed
- output: Returns String with output at standart output
- errors: Returns String with output at errors output
- exit_evel: Returns interger command exit level
- pid: assigned process identificator
- status: Process status object
User example
a = Arh.execute('ls /tmp')
=> #<Arh:0xf7049dac @end_at=Thu Dec 30 18:13:41 +0000 2010, @cmd="ls /tmp", @status=#<Process::Status: pid=10863,exited(0)>, @exit_level=0, @pid=10863, @start_at=Thu Dec 30 18:13:41 +0000 2010, @errors="\n", @pipe=nil, @output="test">
a.exit_level
>> a.exit_level
> 0
>> a = Arh.execute(‘ls /tmps’)
> #<Arh:0xf7041990 @end_at=Thu Dec 30 18:15:21 0000 2010, @cmd=“ls /tmps”, @status=#<Process::Status: pid=10871,exited(2)>, @exit_level=2, @pid=10871, @start_at=Thu Dec 30 18:15:21 0000 2010, @errors=“ls: no se puede acceder a /tmps: No existe el fichero o el directorion”, @pipe=nil, @output=“n”>
>> a.errors
> “ls: no se puede acceder a /tmps: No existe el fichero o el directorion”
>> a.exit_level
> 2
46 47 48 |
# File 'lib/arh.rb', line 46 def self.execute(shell_command, = {}) new(shell_command, ).execute end |
.execute!(shell_command, options = {}) ⇒ Object
50 51 52 |
# File 'lib/arh.rb', line 50 def self.execute!(shell_command, = {}) new(shell_command, ).execute! end |
Instance Method Details
#execute ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/arh.rb', line 65 def execute # trap_save = trap( 0, "IGNORE" ) @start_at = Time.now read_stdout, write_stdout = IO.pipe read_stderr, write_stderr = IO.pipe pid = fork do # trap( 0, "IGNORE" ) read_stdout.close read_stderr.close cmd = @cmd cmd = "#{ @pipe }|#{ cmd }" if @pipe pid_open4, stdin, stdout, stderr = Open4::popen4 "#{ cmd }" write_stdout.puts stdout.read write_stderr.puts stderr.read ignored, status_open4 = Process::waitpid2 pid_open4 exit status_open4.exitstatus end # trap( 0, trap_save ) write_stdout.close write_stderr.close result_stdout = read_stdout.read result_stderr = read_stderr.read @pid, @status = Process.wait2(pid) @output = result_stdout @errors = result_stderr @exit_level = status.exitstatus @pid = pid @status = status @end_at = Time.now self end |
#execute! ⇒ Object
54 55 56 57 |
# File 'lib/arh.rb', line 54 def execute! @raise_on_fail = true execute end |
#success? ⇒ Boolean
110 111 112 |
# File 'lib/arh.rb', line 110 def success? @exit_level.zero? end |