diff options
author | Tor Magnus Rakvåg <tm@intility.no> | 2018-02-12 13:40:48 +0100 |
---|---|---|
committer | Tor Magnus Rakvåg <tm@intility.no> | 2018-02-12 14:20:09 +0100 |
commit | baba34bdf0fd079f6bde0f7f0f28929c084543ba (patch) | |
tree | a92282a14cc12e101eee406f4b846a1aee10f52c /lib/chef/provider | |
parent | a7d56100275045ad44e57386aad86e463788ee85 (diff) | |
download | chef-baba34bdf0fd079f6bde0f7f0f28929c084543ba.tar.gz |
create function to build powershell calls
Signed-off-by: Tor Magnus Rakvåg tm@intility.no
Diffstat (limited to 'lib/chef/provider')
-rw-r--r-- | lib/chef/provider/package/powershell.rb | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/lib/chef/provider/package/powershell.rb b/lib/chef/provider/package/powershell.rb index 9111869c2b..9fe3e52b24 100644 --- a/lib/chef/provider/package/powershell.rb +++ b/lib/chef/provider/package/powershell.rb @@ -53,7 +53,7 @@ class Chef # Installs the package specified with the version passed else latest version will be installed def install_package(names, versions) names.each_with_index do |name, index| - powershell_out("Install-Package '#{name}' -Force -ForceBootstrap -RequiredVersion #{versions[index]}", timeout: new_resource.timeout) + powershell_out(build_powershell_command("Install-Package '#{name}'", versions[index]), timeout: new_resource.timeout) end end @@ -61,11 +61,11 @@ class Chef def remove_package(names, versions) names.each_with_index do |name, index| if versions && !versions[index].nil? - powershell_out("Uninstall-Package '#{name}' -Force -ForceBootstrap -RequiredVersion #{versions[index]}", timeout: new_resource.timeout) + powershell_out(build_powershell_command("Uninstall-Package '#{name}'", versions[index]), timeout: new_resource.timeout) else version = "0" until version.empty? - version = powershell_out("(Uninstall-Package '#{name}' -Force -ForceBootstrap).Version", timeout: new_resource.timeout).stdout.strip + version = powershell_out(build_powershell_command("Uninstall-Package '#{name}'"), timeout: new_resource.timeout).stdout.strip unless version.empty? Chef::Log.info("Removed package '#{name}' with version #{version}") end @@ -79,9 +79,9 @@ class Chef versions = [] new_resource.package_name.each_with_index do |name, index| version = if new_resource.version && !new_resource.version[index].nil? - powershell_out("(Find-Package '#{name}' -RequiredVersion #{new_resource.version[index]} -ForceBootstrap -Force).Version", timeout: new_resource.timeout).stdout.strip + powershell_out(build_powershell_command("Find-Package '#{name}'", new_resource.version[index]), timeout: new_resource.timeout).stdout.strip else - powershell_out("(Find-Package '#{name}' -ForceBootstrap -Force).Version", timeout: new_resource.timeout).stdout.strip + powershell_out(build_powershell_command("Find-Package '#{name}'"), timeout: new_resource.timeout).stdout.strip end if version.empty? version = nil @@ -96,9 +96,9 @@ class Chef version_list = [] new_resource.package_name.each_with_index do |name, index| version = if new_resource.version && !new_resource.version[index].nil? - powershell_out("(Get-Package -Name '#{name}' -RequiredVersion #{new_resource.version[index]} -ForceBootstrap -Force).Version", timeout: new_resource.timeout).stdout.strip + powershell_out(build_powershell_command("Get-Package '#{name}'", new_resource.version[index]), timeout: new_resource.timeout).stdout.strip else - powershell_out("(Get-Package -Name '#{name}' -ForceBootstrap -Force).Version", timeout: new_resource.timeout).stdout.strip + powershell_out(build_powershell_command("Get-Package '#{name}'"), timeout: new_resource.timeout).stdout.strip end if version.empty? version = nil @@ -108,6 +108,16 @@ class Chef version_list end + def build_powershell_command(command, version = nil) + command = [command] unless command.is_a?(Array) + command.unshift("(") + %w(-Force -ForceBootstrap).each do |arg| + command.push(arg) + end + command.push("-RequiredVersion #{version}") if version + command.push(").Version") + command.join(" ") + end end end end |