Class: Cliver::ShellCapture

Inherits:
Object
  • Object
show all
Defined in:
lib/cliver/shell_capture.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command) ⇒ void

Parameters:

  • command (Array<String>)

    the command to run; elements in the supplied array will be shelljoined.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cliver/shell_capture.rb', line 13

def initialize(command)
  command = command.shelljoin if command.kind_of?(Array)
  @stdout = @stderr = ''
  begin
    Open3.popen3(command) do |i, o, e|
      @stdout = o.read.chomp
      @stderr = e.read.chomp
    end
    # Fix for ruby 1.8.7 (and probably earlier):
    # Open3.popen3 does not raise anything there, but the error goes to STDERR.
    if @stderr =~ /open3.rb:\d+:in `exec': No such file or directory -.*\(Errno::ENOENT\)/ or
       @stderr =~ /An exception occurred in a forked block\W+No such file or directory.*\(Errno::ENOENT\)/
      @stderr = ''
      @command_found = false
    else
      @command_found = true
    end
  rescue Errno::ENOENT, IOError
    @command_found = false
  end
end

Instance Attribute Details

#command_foundObject (readonly)

Returns the value of attribute command_found.



5
6
7
# File 'lib/cliver/shell_capture.rb', line 5

def command_found
  @command_found
end

#stderrObject (readonly)

Returns the value of attribute stderr.



5
6
7
# File 'lib/cliver/shell_capture.rb', line 5

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



5
6
7
# File 'lib/cliver/shell_capture.rb', line 5

def stdout
  @stdout
end