woof_util 0.0.0 → 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/woof_util.rb +30 -1
- metadata +1 -1
data/lib/woof_util.rb
CHANGED
@@ -1,5 +1,34 @@
|
|
1
1
|
class WoofUtil
|
2
|
-
|
2
|
+
|
3
|
+
# usage
|
4
|
+
# shellout "xargs", :stdin => "/etc", :args => ["ls", "-ld"]
|
5
|
+
def self.shellout(cmd, params = {})
|
6
|
+
params[:stdin] ||= ""
|
7
|
+
params[:args] ||= []
|
8
|
+
|
9
|
+
parent_r,child_w=IO.pipe ; parent_r.binmode ; child_w.binmode
|
10
|
+
child_r,parent_w=IO.pipe ; child_r.binmode ; parent_w.binmode
|
11
|
+
|
12
|
+
pid = fork do
|
13
|
+
parent_r.close
|
14
|
+
parent_w.close
|
15
|
+
$stdin.reopen child_r
|
16
|
+
$stdout.reopen child_w
|
17
|
+
$stderr.reopen child_w
|
18
|
+
# should be closing other open FDs, punting on it for now
|
19
|
+
exec *([cmd].concat params[:args])
|
20
|
+
end
|
21
|
+
child_w.close
|
22
|
+
child_r.close
|
23
|
+
buf = []
|
24
|
+
parent_w.write params[:stdin]
|
25
|
+
parent_w.close
|
26
|
+
while Process.waitpid(pid, Process::WNOHANG).nil? do
|
27
|
+
buf.push(parent_r.read 4096)
|
28
|
+
end
|
29
|
+
buf.push(parent_r.read 4096)
|
30
|
+
parent_r.close
|
31
|
+
buf.join
|
3
32
|
end
|
4
33
|
|
5
34
|
def self.hello
|