@@ -105,10 +105,16 @@ class RegisterPSResourceRepository : PSCmdlet
105105 public PSCredential ProxyCredential { get ; set ; }
106106
107107 /// <summary>
108- /// When specified, displays the succcessfully registered repository and its information
108+ /// When specified, displays the succcessfully registered repository and its information.
109109 /// </summary>
110110 [ Parameter ]
111111 public SwitchParameter PassThru { get ; set ; }
112+
113+ /// <summary>
114+ /// When specified, will overwrite information for any existing repository with the same name.
115+ /// </summary>
116+ [ Parameter ]
117+ public SwitchParameter Force { get ; set ; }
112118
113119 #endregion
114120
@@ -144,7 +150,16 @@ protected override void ProcessRecord()
144150
145151 try
146152 {
147- items . Add ( NameParameterSetHelper ( Name , _uri , Priority , Trusted , CredentialInfo ) ) ;
153+ items . Add ( RepositorySettings . AddRepository ( Name , _uri , Priority , Trusted , CredentialInfo , Force , this , out string errorMsg ) ) ;
154+
155+ if ( ! string . IsNullOrEmpty ( errorMsg ) )
156+ {
157+ ThrowTerminatingError ( new ErrorRecord (
158+ new PSInvalidOperationException ( errorMsg ) ,
159+ "ErrorInNameParameterSet" ,
160+ ErrorCategory . InvalidArgument ,
161+ this ) ) ;
162+ }
148163 }
149164 catch ( Exception e )
150165 {
@@ -201,71 +216,30 @@ protected override void ProcessRecord()
201216 }
202217 }
203218
204- private PSRepositoryInfo AddToRepositoryStoreHelper ( string repoName , Uri repoUri , int repoPriority , bool repoTrusted , PSCredentialInfo repoCredentialInfo )
205- {
206- // remove trailing and leading whitespaces, and if Name is just whitespace Name should become null now and be caught by following condition
207- repoName = repoName . Trim ( ' ' ) ;
208- if ( String . IsNullOrEmpty ( repoName ) || repoName . Contains ( "*" ) )
209- {
210- throw new ArgumentException ( "Name cannot be null/empty, contain asterisk or be just whitespace" ) ;
211- }
212-
213- if ( repoUri == null || ! ( repoUri . Scheme == System . Uri . UriSchemeHttp || repoUri . Scheme == System . Uri . UriSchemeHttps || repoUri . Scheme == System . Uri . UriSchemeFtp || repoUri . Scheme == System . Uri . UriSchemeFile ) )
214- {
215- throw new ArgumentException ( "Invalid Uri, must be one of the following Uri schemes: HTTPS, HTTP, FTP, File Based" ) ;
216- }
217-
218- if ( repoCredentialInfo != null )
219- {
220- bool isSecretManagementModuleAvailable = Utils . IsSecretManagementModuleAvailable ( repoName , this ) ;
221-
222- if ( repoCredentialInfo . Credential != null )
223- {
224- if ( ! isSecretManagementModuleAvailable )
225- {
226- ThrowTerminatingError ( new ErrorRecord (
227- new PSInvalidOperationException ( $ "Microsoft.PowerShell.SecretManagement module is not found, but is required for saving PSResourceRepository { repoName } 's Credential in a vault.") ,
228- "RepositoryCredentialSecretManagementUnavailableModule" ,
229- ErrorCategory . ResourceUnavailable ,
230- this ) ) ;
231- }
232- else
233- {
234- Utils . SaveRepositoryCredentialToSecretManagementVault ( repoName , repoCredentialInfo , this ) ;
235- }
236- }
237-
238- if ( ! isSecretManagementModuleAvailable )
239- {
240- WriteWarning ( $ "Microsoft.PowerShell.SecretManagement module cannot be found. Make sure it is installed before performing PSResource operations in order to successfully authenticate to PSResourceRepository \" { repoName } \" with its CredentialInfo.") ;
241- }
242- }
243-
244- WriteVerbose ( "All required values to add to repository provided, calling internal Add() API now" ) ;
245- if ( ! ShouldProcess ( repoName , "Register repository to repository store" ) )
246- {
247- return null ;
248- }
249-
250- return RepositorySettings . Add ( repoName , repoUri , repoPriority , repoTrusted , repoCredentialInfo ) ;
251- }
252-
253- private PSRepositoryInfo NameParameterSetHelper ( string repoName , Uri repoUri , int repoPriority , bool repoTrusted , PSCredentialInfo repoCredentialInfo )
254- {
255- if ( repoName . Equals ( "PSGallery" , StringComparison . OrdinalIgnoreCase ) )
256- {
257- WriteVerbose ( "Provided Name (NameParameterSet) but with invalid value of PSGallery" ) ;
258- throw new ArgumentException ( "Cannot register PSGallery with -Name parameter. Try: Register-PSResourceRepository -PSGallery" ) ;
259- }
260-
261- return AddToRepositoryStoreHelper ( repoName , repoUri , repoPriority , repoTrusted , repoCredentialInfo ) ;
262- }
263219
264220 private PSRepositoryInfo PSGalleryParameterSetHelper ( int repoPriority , bool repoTrusted )
265221 {
266222 Uri psGalleryUri = new Uri ( PSGalleryRepoUri ) ;
267223 WriteVerbose ( "(PSGallerySet) internal name and uri values for Add() API are hardcoded and validated, priority and trusted values, if passed in, also validated" ) ;
268- return AddToRepositoryStoreHelper ( PSGalleryRepoName , psGalleryUri , repoPriority , repoTrusted , repoCredentialInfo : null ) ;
224+ var addedRepo = RepositorySettings . AddToRepositoryStore ( PSGalleryRepoName ,
225+ psGalleryUri ,
226+ repoPriority ,
227+ repoTrusted ,
228+ repoCredentialInfo : null ,
229+ Force ,
230+ this ,
231+ out string errorMsg ) ;
232+
233+ if ( ! string . IsNullOrEmpty ( errorMsg ) )
234+ {
235+ ThrowTerminatingError ( new ErrorRecord (
236+ new PSInvalidOperationException ( errorMsg ) ,
237+ "RepositoryCredentialSecretManagementUnavailableModule" ,
238+ ErrorCategory . ResourceUnavailable ,
239+ this ) ) ;
240+ }
241+
242+ return addedRepo ;
269243 }
270244
271245 private List < PSRepositoryInfo > RepositoriesParameterSetHelper ( )
@@ -316,7 +290,7 @@ private List<PSRepositoryInfo> RepositoriesParameterSetHelper()
316290
317291 private PSRepositoryInfo RepoValidationHelper ( Hashtable repo )
318292 {
319- if ( ! repo . ContainsKey ( "Name" ) || String . IsNullOrEmpty ( repo [ "Name" ] . ToString ( ) ) )
293+ if ( ! repo . ContainsKey ( "Name" ) || repo [ "Name" ] == null || String . IsNullOrWhiteSpace ( repo [ "Name" ] . ToString ( ) ) )
320294 {
321295 WriteError ( new ErrorRecord (
322296 new PSInvalidOperationException ( "Repository name cannot be null" ) ,
@@ -336,7 +310,7 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
336310 return null ;
337311 }
338312
339- if ( ! repo . ContainsKey ( "Uri" ) || String . IsNullOrEmpty ( repo [ "Uri" ] . ToString ( ) ) )
313+ if ( ! repo . ContainsKey ( "Uri" ) || repo [ "Uri" ] == null || String . IsNullOrEmpty ( repo [ "Uri" ] . ToString ( ) ) )
340314 {
341315 WriteError ( new ErrorRecord (
342316 new PSInvalidOperationException ( "Repository Uri cannot be null" ) ,
@@ -369,11 +343,25 @@ private PSRepositoryInfo RepoValidationHelper(Hashtable repo)
369343 try
370344 {
371345 WriteVerbose ( String . Format ( "(RepositoriesParameterSet): on repo: {0}. Registers Name based repository" , repo [ "Name" ] ) ) ;
372- return NameParameterSetHelper ( repo [ "Name" ] . ToString ( ) ,
346+ var addedRepo = RepositorySettings . AddRepository ( repo [ "Name" ] . ToString ( ) ,
373347 repoUri ,
374348 repo . ContainsKey ( "Priority" ) ? Convert . ToInt32 ( repo [ "Priority" ] . ToString ( ) ) : DefaultPriority ,
375349 repo . ContainsKey ( "Trusted" ) ? Convert . ToBoolean ( repo [ "Trusted" ] . ToString ( ) ) : DefaultTrusted ,
376- repoCredentialInfo ) ;
350+ repoCredentialInfo ,
351+ Force ,
352+ this ,
353+ out string errorMsg ) ;
354+
355+ if ( ! string . IsNullOrEmpty ( errorMsg ) )
356+ {
357+ ThrowTerminatingError ( new ErrorRecord (
358+ new PSInvalidOperationException ( errorMsg ) ,
359+ "RegisterRepositoryError" ,
360+ ErrorCategory . ResourceUnavailable ,
361+ this ) ) ;
362+ }
363+
364+ return addedRepo ;
377365 }
378366 catch ( Exception e )
379367 {
0 commit comments