Skip to content

Commit 22f738c

Browse files
authored
Merge pull request #20 from palintir/allowclobber
Added support for install_options argument.
2 parents b6524cf + 998e819 commit 22f738c

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ package { 'xPSDesiredStateConfiguration':
149149
ensure => latest,
150150
provider => 'windowspowershell',
151151
source => 'PSGallery',
152+
install_options => [ '-AllowClobber' ]
152153
}
153154
154155
package { 'Pester':
@@ -187,6 +188,19 @@ file{"C:\Program Files\PackageManagement\ProviderAssemblies\nuget\2.8.5.208\Micr
187188

188189
`puppet-powershellmodule` implements a [package type](http://docs.puppet.com/references/latest/type.html#package) with a resource provider, which is built into Puppet.
189190

191+
The implementation supports the [install_options](https://puppet.com/docs/puppet/6.2/type.html#package-attribute-install_options) attribute which can be used to pass additional options to the PowerShell Install-Modules command, e.g.:
192+
193+
```
194+
package { 'xPSDesiredStateConfiguration':
195+
ensure => latest,
196+
provider => 'windowspowershell',
197+
source => 'PSGallery',
198+
install_options => [ '-AllowClobber',
199+
{ '-proxy' => 'http://proxy.local.domain' } ]
200+
}
201+
202+
```
203+
190204
### pspackageprovider
191205

192206
#### Properties/Parameters

lib/puppet/provider/package/powershellcore.rb

+29-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
Puppet::Type.type(:package).provide :powershellcore, parent: Puppet::Provider::Package do
55
initvars
6-
has_feature :installable, :uninstallable, :upgradeable, :versionable
6+
has_feature :installable, :uninstallable, :upgradeable, :versionable, :install_options
77
commands pwsh: 'pwsh'
88

99
def self.invoke_ps_command(command)
@@ -44,6 +44,32 @@ def update
4444
self.class.invoke_ps_command update_command
4545
end
4646

47+
# Turns a array of install_options into flags to be passed to a command.
48+
# The options can be passed as a string or hash. Note that passing a hash
49+
# should only be used in case "-foo bar" must be passed,
50+
# Regular flags like '-foobar' must be passed as a string.
51+
# which can be accomplished with:
52+
# install_options => [ '-foobar',{ '-foo' => 'bar' } ]
53+
# This will result in the following being passed as arguments to the command:
54+
# -foobar -foo bar
55+
# @param options [Array]
56+
# @return Concatenated list of options
57+
# @api private
58+
def install_options(options)
59+
return unless options
60+
61+
options.collect do |val|
62+
case val
63+
when Hash
64+
val.keys.sort.collect do |k|
65+
"#{k} #{val[k]}"
66+
end
67+
else
68+
val
69+
end
70+
end.flatten.join(" ")
71+
end
72+
4773
def self.instances_command
4874
# Get-Package is way faster than Get-InstalledModule
4975
<<-COMMAND
@@ -62,6 +88,7 @@ def install_command
6288
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
6389
command << " -RequiredVersion #{@resource[:ensure]}" unless [:present, :latest].include? @resource[:ensure]
6490
command << " -Repository #{@resource[:source]}" if @resource[:source]
91+
command << " #{install_options(@resource[:install_options])}" if @resource[:install_options]
6592
command
6693
end
6794

@@ -76,6 +103,7 @@ def latest_command
76103
def update_command
77104
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
78105
command << " -Repository #{@resource[:source]}" if @resource[:source]
106+
command << " #{install_options(@resource[:install_options])}" if @resource[:install_options]
79107
command
80108
end
81109
end

lib/puppet/provider/package/windowspowershell.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
initvars
33
confine operatingsystem: :windows
44
confine feature: :powershellgetwindows
5-
has_feature :installable, :uninstallable, :upgradeable, :versionable
5+
has_feature :installable, :uninstallable, :upgradeable, :versionable, :install_options
66
commands powershell: 'powershell'
77

88
def self.invoke_ps_command(command)

0 commit comments

Comments
 (0)