@@ -238,53 +238,71 @@ private static bool TryGetRepositoriesForPackage(
238238 out IReadOnlyList < SourceRepository > repositories )
239239 {
240240 var mappedNames = packageSourceMapping . GetConfiguredPackageSources ( packageId ) ;
241+ IReadOnlyList < SourceRepository > resultRepositories ;
241242
242- // Only validate insecure sources when mapping produced something
243243 if ( mappedNames . Count > 0 )
244244 {
245- var mappedRepos = new List < SourceRepository > ( mappedNames . Count ) ;
246- foreach ( var mappedName in mappedNames )
247- {
248- SourceRepository ? repo = null ;
249- for ( int i = 0 ; i < allRepos . Count ; i ++ )
250- {
251- if ( string . Equals ( allRepos [ i ] . PackageSource . Name , mappedName , StringComparison . OrdinalIgnoreCase ) )
252- {
253- repo = allRepos [ i ] ;
254- break ;
255- }
256- }
245+ resultRepositories = GetMappedRepositories ( mappedNames , allRepos , packageId , logger ) ;
257246
258- if ( repo != null )
259- {
260- mappedRepos . Add ( repo ) ;
261- }
262- else
263- {
264- logger . LogVerbose (
265- string . Format (
266- CultureInfo . CurrentCulture ,
267- Strings . PackageDownloadCommand_PackageSourceMapping_NoSuchSource ,
268- mappedName ,
269- packageId ) ) ;
270- }
271- }
272-
273- if ( DetectAndReportInsecureSources ( args . AllowInsecureConnections , mappedRepos . Select ( repo => repo . PackageSource ) , logger ) )
247+ if ( DetectAndReportInsecureSources ( args . AllowInsecureConnections , resultRepositories . Select ( repo => repo . PackageSource ) , logger ) )
274248 {
275249 repositories = [ ] ;
276250 return false ;
277251 }
278-
279- repositories = mappedRepos ;
280- return true ;
281252 }
282253 else
283254 {
284- // No mapping for this package: fall back to all sources
285- repositories = allRepos ;
286- return true ;
255+ // No mapping for this package
256+ resultRepositories = allRepos ;
287257 }
258+
259+ repositories = resultRepositories ;
260+ return true ;
261+ }
262+
263+ private static IReadOnlyList < SourceRepository > GetMappedRepositories (
264+ IReadOnlyList < string > mappedNames ,
265+ IReadOnlyList < SourceRepository > allRepos ,
266+ string packageId ,
267+ ILoggerWithColor logger )
268+ {
269+ var mappedRepos = new List < SourceRepository > ( mappedNames . Count ) ;
270+
271+ foreach ( var mappedName in mappedNames )
272+ {
273+ SourceRepository ? repo = FindRepositoryByName ( mappedName , allRepos ) ;
274+
275+ if ( repo != null )
276+ {
277+ mappedRepos . Add ( repo ) ;
278+ }
279+ else
280+ {
281+ logger . LogVerbose (
282+ string . Format (
283+ CultureInfo . CurrentCulture ,
284+ Strings . PackageDownloadCommand_PackageSourceMapping_NoSuchSource ,
285+ mappedName ,
286+ packageId ) ) ;
287+ }
288+ }
289+
290+ return mappedRepos ;
291+ }
292+
293+ private static SourceRepository ? FindRepositoryByName (
294+ string mappedName ,
295+ IReadOnlyList < SourceRepository > allRepos )
296+ {
297+ for ( int i = 0 ; i < allRepos . Count ; i ++ )
298+ {
299+ if ( string . Equals ( allRepos [ i ] . PackageSource . Name , mappedName , StringComparison . OrdinalIgnoreCase ) )
300+ {
301+ return allRepos [ i ] ;
302+ }
303+ }
304+
305+ return null ;
288306 }
289307
290308 private static async Task < bool > DownloadPackageAsync (
0 commit comments