Created
November 24, 2014 08:10
-
-
Save ProTip/7fee6a550f11efafc48e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Chef | |
class Recipe | |
class ProTip | |
class StreamReader | |
require 'stringio' | |
def initialize | |
@callbacks = Hash.new { |h, k| h[k] = [] } | |
@buffer = StringIO.new | |
end | |
def on(type, &blk) | |
@callbacks[type] << blk | |
end | |
def << (chunk) | |
@callbacks[:read].each { |blk| blk.call(chunk) } | |
@buffer.write(chunk) | |
@buffer.rewind | |
leftover = '' | |
@buffer.readlines.each do |read_line| | |
if read_line =~ /\n/ | |
@callbacks[:line].each do |blk| | |
blk.call(read_line.chomp) | |
end | |
else | |
leftover = read_line | |
end | |
end | |
@buffer = StringIO.new | |
@buffer.write(leftover) | |
end | |
end | |
end | |
end | |
end | |
stream_reader = Chef::Recipe::ProTip::StreamReader.new.on(:line) do |line| | |
Chef::Log.debug(line) | |
end | |
result = shell_out!(cmd, {:live_stream => stream_reader, :returns => [0,100], :timeout => 3600 }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks - how would I use it?