Skip to content

Commit e6370be

Browse files
authored
Add -PassThru support for Unregister-PSResourceRepository (#556)
add PassThru to Unregister-PSResourceRepository cmdlet
1 parent d6bf643 commit e6370be

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

src/code/RepositorySettings.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ public static PSRepositoryInfo Update(string repoName, Uri repoURL, int repoPrio
185185
/// Returns: void
186186
/// </summary>
187187
/// <param name="sectionName"></param>
188-
public static void Remove(string[] repoNames, out string[] errorList)
188+
public static List<PSRepositoryInfo> Remove(string[] repoNames, out string[] errorList)
189189
{
190+
List<PSRepositoryInfo> removedRepos = new List<PSRepositoryInfo>();
190191
List<string> tempErrorList = new List<string>();
191192
XDocument doc;
192193
try
@@ -211,13 +212,20 @@ public static void Remove(string[] repoNames, out string[] errorList)
211212
continue;
212213
}
213214

215+
removedRepos.Add(
216+
new PSRepositoryInfo(repo,
217+
new Uri(node.Attribute("Url").Value),
218+
Int32.Parse(node.Attribute("Priority").Value),
219+
Boolean.Parse(node.Attribute("Trusted").Value)));
214220
// Remove item from file
215221
node.Remove();
216222
}
217223

218224
// Close the file
219225
root.Save(FullRepositoryPath);
220226
errorList = tempErrorList.ToArray();
227+
228+
return removedRepos;
221229
}
222230

223231
public static List<PSRepositoryInfo> Read(string[] repoNames, out string[] errorList)

src/code/UnregisterPSResourceRepository.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.PowerShell.PowerShellGet.UtilClasses;
55
using System;
6+
using System.Collections.Generic;
67
using System.Management.Automation;
78

89
namespace Microsoft.PowerShell.PowerShellGet.Cmdlets
@@ -28,6 +29,12 @@ class UnregisterPSResourceRepository : PSCmdlet
2829
[ValidateNotNullOrEmpty]
2930
public string[] Name { get; set; } = Utils.EmptyStrArray;
3031

32+
/// <summary>
33+
/// When specified, displays the repositories that were just unregistered
34+
/// </summary>
35+
[Parameter]
36+
public SwitchParameter PassThru { get; set; }
37+
3138
#endregion
3239

3340
#region Method overrides
@@ -45,7 +52,7 @@ protected override void ProcessRecord()
4552
return;
4653
}
4754

48-
RepositorySettings.Remove(Name, out string[] errorList);
55+
List<PSRepositoryInfo> removedRepositories = RepositorySettings.Remove(Name, out string[] errorList);
4956

5057
// handle non-terminating errors
5158
foreach (string error in errorList)
@@ -56,6 +63,14 @@ protected override void ProcessRecord()
5663
ErrorCategory.InvalidOperation,
5764
this));
5865
}
66+
67+
if (PassThru)
68+
{
69+
foreach (PSRepositoryInfo repository in removedRepositories)
70+
{
71+
WriteObject(repository);
72+
}
73+
}
5974
}
6075

6176
#endregion

test/SetPSResourceRepository.Tests.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,4 +164,13 @@ Describe "Test Set-PSResourceRepository" {
164164
$res.Trusted | Should -Be False
165165
$res.Priority | Should -Be 50
166166
}
167+
168+
It "set repository and see updated repository with -PassThru" {
169+
Register-PSResourceRepository -Name "testRepository" -URL $tmpDir1Path
170+
$res = Set-PSResourceRepository -Name "testRepository" -URL $tmpDir2Path -PassThru
171+
$res.Name | Should -Be "testRepository"
172+
$res.URL.LocalPath | Should -Contain $tmpDir2Path
173+
$res.Priority | Should -Be 50
174+
$res.Trusted | Should -Be False
175+
}
167176
}

test/UnregisterPSResourceRepository.Tests.ps1

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force
55

66
Describe "Test Register-PSResourceRepository" {
77
BeforeEach {
8+
$TestGalleryName = Get-PoshTestGalleryName
9+
$TestGalleryUrl = Get-PoshTestGalleryLocation
810
Get-NewPSResourceRepositoryFile
911
$tmpDir1Path = Join-Path -Path $TestDrive -ChildPath "tmpDir1"
1012
$tmpDir2Path = Join-Path -Path $TestDrive -ChildPath "tmpDir2"
@@ -59,4 +61,12 @@ Describe "Test Register-PSResourceRepository" {
5961
It "throw error if Name is null" {
6062
{Unregister-PSResourceRepository -Name $null -ErrorAction Stop} | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.PowerShellGet.Cmdlets.UnregisterPSResourceRepository"
6163
}
64+
65+
It "unregister repository using -PassThru" {
66+
$res = Unregister-PSResourceRepository -Name $TestGalleryName -PassThru
67+
$res.Name | Should -Be $TestGalleryName
68+
$res.Url | Should -Be $TestGalleryURL
69+
$res = Get-PSResourceRepository -Name $TestGalleryName
70+
$res | Should -BeNullOrEmpty
71+
}
6272
}

0 commit comments

Comments
 (0)