Skip to content

Add -PassThru support for Unregister-PSResourceRepository #556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/code/RepositorySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ public static PSRepositoryInfo Update(string repoName, Uri repoURL, int repoPrio
/// Returns: void
/// </summary>
/// <param name="sectionName"></param>
public static void Remove(string[] repoNames, out string[] errorList)
public static List<PSRepositoryInfo> Remove(string[] repoNames, out string[] errorList)
{
List<PSRepositoryInfo> removedRepos = new List<PSRepositoryInfo>();
List<string> tempErrorList = new List<string>();
XDocument doc;
try
Expand All @@ -211,13 +212,20 @@ 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();
}

// Close the file
root.Save(FullRepositoryPath);
errorList = tempErrorList.ToArray();

return removedRepos;
}

public static List<PSRepositoryInfo> Read(string[] repoNames, out string[] errorList)
Expand Down
17 changes: 16 additions & 1 deletion src/code/UnregisterPSResourceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using Microsoft.PowerShell.PowerShellGet.UtilClasses;
using System;
using System.Collections.Generic;
using System.Management.Automation;

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

/// <summary>
/// When specified, displays the repositories that were just unregistered
/// </summary>
[Parameter]
public SwitchParameter PassThru { get; set; }

#endregion

#region Method overrides
Expand All @@ -45,7 +52,7 @@ protected override void ProcessRecord()
return;
}

RepositorySettings.Remove(Name, out string[] errorList);
List<PSRepositoryInfo> removedRepositories = RepositorySettings.Remove(Name, out string[] errorList);

// handle non-terminating errors
foreach (string error in errorList)
Expand All @@ -56,6 +63,14 @@ protected override void ProcessRecord()
ErrorCategory.InvalidOperation,
this));
}

if (PassThru)
{
foreach (PSRepositoryInfo repository in removedRepositories)
{
WriteObject(repository);
}
}
}

#endregion
Expand Down
9 changes: 9 additions & 0 deletions test/SetPSResourceRepository.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
10 changes: 10 additions & 0 deletions test/UnregisterPSResourceRepository.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
}