Sha256: b3bf3ad1680c9c3f3629d80edad0f9bf068c7d85857261f648b6de7d5dc62e55

Contents?: true

Size: 853 Bytes

Versions: 7

Compression:

Stored size: 853 Bytes

Contents

class WoofUtil

  # usage
  # shellout "xargs", :stdin => "/etc", :args => ["ls", "-ld"]
  def self.shellout(cmd, params = {})
    params[:stdin] ||= ""
    params[:args] ||= []
    
    parent_r,child_w=IO.pipe ; parent_r.binmode ; child_w.binmode
    child_r,parent_w=IO.pipe ; child_r.binmode ; parent_w.binmode

    pid = fork do
      parent_r.close
      parent_w.close
      $stdin.reopen child_r
      $stdout.reopen child_w
      $stderr.reopen child_w
      # should be closing other open FDs, punting on it for now
      exec *([cmd].concat params[:args])
    end
    child_w.close
    child_r.close
    buf = []
    parent_w.write params[:stdin]
    parent_w.close
    while Process.waitpid(pid, Process::WNOHANG).nil? do
      buf.push(parent_r.read 4096)
    end
    buf.push(parent_r.read 4096)
    parent_r.close
    buf.join
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
woof_util-0.0.18 lib/woof_util.rb
woof_util-0.0.16 lib/woof_util.rb
woof_util-0.0.13 lib/woof_util.rb
woof_util-0.0.12 lib/woof_util.rb
woof_util-0.0.9 lib/woof_util.rb
woof_util-0.0.8 lib/woof_util.rb
woof_util-0.0.7 lib/woof_util.rb