private void UpdatePackageSource()
{
if (String.IsNullOrEmpty(Name))
{
throw new CommandLineException(NuGetResources.SourcesCommandNameRequired);
}
List<PackageSource> sourceList = _sourceProvider.LoadPackageSources().ToList();
int existingSourceIndex = sourceList.FindIndex(ps => Name.Equals(ps.Name, StringComparison.OrdinalIgnoreCase));
if (existingSourceIndex == -1)
{
throw new CommandLineException(NuGetResources.SourcesCommandNoMatchingSourcesFound, Name);
}
var existingSource = sourceList[existingSourceIndex];
if (!String.IsNullOrEmpty(Source) && !existingSource.Source.Equals(Source, StringComparison.OrdinalIgnoreCase))
{
if (!PathValidator.IsValidSource(Source))
{
throw new CommandLineException(NuGetResources.SourcesCommandInvalidSource);
}
// If the user is updating the source, verify we don't have a duplicate.
bool duplicateSource = sourceList.Any(ps => String.Equals(Source, ps.Source, StringComparison.OrdinalIgnoreCase));
if (duplicateSource)
{
throw new CommandLineException(NuGetResources.SourcesCommandUniqueSource);
}
existingSource = new PackageSource(Source, existingSource.Name);
}
ValidateCredentials();
sourceList.RemoveAt(existingSourceIndex);
existingSource.UserName = UserName;
existingSource.Password = Password;
sourceList.Insert(existingSourceIndex, existingSource);
_sourceProvider.SavePackageSources(sourceList);
Console.WriteLine(NuGetResources.SourcesCommandUpdateSuccessful, Name);
}