Class: BlueShell::Bash

Inherits:
Object
  • Object
show all
Defined in:
lib/blue_shell/bash.rb

Constant Summary collapse

BufferedReader =
java.io.BufferedReader
InputStreamReader =
java.io.InputStreamReader

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#errObject (readonly)

Returns the value of attribute err.



10
11
12
# File 'lib/blue_shell/bash.rb', line 10

def err
  @err
end

#exitObject (readonly)

Returns the value of attribute exit.



10
11
12
# File 'lib/blue_shell/bash.rb', line 10

def exit
  @exit
end

#outObject (readonly)

Returns the value of attribute out.



10
11
12
# File 'lib/blue_shell/bash.rb', line 10

def out
  @out
end

Instance Method Details

#execute!(command, timeout = 30) ⇒ Object

Execute the given command. Will block until the command completes. param [String] the command to execute in bash param [Integer] the number of seconds to wait for it to complete



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/blue_shell/bash.rb', line 15

def execute!(command, timeout = 30)
  # Executing via a file seems clunky, but it is the best way to
  # ensure everything happens exactly as we expect it to without 
  # trying to deal with escaping the world properly.
  file = Tempfile.new('command')
  begin
    file << command
    file.flush

    Timeout::timeout(timeout) do
      proc = runtime.exec("/bin/bash #{file.path}")

      @out = ""
      @err = ""

      # Must have a sink for stdout for the proc to exit
      out_thread = read_thread(proc.getInputStream, @out)
      err_thread = read_thread(proc.getErrorStream, @err)

      out_thread.join
      err_thread.join

      @exit = proc.exitValue
    end
  ensure
    file.close
    file.unlink
  end
end