Module: Chef::Mixin::Command::Windows

Included in:
Chef::Mixin::Command, Chef::Mixin::Command
Defined in:
lib/chef/mixin/command/windows.rb

Instance Method Summary collapse

Instance Method Details

#popen4(cmd, args = {}, &b) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/chef/mixin/command/windows.rb', line 27

def popen4(cmd, args={}, &b)

  # By default, we are waiting before we yield the block.
  args[:waitlast] ||= false

  #XXX :user, :group, :environment support?

  Open4.popen4(cmd) do |stdin,stdout,stderr,cid|
    if b
      if args[:waitlast]
        b[cid, stdin, stdout, stderr]
        # send EOF so that if the child process is reading from STDIN
        # it will actually finish up and exit
        stdin.close_write
      else
        o = StringIO.new
        e = StringIO.new

        stdin.close

        stdout.sync = true
        stderr.sync = true

        line = stdout.gets(nil)
        if line
          o.write(line)
        end
        line = stderr.gets(nil)
        if line
          e.write(line)
        end
        o.rewind
        e.rewind
        b[cid, stdin, o, e]
      end
    else
      [cid, stdin, stdout, stderr]
    end
  end
  $?
end