blue_shell 0.0.2 → 0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/blue_shell/bash.rb +22 -14
- data/lib/blue_shell/version.rb +1 -1
- metadata +1 -1
data/lib/blue_shell/bash.rb
CHANGED
@@ -12,7 +12,7 @@ module BlueShell
|
|
12
12
|
# Execute the given command. Will block until the command completes.
|
13
13
|
# param [String] the command to execute in bash
|
14
14
|
# param [Integer] the number of seconds to wait for it to complete
|
15
|
-
def execute!(command, timeout =
|
15
|
+
def execute!(command, timeout = 30)
|
16
16
|
# Executing via a file seems clunky, but it is the best way to
|
17
17
|
# ensure everything happens exactly as we expect it to without
|
18
18
|
# trying to deal with escaping the world properly.
|
@@ -22,10 +22,19 @@ module BlueShell
|
|
22
22
|
file.flush
|
23
23
|
|
24
24
|
Timeout::timeout(timeout) do
|
25
|
-
|
26
|
-
|
27
|
-
@
|
28
|
-
@
|
25
|
+
proc = runtime.exec("/bin/bash #{file.path}")
|
26
|
+
|
27
|
+
@out = ""
|
28
|
+
@err = ""
|
29
|
+
|
30
|
+
# Must have a sink for stdout for the proc to exit
|
31
|
+
out_thread = read_thread(proc.getInputStream, @out)
|
32
|
+
err_thread = read_thread(proc.getErrorStream, @err)
|
33
|
+
|
34
|
+
out_thread.join
|
35
|
+
err_thread.join
|
36
|
+
|
37
|
+
@exit = proc.exitValue
|
29
38
|
end
|
30
39
|
ensure
|
31
40
|
file.close
|
@@ -33,18 +42,17 @@ module BlueShell
|
|
33
42
|
end
|
34
43
|
end
|
35
44
|
|
36
|
-
def
|
37
|
-
|
38
|
-
|
39
|
-
|
45
|
+
def read_thread(stream, sink)
|
46
|
+
Thread.new(stream, sink) do |stream, sink|
|
47
|
+
line = nil
|
48
|
+
br = BufferedReader.new(InputStreamReader.new(stream))
|
40
49
|
|
41
|
-
|
42
|
-
|
50
|
+
while(line = br.readLine) do
|
51
|
+
sink << line
|
52
|
+
end
|
43
53
|
end
|
44
|
-
|
45
|
-
lines.join("\n")
|
46
54
|
end
|
47
|
-
private :
|
55
|
+
private :read_thread
|
48
56
|
|
49
57
|
def runtime
|
50
58
|
@runtime ||= java.lang.Runtime.getRuntime
|
data/lib/blue_shell/version.rb
CHANGED