Skip to content

Commit eaad16b

Browse files
authored
Merge pull request #2 from hbuckle/fixparam
Fixparam
2 parents e0f3fa2 + a0fc712 commit eaad16b

File tree

6 files changed

+40
-29
lines changed

6 files changed

+40
-29
lines changed

examples/init.pp

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@
1818
package { 'PSExcel':
1919
ensure => latest,
2020
provider => 'psmodule',
21-
}
21+
source => 'PSGallery',
22+
}

lib/puppet/provider/package/psmodule.rb

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require 'puppet/provider/package'
2+
require 'json'
23

34
Puppet::Type.type(:package).provide :psmodule, parent: Puppet::Provider::Package do
45
confine operatingsystem: :windows
@@ -25,12 +26,8 @@ def self.invoke_ps_command(command)
2526

2627
def self.instances
2728
result = invoke_ps_command instances_command
28-
result.each_slice(2).collect do |mod|
29-
new(
30-
name: mod[0].strip,
31-
ensure: mod[1].strip.split(','),
32-
provider: 'psmodule'
33-
)
29+
result.each.collect do |line|
30+
new(JSON.parse(line.strip, symbolize_names: true))
3431
end
3532
end
3633

@@ -61,17 +58,20 @@ def self.instances_command
6158
# Get-Package is way faster than Get-InstalledModule
6259
<<-COMMAND
6360
Get-Package -AllVersions -ProviderName PowerShellGet -Scope AllUsers -Type Module |
64-
Group-Object -Property Name | % {
65-
$_.Name
66-
($_.Group).Version -join ','
61+
Group-Object -Property Name | % {
62+
[ordered]@{
63+
'name' = $_.Name
64+
'ensure' = @(($_.Group).Version)
65+
'provider' = 'psmodule'
66+
} | ConvertTo-Json -Depth 99 -Compress
6767
}
6868
COMMAND
6969
end
7070

7171
def install_command
7272
command = "Install-Module #{@resource[:name]} -Force"
7373
command << " -RequiredVersion #{@resource[:ensure]}" unless [:present, :latest].include? @resource[:ensure]
74-
command << " -Source #{@resource[:source]}" if @resource[:source]
74+
command << " -Repository #{@resource[:source]}" if @resource[:source]
7575
command
7676
end
7777

@@ -85,7 +85,7 @@ def latest_command
8585

8686
def update_command
8787
command = "Install-Module #{@resource[:name]} -Force"
88-
command << " -Source #{@resource[:source]}" if @resource[:source]
88+
command << " -Repository #{@resource[:source]}" if @resource[:source]
8989
command
9090
end
9191
end

lib/puppet/provider/psrepository/windows.rb

+13-8
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@ def self.invoke_ps_command(command)
2525

2626
def self.instances
2727
result = invoke_ps_command instances_command
28-
result.each.collect do |repo|
29-
new(
30-
name: repo.split(',')[0].strip,
31-
ensure: :present,
32-
source_location: repo.split(',')[1].downcase.strip,
33-
installation_policy: repo.split(',')[2].downcase.strip
34-
)
28+
result.each.collect do |line|
29+
repo = JSON.parse(line.strip, symbolize_names: true)
30+
repo[:ensure] = :present
31+
new(repo)
3532
end
3633
end
3734

@@ -77,7 +74,15 @@ def flush
7774
end
7875

7976
def self.instances_command
80-
'@(Get-PSRepository).foreach({\"$($_.Name),$($_.SourceLocation),$($_.InstallationPolicy)\"})'
77+
<<-COMMAND
78+
@(Get-PSRepository).foreach({
79+
[ordered]@{
80+
'name' = $_.Name
81+
'source_location' = $_.SourceLocation.ToLower()
82+
'installation_policy' = $_.InstallationPolicy.ToLower()
83+
} | ConvertTo-Json -Depth 99 -Compress
84+
})
85+
COMMAND
8186
end
8287

8388
def create_command

metadata.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
{
22
"name": "hbuckle-powershellmodule",
3-
"version": "0.1.0",
3+
"version": "0.2.0",
44
"author": "Henry Buckle",
55
"summary": "Manage PowerShell modules and repositories",
66
"license": "Apache-2.0",
77
"source": "https://github.com/hbuckle/puppet-powershellmodule",
88
"project_page": "https://github.com/hbuckle/puppet-powershellmodule",
99
"issues_url": "https://github.com/hbuckle/puppet-powershellmodule/issues",
1010
"tags": ["powershell", "microsoft", "package"],
11+
"dependencies": [],
1112
"operatingsystem_support": [
1213
{
1314
"operatingsystem": "Windows",

spec/unit/puppet/provider/psmodule_spec.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
@provider_instance = provider_class.new(type)
1111
allow(provider_class).to receive(:invoke_ps_command).with(
1212
provider_class.instances_command).and_return(
13-
['PackageManagement', '1.1.1.0', 'Pester', '4.0.2', 'Posh-SSH',
14-
'1.7.7', 'PowerShellGet', '1.1.2.0', 'PSDeployTools', '1.0.6',
15-
'PSExcel', '1.0,1.0.2']
13+
[
14+
'{"name":"PackageManagement","ensure":["1.1.6.0","1.1.7.0"],"provider":"psmodule"}',
15+
'{"name":"Pester","ensure":["4.0.8"],"provider":"psmodule"}',
16+
'{"name":"PowerShellGet","ensure":["1.5.0.0"],"provider":"psmodule"}'
17+
]
1618
)
1719
end
1820
describe :instances do
1921
specify 'returns an array of :psmodule providers' do
2022
instances = provider_class.instances
21-
expect(instances.count).to eq(6)
23+
expect(instances.count).to eq(3)
2224
expect(instances).to all(be_instance_of(provider_class))
2325
end
2426
end

spec/unit/puppet/provider/psrepository_spec.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@
1212
allow(provider_class).to receive(:invoke_ps_command).and_return(nil)
1313
allow(provider_class).to receive(:invoke_ps_command).with(
1414
provider_class.instances_command
15-
).and_return([
16-
'Repo1,https://repo1.com,Untrusted',
17-
'Repo2,https://repo2.com,Trusted'
18-
])
15+
).and_return(
16+
[
17+
'{"name":"Repo1","source_location":"https://repo1.com","installation_policy":"untrusted"}',
18+
'{"name":"Repo2","source_location":"https://repo2.com","installation_policy":"trusted"}'
19+
]
20+
)
1921
end
2022
describe :instances do
2123
specify 'returns an array of :windows providers' do

0 commit comments

Comments
 (0)