Skip to content

Refactor SetPSResourceRepository cmdlet #359

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
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
116 changes: 51 additions & 65 deletions help/Set-PSResourceRepository.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,74 @@ schema: 2.0.0
# Set-PSResourceRepository

## SYNOPSIS
{{ Fill in the Synopsis }}
Sets information for a registered repository.

## SYNTAX

### NameParameterSet (Default)
```
Set-PSResourceRepository [-Name] <String> [-URL <Uri>] [-Credential <PSCredential>] [-Trusted] [-Proxy <Uri>]
[-ProxyCredential <PSCredential>] [-Priority <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
Set-PSResourceRepository [-Name] <String> [-URL <Uri>] [-Trusted] [-Priority <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
```

### RepositoriesParameterSet
```
Set-PSResourceRepository -Repositories <System.Collections.Generic.List`1[System.Collections.Hashtable]>
[-Proxy <Uri>] [-ProxyCredential <PSCredential>] [-Priority <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
Set-PSResourceRepository -Repositories <Hashtable[]> [-Priority <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
```

## DESCRIPTION
{{ Fill in the Description }}
The Set-PSResourceRepository cmdlet sets information for a registered repository.

## EXAMPLES

These examples are run independently of each other and assume the repositories used are already registered. The 'PassThru' parameter used with Set-PSResourceRepository is only used to display the changes made to the repository and is not mandatory.
### Example 1
```powershell
PS C:\> {{ Add example code here }}
PS C:\> Get-PSResourceRepository -Name "PoshTestGallery"
Name Url Trusted Priority
---- --- ------- --------
PoshTestGallery https://www.poshtestgallery.com/api/v2 False 50
PS C:\> Set-PSResourceRepository -Name "PoshTestGallery" -URL "c:/code/testdir" -PassThru
Name Url Trusted Priority
---- --- ------- --------
PoshTestGallery file:///c:/code/testdir False 50
```

{{ Add example description here }}

## PARAMETERS
This example first checks if the PoshTestGallery repository has been registered. We wish to set the 'URL' value of this repository by running the Set-PSResourceRepository cmdlet with the 'URL' parameter and a valid Uri scheme url. We run the Get-PSResourceRepository cmdlet again to ensure that the 'URL' of the repository was changed. We also use the 'PassThru' parameter to see the changed repository.

### -Credential
{{ Fill Credential Description }}
### Example 2
```powershell
PS C:\> Get-PSResourceRepository -Name "PSGallery"
Name Url Trusted Priority
---- --- ------- --------
PSGallery https://www.powershellgallery.com/api/v2 False 50
PS C:\> Set-PSResourceRepository -Name "PSGallery" -Priority 25 -Trusted -PassThru
Name Url Trusted Priority
---- --- ------- --------
PSGallery https://www.powershellgallery.com/api/v2 True 25
```

```yaml
Type: System.Management.Automation.PSCredential
Parameter Sets: NameParameterSet
Aliases:
This example first checks if the PSGallery repository has been registered. We wish to set the 'Priority' and 'Trusted' values of this repository by running the Set-PSResourceRepository cmdlet with the 'Priority' parameter set to a value between 0 and 50 and by using the 'Trusted' parameter switch. We run the Get-PSResourceRepository cmdlet again to ensure that the 'Priority' and 'Trusted' values of the repository were changed. An important note here is that just for the default PSGallery repository, the 'URL' value can't be changed/set. We also use the 'PassThru' parameter to see the changed repository.

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
### Example 3
```powershell
PS C:\> Get-PSResourceRepository -Name "*"
Name Url Trusted Priority
---- --- ------- --------
PSGallery https://www.powershellgallery.com/api/v2 False 50
PoshTestGallery https://www.poshtestgallery.com/api/v2 False 50
PS C:\> $arrayOfHashtables = @{Name = "PSGallery"; Trusted = $True},@{Name = "PoshTestGallery"; URL = "c:/code/testdir"}
PS C:\> Set-PSResourceRepository -Repositories $arrayOfHashtables -PassThru
Name Url Trusted Priority
---- --- ------- --------
PSGallery https://www.powershellgallery.com/api/v2 True 50
PoshTestGallery file:///c:/code/testdir False 50
```

This example first checks for all registered repositories. We wish to set the properties for multiple repositories at once (i.e the PSGallery and PoshTestGallery repositories), so we run Set-PSResourceRepository with the 'Repositories' parameter. This parameter takes an array of hashtables, where each hashtable contains information for a repository we wish to set information for. We also use the 'PassThru' parameter to see the changed repositories.

## PARAMETERS

### -Name
{{ Fill Name Description }}
Specifies the name of the repository to be set.

```yaml
Type: System.String
Expand All @@ -69,7 +90,7 @@ Accept wildcard characters: False
```

### -Priority
{{ Fill Priority Description }}
Specifies the priority ranking of the repository, such that repositories with higher ranking priority are searched before a lower ranking priority one, when searching for a repository item across multiple registered repositories. Valid priority values range from 0 to 50, such that a lower numeric value (i.e 10) corresponds to a higher priority ranking than a higher numeric value (i.e 40).

```yaml
Type: System.Int32
Expand All @@ -83,41 +104,11 @@ Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -Proxy
{{ Fill Proxy Description }}

```yaml
Type: System.Uri
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -ProxyCredential
{{ Fill ProxyCredential Description }}

```yaml
Type: System.Management.Automation.PSCredential
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: None
Accept pipeline input: True (ByPropertyName)
Accept wildcard characters: False
```

### -Repositories
{{ Fill Repositories Description }}
Specifies a hashtable of repositories and is used to register multiple repositories at once.

```yaml
Type: System.Collections.Generic.List`1[System.Collections.Hashtable]
Type: Hashtable[]
Parameter Sets: RepositoriesParameterSet
Aliases:

Expand All @@ -129,7 +120,7 @@ Accept wildcard characters: False
```

### -Trusted
{{ Fill Trusted Description }}
Specifies whether the repository should be trusted.

```yaml
Type: System.Management.Automation.SwitchParameter
Expand All @@ -144,7 +135,7 @@ Accept wildcard characters: False
```

### -URL
{{ Fill URL Description }}
Specifies the location of the repository to be set.

```yaml
Type: System.Uri
Expand Down Expand Up @@ -198,19 +189,14 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable

### System.Uri

### System.Management.Automation.PSCredential

### System.Collections.Generic.List`1[[System.Collections.Hashtable, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
### System.Collections.Hashtable[]

### System.Int32

## OUTPUTS

### System.Object
### Microsoft.PowerShell.PowerShellGet.UtilClasses.PSRepositoryInfo (if 'PassThru' parameter used)

## NOTES

## RELATED LINKS

[<add>](<add>)

22 changes: 21 additions & 1 deletion src/code/RepositorySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ public static PSRepositoryInfo Add(string repoName, Uri repoURL, int repoPriorit
/// Updates a repository name, URL, priority, or installation policy
/// Returns: void
/// </summary>
public static void Update(string repoName, Uri repoURL, int repoPriority, bool? repoTrusted)
public static PSRepositoryInfo Update(string repoName, Uri repoURL, int repoPriority, bool? repoTrusted)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update comment (// Returns: void)

{
Dbg.Assert(!string.IsNullOrEmpty(repoName), "Repository name cannot be null or empty");

PSRepositoryInfo updatedRepo;
try
{
// Open file
Expand All @@ -132,28 +133,47 @@ public static void Update(string repoName, Uri repoURL, int repoPriority, bool?
// Get root of XDocument (XElement)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On line 129, error is not ArgumentException, the argument is valid. Should be ItemNotFoundException error.

var root = doc.Root;

// A null URL value passed in signifies the URL was not attempted to be set.
// So only set Url attribute if non-null value passed in for repoUrl
if (repoURL != null)
{
node.Attribute("Url").Value = repoURL.AbsoluteUri;
}

// A negative Priority value passed in signifies the Priority value was not attempted to be set.
// So only set Priority attribute if non-null value passed in for repoPriority
if (repoPriority >= 0)
{
node.Attribute("Priority").Value = repoPriority.ToString();
}

// A null Trusted value passed in signifies the Trusted value was not attempted to be set.
// So only set Trusted attribute if non-null value passed in for repoTrusted.
if (repoTrusted != null)
{
node.Attribute("Trusted").Value = repoTrusted.ToString();
}

// Create Uri from node Url attribute to create PSRepositoryInfo item to return.
if (!Uri.TryCreate(node.Attribute("Url").Value, UriKind.Absolute, out Uri thisUrl))
{
throw new PSInvalidOperationException(String.Format("Unable to read incorrectly formatted URL for repo {0}", repoName));
}

updatedRepo = new PSRepositoryInfo(repoName,
thisUrl,
Int32.Parse(node.Attribute("Priority").Value),
Boolean.Parse(node.Attribute("Trusted").Value));

// Close the file
root.Save(FullRepositoryPath);
}
catch (Exception e)
{
throw new PSInvalidOperationException(String.Format("Updating to repository store failed: {0}", e.Message));
}

return updatedRepo;
}

/// <summary>
Expand Down
Loading