Skip to content

Commit f7f5ee9

Browse files
authored
Refactor SetPSResourceRepository cmdlet (#359)
* added implemenation and test for Set * remove unnecessary comments * add some summary parameter comments * remove more comments * remove NameParameterSetHelper() * add test and code for detecting if name key in repositories param is null * update help doc for Set * fix codacy issues * add Debug statements * add newline at end of SetPSResourceRepository.cs file * used parameterset name const variables instead of parameter set strings * remove Credential parameter * remove Proxy and ProxyCredential parameters * remove Credential parameter from help docs as well * trim whitespace with Trim(), no param needed * order directives alphabetically * remove Priority and URL parameters from pipeline
1 parent 95b8557 commit f7f5ee9

File tree

3 files changed

+356
-66
lines changed

3 files changed

+356
-66
lines changed

help/Set-PSResourceRepository.md

Lines changed: 51 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -8,53 +8,74 @@ schema: 2.0.0
88
# Set-PSResourceRepository
99

1010
## SYNOPSIS
11-
{{ Fill in the Synopsis }}
11+
Sets information for a registered repository.
1212

1313
## SYNTAX
1414

1515
### NameParameterSet (Default)
1616
```
17-
Set-PSResourceRepository [-Name] <String> [-URL <Uri>] [-Credential <PSCredential>] [-Trusted] [-Proxy <Uri>]
18-
[-ProxyCredential <PSCredential>] [-Priority <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
17+
Set-PSResourceRepository [-Name] <String> [-URL <Uri>] [-Trusted] [-Priority <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
1918
```
2019

2120
### RepositoriesParameterSet
2221
```
23-
Set-PSResourceRepository -Repositories <System.Collections.Generic.List`1[System.Collections.Hashtable]>
24-
[-Proxy <Uri>] [-ProxyCredential <PSCredential>] [-Priority <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
22+
Set-PSResourceRepository -Repositories <Hashtable[]> [-Priority <Int32>] [-WhatIf] [-Confirm] [<CommonParameters>]
2523
```
2624

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

3028
## EXAMPLES
31-
29+
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.
3230
### Example 1
3331
```powershell
34-
PS C:\> {{ Add example code here }}
32+
PS C:\> Get-PSResourceRepository -Name "PoshTestGallery"
33+
Name Url Trusted Priority
34+
---- --- ------- --------
35+
PoshTestGallery https://www.poshtestgallery.com/api/v2 False 50
36+
PS C:\> Set-PSResourceRepository -Name "PoshTestGallery" -URL "c:/code/testdir" -PassThru
37+
Name Url Trusted Priority
38+
---- --- ------- --------
39+
PoshTestGallery file:///c:/code/testdir False 50
3540
```
3641

37-
{{ Add example description here }}
38-
39-
## PARAMETERS
42+
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.
4043

41-
### -Credential
42-
{{ Fill Credential Description }}
44+
### Example 2
45+
```powershell
46+
PS C:\> Get-PSResourceRepository -Name "PSGallery"
47+
Name Url Trusted Priority
48+
---- --- ------- --------
49+
PSGallery https://www.powershellgallery.com/api/v2 False 50
50+
PS C:\> Set-PSResourceRepository -Name "PSGallery" -Priority 25 -Trusted -PassThru
51+
Name Url Trusted Priority
52+
---- --- ------- --------
53+
PSGallery https://www.powershellgallery.com/api/v2 True 25
54+
```
4355

44-
```yaml
45-
Type: System.Management.Automation.PSCredential
46-
Parameter Sets: NameParameterSet
47-
Aliases:
56+
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.
4857

49-
Required: False
50-
Position: Named
51-
Default value: None
52-
Accept pipeline input: True (ByPropertyName)
53-
Accept wildcard characters: False
58+
### Example 3
59+
```powershell
60+
PS C:\> Get-PSResourceRepository -Name "*"
61+
Name Url Trusted Priority
62+
---- --- ------- --------
63+
PSGallery https://www.powershellgallery.com/api/v2 False 50
64+
PoshTestGallery https://www.poshtestgallery.com/api/v2 False 50
65+
PS C:\> $arrayOfHashtables = @{Name = "PSGallery"; Trusted = $True},@{Name = "PoshTestGallery"; URL = "c:/code/testdir"}
66+
PS C:\> Set-PSResourceRepository -Repositories $arrayOfHashtables -PassThru
67+
Name Url Trusted Priority
68+
---- --- ------- --------
69+
PSGallery https://www.powershellgallery.com/api/v2 True 50
70+
PoshTestGallery file:///c:/code/testdir False 50
5471
```
5572

73+
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.
74+
75+
## PARAMETERS
76+
5677
### -Name
57-
{{ Fill Name Description }}
78+
Specifies the name of the repository to be set.
5879

5980
```yaml
6081
Type: System.String
@@ -69,7 +90,7 @@ Accept wildcard characters: False
6990
```
7091
7192
### -Priority
72-
{{ Fill Priority Description }}
93+
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).
7394
7495
```yaml
7596
Type: System.Int32
@@ -83,41 +104,11 @@ Accept pipeline input: True (ByPropertyName)
83104
Accept wildcard characters: False
84105
```
85106
86-
### -Proxy
87-
{{ Fill Proxy Description }}
88-
89-
```yaml
90-
Type: System.Uri
91-
Parameter Sets: (All)
92-
Aliases:
93-
94-
Required: False
95-
Position: Named
96-
Default value: None
97-
Accept pipeline input: True (ByPropertyName)
98-
Accept wildcard characters: False
99-
```
100-
101-
### -ProxyCredential
102-
{{ Fill ProxyCredential Description }}
103-
104-
```yaml
105-
Type: System.Management.Automation.PSCredential
106-
Parameter Sets: (All)
107-
Aliases:
108-
109-
Required: False
110-
Position: Named
111-
Default value: None
112-
Accept pipeline input: True (ByPropertyName)
113-
Accept wildcard characters: False
114-
```
115-
116107
### -Repositories
117-
{{ Fill Repositories Description }}
108+
Specifies a hashtable of repositories and is used to register multiple repositories at once.
118109
119110
```yaml
120-
Type: System.Collections.Generic.List`1[System.Collections.Hashtable]
111+
Type: Hashtable[]
121112
Parameter Sets: RepositoriesParameterSet
122113
Aliases:
123114

@@ -129,7 +120,7 @@ Accept wildcard characters: False
129120
```
130121
131122
### -Trusted
132-
{{ Fill Trusted Description }}
123+
Specifies whether the repository should be trusted.
133124
134125
```yaml
135126
Type: System.Management.Automation.SwitchParameter
@@ -144,7 +135,7 @@ Accept wildcard characters: False
144135
```
145136
146137
### -URL
147-
{{ Fill URL Description }}
138+
Specifies the location of the repository to be set.
148139
149140
```yaml
150141
Type: System.Uri
@@ -198,19 +189,14 @@ This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable
198189
199190
### System.Uri
200191
201-
### System.Management.Automation.PSCredential
202-
203-
### System.Collections.Generic.List`1[[System.Collections.Hashtable, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]
192+
### System.Collections.Hashtable[]
204193
205194
### System.Int32
206195
207196
## OUTPUTS
208197
209-
### System.Object
198+
### Microsoft.PowerShell.PowerShellGet.UtilClasses.PSRepositoryInfo (if 'PassThru' parameter used)
210199
211200
## NOTES
212201
213202
## RELATED LINKS
214-
215-
[<add>](<add>)
216-

src/code/RepositorySettings.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ public static PSRepositoryInfo Add(string repoName, Uri repoURL, int repoPriorit
114114
/// Updates a repository name, URL, priority, or installation policy
115115
/// Returns: void
116116
/// </summary>
117-
public static void Update(string repoName, Uri repoURL, int repoPriority, bool? repoTrusted)
117+
public static PSRepositoryInfo Update(string repoName, Uri repoURL, int repoPriority, bool? repoTrusted)
118118
{
119119
Dbg.Assert(!string.IsNullOrEmpty(repoName), "Repository name cannot be null or empty");
120120

121+
PSRepositoryInfo updatedRepo;
121122
try
122123
{
123124
// Open file
@@ -132,28 +133,47 @@ public static void Update(string repoName, Uri repoURL, int repoPriority, bool?
132133
// Get root of XDocument (XElement)
133134
var root = doc.Root;
134135

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

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

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

157+
// Create Uri from node Url attribute to create PSRepositoryInfo item to return.
158+
if (!Uri.TryCreate(node.Attribute("Url").Value, UriKind.Absolute, out Uri thisUrl))
159+
{
160+
throw new PSInvalidOperationException(String.Format("Unable to read incorrectly formatted URL for repo {0}", repoName));
161+
}
162+
163+
updatedRepo = new PSRepositoryInfo(repoName,
164+
thisUrl,
165+
Int32.Parse(node.Attribute("Priority").Value),
166+
Boolean.Parse(node.Attribute("Trusted").Value));
167+
150168
// Close the file
151169
root.Save(FullRepositoryPath);
152170
}
153171
catch (Exception e)
154172
{
155173
throw new PSInvalidOperationException(String.Format("Updating to repository store failed: {0}", e.Message));
156174
}
175+
176+
return updatedRepo;
157177
}
158178

159179
/// <summary>

0 commit comments

Comments
 (0)