summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJay Mundrawala <jdmundrawala@gmail.com>2015-03-10 14:13:58 -0700
committerJay Mundrawala <jdmundrawala@gmail.com>2015-03-10 15:15:19 -0700
commit3b42ee9c9c941dcd0939dc31279bb10cc7a69d2c (patch)
tree2447b48421d673901062663d50032dbea6fc4e3b
parent07c2a1d86eaeb33d1213a60b936cbd43be955228 (diff)
downloadchef-jdm/cmdlet-streams.tar.gz
JSON needs to be kept separate from other outputjdm/cmdlet-streams
Write-Host and friends can utlimately poplute stdout. This renders the json invalid
-rw-r--r--lib/chef/util/powershell/cmdlet.rb31
-rw-r--r--lib/chef/util/powershell/cmdlet_result.rb11
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