Skip to content

Instantly share code, notes, and snippets.

@ProTip
Created November 24, 2014 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ProTip/7fee6a550f11efafc48e to your computer and use it in GitHub Desktop.
Save ProTip/7fee6a550f11efafc48e to your computer and use it in GitHub Desktop.
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 })
@mrjcleaver
Copy link

Thanks - how would I use it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment