Skip to content

Commit

Permalink
Added support for install_options argument.
Browse files Browse the repository at this point in the history
install_options can now be specified to allow additional arguments to be passed to the PowerShell Install-Module command, e.g.

    package { 'foobarmodule':
        ensure => latest,
        provider => 'windowspowershell',
        source => 'PSGallery',
        install_options => [ '-AllowClobber' ]
    }
  • Loading branch information
palintir committed Jan 24, 2019
1 parent b6524cf commit d097248
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ package { 'xPSDesiredStateConfiguration':
ensure => latest,
provider => 'windowspowershell',
source => 'PSGallery',
install_options => [ '-AllowClobber' ]
}
package { 'Pester':
Expand Down
30 changes: 29 additions & 1 deletion lib/puppet/provider/package/powershellcore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Puppet::Type.type(:package).provide :powershellcore, parent: Puppet::Provider::Package do
initvars
has_feature :installable, :uninstallable, :upgradeable, :versionable
has_feature :installable, :uninstallable, :upgradeable, :versionable, :install_options
commands pwsh: 'pwsh'

def self.invoke_ps_command(command)
Expand Down Expand Up @@ -44,6 +44,32 @@ def update
self.class.invoke_ps_command update_command
end

# Turns a array of install_options into flags to be passed to a command.
# The options can be passed as a string or hash. Note that passing a hash
# should only be used in case "-foo bar" must be passed,
# Regular flags like '-foobar' must be passed as a string.
# which can be accomplished with:
# install_options => [ '-foobar',{ '-foo' => 'bar' } ]
# This will result in the following being passed as arguments to the command:
# -foobar -foo bar
# @param options [Array]
# @return Concatenated list of options
# @api private
def install_options
return unless @resource[:install_options]

@resource[:install_options].collect do |val|
case val
when Hash
val.keys.sort.collect do |k|
"#{k} #{val[k]}"
end
else
val
end
end.flatten.join(" ")
end

def self.instances_command
# Get-Package is way faster than Get-InstalledModule
<<-COMMAND
Expand All @@ -62,6 +88,7 @@ def install_command
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
command << " -RequiredVersion #{@resource[:ensure]}" unless [:present, :latest].include? @resource[:ensure]
command << " -Repository #{@resource[:source]}" if @resource[:source]
command << " #{install_options}" if @resource[:install_options]
command
end

Expand All @@ -76,6 +103,7 @@ def latest_command
def update_command
command = "Install-Module #{@resource[:name]} -Scope AllUsers -Force"
command << " -Repository #{@resource[:source]}" if @resource[:source]
command << " #{install_options}" if @resource[:install_options]
command
end
end
2 changes: 1 addition & 1 deletion lib/puppet/provider/package/windowspowershell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
initvars
confine operatingsystem: :windows
confine feature: :powershellgetwindows
has_feature :installable, :uninstallable, :upgradeable, :versionable
has_feature :installable, :uninstallable, :upgradeable, :versionable, :install_options
commands powershell: 'powershell'

def self.invoke_ps_command(command)
Expand Down

0 comments on commit d097248

Please sign in to comment.