summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTim Smith <tsmith84@gmail.com>2020-04-26 20:05:30 -0700
committerTim Smith <tsmith84@gmail.com>2020-04-26 21:45:22 -0700
commitdaf65da4090a837b4d283c4153fc2a631e4f0f45 (patch)
tree6ade5d4b0e0c2a2d93efc8b1e6e8406d4d79b163
parent3e774a6bc7e7d65231d4b45753cc0404189a5359 (diff)
downloadchef-daf65da4090a837b4d283c4153fc2a631e4f0f45.tar.gz
Add .error! for powershell_exec
This matches powershell_out and it's something we need to easily migrate resources to the new helper. Signed-off-by: Tim Smith <tsmith@chef.io>
-rw-r--r--lib/chef/mixin/powershell_exec.rb11
-rw-r--r--lib/chef/powershell.rb14
2 files changed, 24 insertions, 1 deletions
diff --git a/lib/chef/mixin/powershell_exec.rb b/lib/chef/mixin/powershell_exec.rb
index 1a60a3d480..e3410e007f 100644
--- a/lib/chef/mixin/powershell_exec.rb
+++ b/lib/chef/mixin/powershell_exec.rb
@@ -20,7 +20,7 @@ require_relative "../powershell"
# The powershell_exec mixin provides in-process access to PowerShell engine via
# a COM interop (installed by the Chef Client installer).
#
-# powershell_exec returns a Chef::PowerShell object that provides 3 methods:
+# powershell_exec returns a Chef::PowerShell object that provides 4 methods:
#
# .result - returns a hash representing the results returned by executing the
# PowerShell script block
@@ -28,6 +28,7 @@ require_relative "../powershell"
# PowerShell error stream during execution
# .error? - returns true if there were error messages written to the PowerShell
# error stream during execution
+# .error! - raise Chef::PowerShell::CommandFailed if there was an error
#
# Some examples of usage:
#
@@ -100,6 +101,14 @@ class Chef
def powershell_exec(script)
Chef::PowerShell.new(script)
end
+
+ # The same as the #powershell_exec method except this will raise
+ # Chef::PowerShell::CommandFailed if the command fails
+ def powershell_exec!(script)
+ cmd = Chef::PowerShell.new(script)
+ cmd.error!
+ cmd
+ end
end
end
end
diff --git a/lib/chef/powershell.rb b/lib/chef/powershell.rb
index b1f8c5396c..9790a3c10f 100644
--- a/lib/chef/powershell.rb
+++ b/lib/chef/powershell.rb
@@ -39,12 +39,26 @@ class Chef
exec(script)
end
+ #
+ # Was there an error running the command
+ #
+ # @return [Boolean]
+ #
def error?
return true if errors.count > 0
false
end
+ class CommandFailed < RuntimeError; end
+
+ #
+ # @raise [Chef::PowerShell::CommandFailed] raise if the command failed
+ #
+ def error!
+ raise Chef::PowerShell::CommandFailed, "Unexpected exit in PowerShell command: #{@errors}" if error?
+ end
+
private
def exec(script)