2
2
// Licensed under the MIT License.
3
3
4
4
using Microsoft . PowerShell . PowerShellGet . UtilClasses ;
5
+ using NuGet . Versioning ;
5
6
using System ;
6
7
using System . Collections . Generic ;
7
8
using System . Linq ;
@@ -30,7 +31,6 @@ public sealed class FindPSResource : PSCmdlet
30
31
private const string NameParameterSet = "NameParameterSet" ;
31
32
private const string CommandNameParameterSet = "CommandNameParameterSet" ;
32
33
private const string DscResourceNameParameterSet = "DscResourceNameParameterSet" ;
33
- private const string TagParameterSet = "TagParameterSet" ;
34
34
private CancellationTokenSource _cancellationTokenSource ;
35
35
private FindHelper _findHelper ;
36
36
@@ -50,10 +50,9 @@ public sealed class FindPSResource : PSCmdlet
50
50
51
51
/// <summary>
52
52
/// Specifies one or more resource types to find.
53
- /// Resource types supported are: Module, Script, Command, DscResource
53
+ /// Resource types supported are: Module, Script
54
54
/// </summary>
55
55
[ Parameter ( ParameterSetName = NameParameterSet ) ]
56
- [ Parameter ( ParameterSetName = TagParameterSet ) ]
57
56
public ResourceType Type { get ; set ; }
58
57
59
58
/// <summary>
@@ -86,7 +85,7 @@ public sealed class FindPSResource : PSCmdlet
86
85
/// <summary>
87
86
/// Filters search results for resources that include one or more of the specified tags.
88
87
/// </summary>
89
- [ Parameter ( ParameterSetName = TagParameterSet ) ]
88
+ [ Parameter ( ParameterSetName = NameParameterSet ) ]
90
89
[ ValidateNotNull ]
91
90
public string [ ] Tag { get ; set ; }
92
91
@@ -157,10 +156,6 @@ protected override void ProcessRecord()
157
156
ProcessCommandOrDscParameterSet ( isSearchingForCommands : false ) ;
158
157
break ;
159
158
160
- case TagParameterSet :
161
- ProcessTagParameterSet ( ) ;
162
- break ;
163
-
164
159
default :
165
160
Dbg . Assert ( false , "Invalid parameter set" ) ;
166
161
break ;
@@ -173,20 +168,28 @@ protected override void ProcessRecord()
173
168
174
169
private void ProcessResourceNameParameterSet ( )
175
170
{
171
+ // only cases where Name is allowed to not be specified is if Type or Tag parameters are
176
172
if ( ! MyInvocation . BoundParameters . ContainsKey ( nameof ( Name ) ) )
177
173
{
178
- // only cases where Name is allowed to not be specified is if Type or Tag parameters are
179
- if ( ! MyInvocation . BoundParameters . ContainsKey ( nameof ( Type ) ) && ! MyInvocation . BoundParameters . ContainsKey ( nameof ( Tag ) ) )
174
+ if ( MyInvocation . BoundParameters . ContainsKey ( nameof ( Tag ) ) )
175
+ {
176
+ // case where Name specified: false, Tag specified: true (i.e just search by Tags)
177
+ ProcessTagParameterSet ( ) ; // TODO: rename
178
+ return ;
179
+ }
180
+ else if ( MyInvocation . BoundParameters . ContainsKey ( nameof ( Type ) ) )
181
+ {
182
+ Name = new string [ ] { "*" } ;
183
+ }
184
+ else
180
185
{
181
186
ThrowTerminatingError (
182
187
new ErrorRecord (
183
- new PSInvalidOperationException ( "Name parameter must be provided." ) ,
188
+ new PSInvalidOperationException ( "Name parameter must be provided, unless Tag or Type parameters are used ." ) ,
184
189
"NameParameterNotProvided" ,
185
190
ErrorCategory . InvalidOperation ,
186
191
this ) ) ;
187
192
}
188
-
189
- Name = new string [ ] { "*" } ;
190
193
}
191
194
192
195
Name = Utils . ProcessNameWildcards ( Name , removeWildcardEntries : false , out string [ ] errorMsgs , out bool nameContainsWildcard ) ;
@@ -205,11 +208,48 @@ private void ProcessResourceNameParameterSet()
205
208
if ( Name . Length == 0 )
206
209
{
207
210
return ;
208
- }
211
+ }
212
+
213
+ // determine/parse out Version param
214
+ VersionType versionType = VersionType . VersionRange ;
215
+ NuGetVersion nugetVersion = null ;
216
+ VersionRange versionRange = null ;
217
+
218
+ if ( Version != null )
219
+ {
220
+ if ( ! NuGetVersion . TryParse ( Version , out nugetVersion ) )
221
+ {
222
+ if ( Version . Trim ( ) . Equals ( "*" ) )
223
+ {
224
+ versionRange = VersionRange . All ;
225
+ versionType = VersionType . VersionRange ;
226
+ }
227
+ else if ( ! VersionRange . TryParse ( Version , out versionRange ) )
228
+ {
229
+ WriteError ( new ErrorRecord (
230
+ new ArgumentException ( "Argument for -Version parameter is not in the proper format" ) ,
231
+ "IncorrectVersionFormat" ,
232
+ ErrorCategory . InvalidArgument ,
233
+ this ) ) ;
234
+ return ;
235
+ }
236
+ }
237
+ else
238
+ {
239
+ versionType = VersionType . SpecificVersion ;
240
+ }
241
+ }
242
+ else
243
+ {
244
+ versionType = VersionType . NoVersion ;
245
+ }
209
246
210
247
foreach ( PSResourceInfo pkg in _findHelper . FindByResourceName (
211
248
name : Name ,
212
249
type : Type ,
250
+ versionRange : versionRange ,
251
+ nugetVersion : nugetVersion ,
252
+ versionType : versionType ,
213
253
version : Version ,
214
254
prerelease : Prerelease ,
215
255
tag : Tag ,
0 commit comments