22// Licensed under the MIT License.
33
44using Microsoft . PowerShell . PowerShellGet . UtilClasses ;
5+ using NuGet . Versioning ;
56using System ;
67using System . Collections . Generic ;
78using System . Linq ;
@@ -30,7 +31,6 @@ public sealed class FindPSResource : PSCmdlet
3031 private const string NameParameterSet = "NameParameterSet" ;
3132 private const string CommandNameParameterSet = "CommandNameParameterSet" ;
3233 private const string DscResourceNameParameterSet = "DscResourceNameParameterSet" ;
33- private const string TagParameterSet = "TagParameterSet" ;
3434 private CancellationTokenSource _cancellationTokenSource ;
3535 private FindHelper _findHelper ;
3636
@@ -50,10 +50,9 @@ public sealed class FindPSResource : PSCmdlet
5050
5151 /// <summary>
5252 /// Specifies one or more resource types to find.
53- /// Resource types supported are: Module, Script, Command, DscResource
53+ /// Resource types supported are: Module, Script
5454 /// </summary>
5555 [ Parameter ( ParameterSetName = NameParameterSet ) ]
56- [ Parameter ( ParameterSetName = TagParameterSet ) ]
5756 public ResourceType Type { get ; set ; }
5857
5958 /// <summary>
@@ -86,7 +85,7 @@ public sealed class FindPSResource : PSCmdlet
8685 /// <summary>
8786 /// Filters search results for resources that include one or more of the specified tags.
8887 /// </summary>
89- [ Parameter ( ParameterSetName = TagParameterSet ) ]
88+ [ Parameter ( ParameterSetName = NameParameterSet ) ]
9089 [ ValidateNotNull ]
9190 public string [ ] Tag { get ; set ; }
9291
@@ -157,10 +156,6 @@ protected override void ProcessRecord()
157156 ProcessCommandOrDscParameterSet ( isSearchingForCommands : false ) ;
158157 break ;
159158
160- case TagParameterSet :
161- ProcessTagParameterSet ( ) ;
162- break ;
163-
164159 default :
165160 Dbg . Assert ( false , "Invalid parameter set" ) ;
166161 break ;
@@ -173,20 +168,28 @@ protected override void ProcessRecord()
173168
174169 private void ProcessResourceNameParameterSet ( )
175170 {
171+ // only cases where Name is allowed to not be specified is if Type or Tag parameters are
176172 if ( ! MyInvocation . BoundParameters . ContainsKey ( nameof ( Name ) ) )
177173 {
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
180185 {
181186 ThrowTerminatingError (
182187 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 ." ) ,
184189 "NameParameterNotProvided" ,
185190 ErrorCategory . InvalidOperation ,
186191 this ) ) ;
187192 }
188-
189- Name = new string [ ] { "*" } ;
190193 }
191194
192195 Name = Utils . ProcessNameWildcards ( Name , removeWildcardEntries : false , out string [ ] errorMsgs , out bool nameContainsWildcard ) ;
@@ -205,11 +208,48 @@ private void ProcessResourceNameParameterSet()
205208 if ( Name . Length == 0 )
206209 {
207210 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+ }
209246
210247 foreach ( PSResourceInfo pkg in _findHelper . FindByResourceName (
211248 name : Name ,
212249 type : Type ,
250+ versionRange : versionRange ,
251+ nugetVersion : nugetVersion ,
252+ versionType : versionType ,
213253 version : Version ,
214254 prerelease : Prerelease ,
215255 tag : Tag ,
0 commit comments