Skip to content
This repository was archived by the owner on Jun 13, 2024. It is now read-only.

Commit dc719db

Browse files
authored
add prototype for repository cmdlets and validation (#519)
1 parent a92d23d commit dc719db

File tree

2 files changed

+372
-0
lines changed

2 files changed

+372
-0
lines changed

v3prototype/RepositoryManagement.ps1

Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
2+
function Register-PSResourceRepository {
3+
[OutputType([void])]
4+
[Cmdletbinding(SupportsShouldProcess = $true,
5+
DefaultParameterSetName = 'NameParameterSet')]
6+
Param
7+
(
8+
[Parameter(Mandatory = $true,
9+
Position = 0,
10+
ParameterSetName = 'NameParameterSet')]
11+
[ValidateNotNullOrEmpty()]
12+
[string]
13+
$Name,
14+
15+
[Parameter(Mandatory = $true,
16+
Position = 1,
17+
ParameterSetName = 'NameParameterSet')]
18+
[ValidateNotNullOrEmpty()]
19+
[Uri]
20+
$URL,
21+
22+
[Parameter(ValueFromPipelineByPropertyName = $true,
23+
ParameterSetName = 'NameParameterSet')]
24+
[PSCredential]
25+
$Credential,
26+
27+
[Parameter(Mandatory = $true,
28+
ParameterSetName = 'PSGalleryParameterSet')]
29+
[Switch]
30+
$PSGallery,
31+
32+
[Parameter(ParameterSetName = 'RepositoriesParameterSet')]
33+
[ValidateNotNullOrEmpty()]
34+
[Hashtable]
35+
$Repositories,
36+
37+
[Parameter()]
38+
[Switch]
39+
$Trusted,
40+
41+
[Parameter(ValueFromPipelineByPropertyName = $true)]
42+
[ValidateNotNullOrEmpty()]
43+
[Uri]
44+
$Proxy,
45+
46+
[Parameter(ValueFromPipelineByPropertyName = $true)]
47+
[PSCredential]
48+
$ProxyCredential,
49+
50+
[Parameter()]
51+
[ValidateRange(0, 50)]
52+
[int]
53+
$Priority = 25
54+
)
55+
56+
begin { }
57+
process {
58+
59+
if ($PSCmdlet.ParameterSetName -eq 'PSGalleryParameterSet') {
60+
if (-not $PSGallery) {
61+
return
62+
}
63+
64+
#Register PSGallery
65+
write-verbose -message "Successfully registered the repository PSGallery"
66+
}
67+
elseif ($PSCmdlet.ParameterSetName -eq 'RepositoriesParameterSet') {
68+
foreach ($repo in $Repositories) {
69+
70+
if ($pscmdlet.ShouldProcess($repo)) {
71+
#Register each repository in the hashtable
72+
$PSResourceRepository = [PSCustomObject] @{
73+
Name = $repo.Name
74+
URL = $repo.URL
75+
Trusted = $repo.Trusted
76+
Priority = $repo.Priority
77+
}
78+
79+
write-verbose -message "Successfully registered the repository $repo"
80+
}
81+
}
82+
}
83+
else {
84+
if ($pscmdlet.ShouldProcess($Name)) {
85+
86+
#Register the repository
87+
$PSResourceRepository = [PSCustomObject] @{
88+
Name = $Name
89+
URL = $URL
90+
Trusted = $Trusted
91+
Priority = $Priority
92+
}
93+
94+
write-verbose -message "Successfully registered the repository $Name"
95+
}
96+
}
97+
}
98+
end { }
99+
}
100+
101+
102+
function Get-PSResourceRepository {
103+
[OutputType([PSCustomObject])]
104+
[Cmdletbinding(SupportsShouldProcess = $true)]
105+
Param
106+
(
107+
[Parameter(ValueFromPipeline = $true,
108+
ValueFromPipelineByPropertyName = $true)]
109+
[ValidateNotNullOrEmpty()]
110+
[string[]]
111+
$Name
112+
)
113+
114+
begin { }
115+
process {
116+
foreach ($n in $name) {
117+
if ($pscmdlet.ShouldProcess($n)) {
118+
119+
#Find and return repository
120+
$PSResourceRepository = [PSCustomObject] @{
121+
Name = $Name
122+
URL = "placeholder-for-url"
123+
Trusted = "placeholder-for-trusted"
124+
Priority = "placeholder-for-priority"
125+
}
126+
127+
return $PSResourceRepository
128+
}
129+
}
130+
}
131+
end { }
132+
}
133+
134+
135+
function Set-PSResourceRepository {
136+
[OutputType([void])]
137+
[Cmdletbinding(SupportsShouldProcess = $true)]
138+
Param
139+
(
140+
[Parameter(Mandatory = $true,
141+
ValueFromPipeline = $true,
142+
ValueFromPipelineByPropertyName = $true,
143+
Position = 0)]
144+
[ValidateNotNullOrEmpty()]
145+
[string]
146+
$Name,
147+
148+
[Parameter(ValueFromPipelineByPropertyName = $true,
149+
Position = 1)]
150+
[ValidateNotNullOrEmpty()]
151+
[Uri]
152+
$URL,
153+
154+
[Parameter(ValueFromPipelineByPropertyName = $true)]
155+
[PSCredential]
156+
$Credential,
157+
158+
[Parameter()]
159+
[Switch]
160+
$Trusted,
161+
162+
[Parameter(ValueFromPipelineByPropertyName = $true)]
163+
[ValidateNotNullOrEmpty()]
164+
[Uri]
165+
$Proxy,
166+
167+
[Parameter(ValueFromPipelineByPropertyName = $true)]
168+
[PSCredential]
169+
$ProxyCredential,
170+
171+
[Parameter(ValueFromPipelineByPropertyName = $true,
172+
ParameterSetName = 'RepositoriesParameterSet')]
173+
[ValidateNotNullOrEmpty()]
174+
[Hashtable]
175+
$Repositories,
176+
177+
[Parameter(ValueFromPipelineByPropertyName = $true)]
178+
[ValidateRange(0, 50)]
179+
[int]
180+
$Priority
181+
)
182+
183+
184+
begin { }
185+
process {
186+
187+
if ($PSCmdlet.ParameterSetName -eq 'RepositoriesParameterSet') {
188+
189+
foreach ($repo in $Repositories) {
190+
if ($pscmdlet.ShouldProcess($repo)) {
191+
192+
$repository = Get-PSResourceRepository
193+
194+
if (-not $repository) {
195+
ThrowError -ExceptionMessage "This repository could not be found."
196+
return
197+
}
198+
199+
#Set repository properties
200+
$PSResourceRepository = [PSCustomObject] @{
201+
Name = $repo.Name
202+
URL = $repo.URL
203+
Trusted = $repo.Trusted
204+
Priority = $repo.Priority
205+
}
206+
write-verbose -message "Successfully set the $repo repository."
207+
}
208+
}
209+
}
210+
else {
211+
if ($pscmdlet.ShouldProcess($Name)) {
212+
213+
$repository = Get-PSResourceRepository
214+
215+
if (-not $repository) {
216+
ThrowError -ExceptionMessage "This repository could not be found."
217+
return
218+
}
219+
220+
#Set repository properties
221+
$PSResourceRepository = [PSCustomObject] @{
222+
Name = $Name
223+
URL = $URL
224+
InstallationPolicy = $InstallationPolicy
225+
Priority = $Priority
226+
}
227+
228+
write-verbose "Successfully set the $Name repository."
229+
}
230+
}
231+
}
232+
end { }
233+
}
234+
235+
236+
function Unregister-PSResourceRepository {
237+
[OutputType([void])]
238+
[Cmdletbinding(SupportsShouldProcess = $true)]
239+
Param
240+
(
241+
[Parameter(Mandatory = $true,
242+
ValueFromPipeline = $true,
243+
ValueFromPipelineByPropertyName = $true,
244+
Position = 0)]
245+
[ValidateNotNullOrEmpty()]
246+
[string[]]
247+
$Name
248+
)
249+
250+
begin { }
251+
process {
252+
foreach ($n in $name) {
253+
if ($pscmdlet.ShouldProcess($n)) {
254+
#Unregister the each repository
255+
write-verbose -message "Successfully unregistered $n"
256+
}
257+
}
258+
}
259+
end { }
260+
}

v3prototype/RepositoryValidations.ps1

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
#####################################
2+
### Register-PSResourceRepository ###
3+
#####################################
4+
# Should register the PowerShell Gallery
5+
Register-PSRepository -PSGallery
6+
7+
# Should register the repository "TestRepo"
8+
Register-PSResourceRepository "TestRepo" "www.testrepo.com"
9+
10+
# Should register the repository "TestRepo"
11+
Register-PSResourceRepository -name "TestRepo" -url "www.testrepo.com"
12+
13+
# Should register the repository "TestRepo" as trusted, priority should be set to 0
14+
Register-PSResourceRepository -name "TestRepo" -url "www.testrepo.com" -Trusted
15+
16+
# Should register the repository "TestRepo" with a priority of 2
17+
Register-PSResourceRepository -name "TestRepo" -url "www.testrepo.com" -Priority 2
18+
19+
### Repositories
20+
# Should register the repositories "TestRepo1", "TestRepo2", "PSGallery"
21+
Register-PSResourceRepository -Repositories @(
22+
@{ Name = "TestRepo1"; URL = "https://testrepo1.com"; Trusted = $true; Credential = $cred }
23+
@{ Name = "TestRepo2"; URL = "\\server\share\myrepository"; Trusted = $true }
24+
@{ Default = $true; Trusted = $true }
25+
)
26+
27+
# Should return the repositories "TestRepo1", "TestRepo2", "PSGallery"
28+
$repos = @(
29+
@{ Name = "TestRepo1"; URL = "https://testrepo1.com"; Trusted = $true; }
30+
@{ Name = "TestRepo2"; URL = "\\server\share\myrepository"; Trusted = $true }
31+
@{ Default = $true; Trusted = $true }
32+
)
33+
$repos | Register-PSResourceRepository
34+
35+
36+
37+
################################
38+
### Get-PSResourceRepository ###
39+
################################
40+
# Should return all repositories
41+
Get-PSResourceRepository
42+
43+
# Should return the repository "TestRepo"
44+
Get-PSResourceRepository "TestRepo"
45+
46+
# Should return the repository "TestRepo"
47+
Get-PSResourceRepository -name "TestRepo"
48+
49+
# Should return the repositories "TestRepo1", "TestRepo2", "TestRepo3"
50+
Get-PSResourceRepository "TestRepo1", "TestRepo2", "TestRepo3"
51+
52+
# Should return the repositories "TestRepo1", "TestRepo2", "TestRepo3"
53+
Get-PSResourceRepository -name "TestRepo1", "TestRepo2", "TestRepo3"
54+
55+
# Should return the repository "TestRepo"
56+
"TestRepo1" | Get-PSResourceRepository
57+
58+
# Should return the repositories "TestRepo1", "TestRepo2", "TestRepo3"
59+
"TestRepo1", "TestRepo2", "TestRepo3" | Get-PSResourceRepository
60+
61+
62+
63+
################################
64+
### Set-PSResourceRepository ###
65+
################################
66+
# Should set the repository "TestRepo" to the url "www.testrepo.com"
67+
Set-PSResourceRepository "TestRepo" "www.testrepo.com"
68+
69+
# Should set the repository "TestRepo" to the url "www.testrepo.com"
70+
Set-PSResourceRepository -name "TestRepo" -url "www.testrepo.com"
71+
72+
# Should set the repository "TestRepo" to trusted, with a priority of 0
73+
Set-PSResourceRepository "TestRepo" -Trusted
74+
75+
# Should set the repository "TestRepo" with a priority of 2
76+
Set-PSResourceRepository "TestRepo" "www.testrepo.com" -Priority 2
77+
78+
# Should set the repository "TestRepo"
79+
"TestRepo1" | Set-PSResourceRepository -url "www.testrepo.com"
80+
81+
### Repositories
82+
# Should set the repositories "TestRepo1", "TestRepo2", "PSGallery" to trusted, with a priority of 0
83+
Set-PSResourceRepository -Repositories @(
84+
@{ Name = "TestRepo1"; URL = "https://testrepo1.com"; Trusted = $true; }
85+
@{ Name = "TestRepo2"; URL = "\\server\share\myrepository"; Trusted = $true }
86+
@{ Default = $true; Trusted = $true }
87+
)
88+
89+
# Should return the repositories "TestRepo1", "TestRepo2", "PSGallery"
90+
$repos = @(
91+
@{ Name = "TestRepo1"; URL = "https://testrepo1.com"; Trusted = $true; }
92+
@{ Name = "TestRepo2"; URL = "\\server\share\myrepository"; Trusted = $true }
93+
@{ Default = $true; Trusted = $true }
94+
)
95+
$repos | Set-PSResourceRepository
96+
97+
98+
99+
#######################################
100+
### Unregister-PSResourceRepository ###
101+
#######################################
102+
# Should unregister the repository "TestRepo"
103+
Unregister-PSResourceRepository -name "TestRepo"
104+
105+
# Should unregister the repositories "TestRepo1", "TestRepo2", "TestRepo3"
106+
Unregister-PSResourceRepository -name "TestRepo1", "TestRepo2", "TestRepo3"
107+
108+
# Should unregister the repository "TestRepo"
109+
"TestRepo1" | Unregister-PSResourceRepository
110+
111+
# Should unregister the repositories "TestRepo1", "TestRepo2", "TestRepo3"
112+
"TestRepo1", "TestRepo2", "TestRepo3" | Unregister-PSResourceRepository

0 commit comments

Comments
 (0)