diff --git a/src/code/RepositorySettings.cs b/src/code/RepositorySettings.cs index a51ab4416..a38f45856 100644 --- a/src/code/RepositorySettings.cs +++ b/src/code/RepositorySettings.cs @@ -185,8 +185,9 @@ public static PSRepositoryInfo Update(string repoName, Uri repoURL, int repoPrio /// Returns: void /// /// - public static void Remove(string[] repoNames, out string[] errorList) + public static List Remove(string[] repoNames, out string[] errorList) { + List removedRepos = new List(); List tempErrorList = new List(); XDocument doc; try @@ -211,6 +212,11 @@ public static void Remove(string[] repoNames, out string[] errorList) continue; } + removedRepos.Add( + new PSRepositoryInfo(repo, + new Uri(node.Attribute("Url").Value), + Int32.Parse(node.Attribute("Priority").Value), + Boolean.Parse(node.Attribute("Trusted").Value))); // Remove item from file node.Remove(); } @@ -218,6 +224,8 @@ public static void Remove(string[] repoNames, out string[] errorList) // Close the file root.Save(FullRepositoryPath); errorList = tempErrorList.ToArray(); + + return removedRepos; } public static List Read(string[] repoNames, out string[] errorList) diff --git a/src/code/UnregisterPSResourceRepository.cs b/src/code/UnregisterPSResourceRepository.cs index bf7647082..46b209e3f 100644 --- a/src/code/UnregisterPSResourceRepository.cs +++ b/src/code/UnregisterPSResourceRepository.cs @@ -3,6 +3,7 @@ using Microsoft.PowerShell.PowerShellGet.UtilClasses; using System; +using System.Collections.Generic; using System.Management.Automation; namespace Microsoft.PowerShell.PowerShellGet.Cmdlets @@ -28,6 +29,12 @@ class UnregisterPSResourceRepository : PSCmdlet [ValidateNotNullOrEmpty] public string[] Name { get; set; } = Utils.EmptyStrArray; + /// + /// When specified, displays the repositories that were just unregistered + /// + [Parameter] + public SwitchParameter PassThru { get; set; } + #endregion #region Method overrides @@ -45,7 +52,7 @@ protected override void ProcessRecord() return; } - RepositorySettings.Remove(Name, out string[] errorList); + List removedRepositories = RepositorySettings.Remove(Name, out string[] errorList); // handle non-terminating errors foreach (string error in errorList) @@ -56,6 +63,14 @@ protected override void ProcessRecord() ErrorCategory.InvalidOperation, this)); } + + if (PassThru) + { + foreach (PSRepositoryInfo repository in removedRepositories) + { + WriteObject(repository); + } + } } #endregion diff --git a/test/SetPSResourceRepository.Tests.ps1 b/test/SetPSResourceRepository.Tests.ps1 index c302c3d79..30c0d3ecc 100644 --- a/test/SetPSResourceRepository.Tests.ps1 +++ b/test/SetPSResourceRepository.Tests.ps1 @@ -164,4 +164,13 @@ Describe "Test Set-PSResourceRepository" { $res.Trusted | Should -Be False $res.Priority | Should -Be 50 } + + It "set repository and see updated repository with -PassThru" { + Register-PSResourceRepository -Name "testRepository" -URL $tmpDir1Path + $res = Set-PSResourceRepository -Name "testRepository" -URL $tmpDir2Path -PassThru + $res.Name | Should -Be "testRepository" + $res.URL.LocalPath | Should -Contain $tmpDir2Path + $res.Priority | Should -Be 50 + $res.Trusted | Should -Be False + } } diff --git a/test/UnregisterPSResourceRepository.Tests.ps1 b/test/UnregisterPSResourceRepository.Tests.ps1 index 4359c4be9..9cb30db2b 100644 --- a/test/UnregisterPSResourceRepository.Tests.ps1 +++ b/test/UnregisterPSResourceRepository.Tests.ps1 @@ -5,6 +5,8 @@ Import-Module "$psscriptroot\PSGetTestUtils.psm1" -Force Describe "Test Register-PSResourceRepository" { BeforeEach { + $TestGalleryName = Get-PoshTestGalleryName + $TestGalleryUrl = Get-PoshTestGalleryLocation Get-NewPSResourceRepositoryFile $tmpDir1Path = Join-Path -Path $TestDrive -ChildPath "tmpDir1" $tmpDir2Path = Join-Path -Path $TestDrive -ChildPath "tmpDir2" @@ -59,4 +61,12 @@ Describe "Test Register-PSResourceRepository" { It "throw error if Name is null" { {Unregister-PSResourceRepository -Name $null -ErrorAction Stop} | Should -Throw -ErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.PowerShellGet.Cmdlets.UnregisterPSResourceRepository" } + + It "unregister repository using -PassThru" { + $res = Unregister-PSResourceRepository -Name $TestGalleryName -PassThru + $res.Name | Should -Be $TestGalleryName + $res.Url | Should -Be $TestGalleryURL + $res = Get-PSResourceRepository -Name $TestGalleryName + $res | Should -BeNullOrEmpty + } }