public static void Run(UpdateSourceArgs args, Func <ILogger> getLogger) { var settings = RunnerHelper.GetSettings(args.Configfile); var sourceProvider = RunnerHelper.GetSourceProvider(settings); var existingSource = sourceProvider.GetPackageSourceByName(args.Name); if (existingSource == null) { throw new CommandException(Strings.SourcesCommandNoMatchingSourcesFound, args.Name); } if (!string.IsNullOrEmpty(args.Source) && !existingSource.Source.Equals(args.Source, StringComparison.OrdinalIgnoreCase)) { if (!PathValidator.IsValidSource(args.Source)) { throw new CommandException(Strings.SourcesCommandInvalidSource); } // If the user is updating the source, verify we don't have a duplicate. var duplicateSource = sourceProvider.GetPackageSourceBySource(args.Source); if (duplicateSource != null) { throw new CommandException(Strings.SourcesCommandUniqueSource); } existingSource = new Configuration.PackageSource(args.Source, existingSource.Name); // If the existing source is not http, warn the user if (existingSource.IsHttp && !existingSource.IsHttps) { getLogger().LogWarning(string.Format(CultureInfo.CurrentCulture, Strings.Warning_HttpServerUsage, "update source", args.Source)); } } RunnerHelper.ValidateCredentials(args.Username, args.Password, args.ValidAuthenticationTypes); if (!string.IsNullOrEmpty(args.Username)) { var hasExistingAuthTypes = existingSource.Credentials?.ValidAuthenticationTypes.Any() ?? false; if (hasExistingAuthTypes && string.IsNullOrEmpty(args.ValidAuthenticationTypes)) { getLogger().LogMinimal(string.Format(CultureInfo.CurrentCulture, Strings.SourcesCommandClearingExistingAuthTypes, args.Name)); } var credentials = Configuration.PackageSourceCredential.FromUserInput( args.Name, args.Username, args.Password, args.StorePasswordInClearText, args.ValidAuthenticationTypes); existingSource.Credentials = credentials; } sourceProvider.UpdatePackageSource(existingSource, updateCredentials: existingSource.Credentials != null, updateEnabled: false); getLogger().LogMinimal(string.Format(CultureInfo.CurrentCulture, Strings.SourcesCommandUpdateSuccessful, args.Name)); }
public static void Run(AddSourceArgs args, Func <ILogger> getLogger) { var settings = RunnerHelper.GetSettings(args.Configfile); var sourceProvider = RunnerHelper.GetSourceProvider(settings); if (string.IsNullOrEmpty(args.Name)) { // find first unused name of pattern: prefixN, where N is an integer. string defaultNamePrefix = Strings.Source_DefaultNamePrefix; var namesSet = sourceProvider.GetPackageSourceNamesMatchingNamePrefix(defaultNamePrefix); int i = 1; while (true) { var defaultNameToUse = defaultNamePrefix + i.ToString(); if (!namesSet.Contains(defaultNameToUse)) { args.Name = defaultNameToUse; break; } i++; } } else if (string.Equals(args.Name, Strings.ReservedPackageNameAll)) { throw new CommandException(Strings.SourcesCommandAllNameIsReserved); } // Make sure that the Source given is a valid one. if (!PathValidator.IsValidSource(args.Source)) { throw new CommandException(Strings.SourcesCommandInvalidSource); } RunnerHelper.ValidateCredentials(args.Username, args.Password, args.ValidAuthenticationTypes); // Check to see if we already have a registered source with the same name or source var existingSourceWithName = sourceProvider.GetPackageSourceByName(args.Name); if (existingSourceWithName != null) { throw new CommandException(Strings.SourcesCommandUniqueName); } var existingSourceWithSource = sourceProvider.GetPackageSourceBySource(args.Source); if (existingSourceWithSource != null) { throw new CommandException(Strings.SourcesCommandUniqueSource); } var newPackageSource = new Configuration.PackageSource(args.Source, args.Name); if (newPackageSource.IsHttp && !newPackageSource.IsHttps) { getLogger().LogWarning( string.Format(CultureInfo.CurrentCulture, Strings.Warning_HttpServerUsage, "add source", args.Source)); } if (!string.IsNullOrEmpty(args.Username)) { var credentials = Configuration.PackageSourceCredential.FromUserInput( args.Name, args.Username, args.Password, args.StorePasswordInClearText, args.ValidAuthenticationTypes); newPackageSource.Credentials = credentials; } sourceProvider.AddPackageSource(newPackageSource); getLogger().LogMinimal(string.Format(CultureInfo.CurrentCulture, Strings.SourcesCommandSourceAddedSuccessfully, args.Name)); }