diff options
author | Tim Smith <tsmith@chef.io> | 2020-04-27 10:38:33 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-27 10:38:33 -0700 |
commit | a1a58fbbb6bbcf106c8960c04240d68d42f7fbe9 (patch) | |
tree | 72a9fd7bec328e51298808441f11fec1c33393fa /lib | |
parent | 47db22988400fb323441cafbba8399a3a742afc9 (diff) | |
parent | 3f1d5f3b916aa4c2aea10bb68dfe6765ce1d8969 (diff) | |
download | chef-a1a58fbbb6bbcf106c8960c04240d68d42f7fbe9.tar.gz |
Merge pull request #9742 from chef/powershell_exec_bang
Add powershell_exec! helper to make conversion from powershell_out! easier
Diffstat (limited to 'lib')
-rw-r--r-- | lib/chef/mixin/powershell_exec.rb | 11 | ||||
-rw-r--r-- | lib/chef/powershell.rb | 14 |
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) |