diff options
author | Jay Mundrawala <jdmundrawala@gmail.com> | 2015-03-10 14:13:58 -0700 |
---|---|---|
committer | Jay Mundrawala <jdmundrawala@gmail.com> | 2015-03-20 14:38:01 -0700 |
commit | bf1690709e532c10a534db544e264b0fd047a5f8 (patch) | |
tree | f639840c809ce003814951be532cb9a95de6c6dd | |
parent | 3d6675cac2ac2463a7fa577d8d7a44bd18addd51 (diff) | |
download | chef-bf1690709e532c10a534db544e264b0fd047a5f8.tar.gz |
JSON needs to be kept separate from other output
Write-Host and friends can utlimately poplute stdout.
This renders the json invalid
-rw-r--r-- | lib/chef/util/powershell/cmdlet.rb | 31 | ||||
-rw-r--r-- | lib/chef/util/powershell/cmdlet_result.rb | 11 |
2 files changed, 38 insertions, 4 deletions
diff --git a/lib/chef/util/powershell/cmdlet.rb b/lib/chef/util/powershell/cmdlet.rb index ed18281b59..0c9fa135a8 100644 --- a/lib/chef/util/powershell/cmdlet.rb +++ b/lib/chef/util/powershell/cmdlet.rb @@ -46,6 +46,8 @@ class Chef::Util::Powershell attr_reader :output_format def run(switches={}, execution_options={}, *arguments) + streams = { :json => CmdletStream.new('json') } + arguments_string = arguments.join(' ') switches_string = command_switches_string(switches) @@ -56,7 +58,9 @@ class Chef::Util::Powershell json_depth = @output_format_options[:depth] end - json_command = @json_format ? " | convertto-json -compress -depth #{json_depth}" : "" + json_command = @json_format ? " | convertto-json -compress -depth #{json_depth} "\ + "> #{streams[:json].path}" : "" + command_string = "powershell.exe -executionpolicy bypass -noprofile -noninteractive "\ "-command \"trap [Exception] {write-error -exception "\ "($_.Exception.Message);exit 1};#{@cmdlet} #{switches_string} "\ @@ -71,7 +75,7 @@ class Chef::Util::Powershell status = command.run_command end - CmdletResult.new(status, @output_format) + CmdletResult.new(status, streams, @output_format) end def run!(switches={}, execution_options={}, *arguments) @@ -132,5 +136,28 @@ class Chef::Util::Powershell command_switches.join(' ') end + + class CmdletStream + def initialize(name) + @filename = Dir::Tmpname.create(name) {} + ObjectSpace.define_finalizer(self, self.class.destroy(@filename)) + end + + def path + @filename + end + + def read + if File.exist? @filename + File.open(@filename, 'rb:bom|UTF-16LE') do |f| + f.read.encode('UTF-8') + end + end + end + + def self.destroy(name) + proc { File.delete(name) if File.exists? name } + end + end end end diff --git a/lib/chef/util/powershell/cmdlet_result.rb b/lib/chef/util/powershell/cmdlet_result.rb index 246701a7bc..31d8d38ccc 100644 --- a/lib/chef/util/powershell/cmdlet_result.rb +++ b/lib/chef/util/powershell/cmdlet_result.rb @@ -22,18 +22,25 @@ class Chef::Util::Powershell class CmdletResult attr_reader :output_format - def initialize(status, output_format) + def initialize(status, streams, output_format) @status = status @output_format = output_format + @streams = streams end def stderr @status.stderr end + def stream(name) + @streams[name].read + end + def return_value if output_format == :object - Chef::JSONCompat.parse(@status.stdout) + Chef::JSONCompat.parse(stream(:json)) + elsif output_format == :json + stream(:json) else @status.stdout end |