diff --git a/src/chocolatey.console/Program.cs b/src/chocolatey.console/Program.cs index 5cb43e8ff4..2250280427 100644 --- a/src/chocolatey.console/Program.cs +++ b/src/chocolatey.console/Program.cs @@ -82,8 +82,6 @@ that chocolatey.licensed.dll exists at } var container = SimpleInjectorContainer.Container; - add_or_remove_licensed_source(license, container); - var config = container.GetInstance(); var fileSystem = container.GetInstance(); @@ -224,37 +222,6 @@ private static void remove_old_chocolatey_exe(IFileSystem fileSystem) } } - private static void add_or_remove_licensed_source(ChocolateyLicense license, Container container) - { - var addOrUpdate = license.IsValid; - var config = new ChocolateyConfiguration { - RegularOutput = false, - QuietOutput = true, - }; - - var sourceService = container.GetInstance(); - var sources = sourceService.source_list(config); - - config.SourceCommand.Name = ApplicationParameters.ChocolateyLicensedFeedSourceName; - config.Sources = ApplicationParameters.ChocolateyLicensedFeedSource; - config.SourceCommand.Username = "customer"; - config.SourceCommand.Password = license.Id; - config.SourceCommand.Priority = 10; - - if (addOrUpdate && !sources.Any(s => - s.Id.is_equal_to(ApplicationParameters.ChocolateyLicensedFeedSourceName) - && s.Authenticated) - ) - { - sourceService.source_add(config); - } - - if (!addOrUpdate) - { - sourceService.source_remove(config); - } - } - private static void pause_execution_if_debug() { #if DEBUG diff --git a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs index 63291deb24..3a50ac18a2 100644 --- a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs +++ b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs @@ -70,6 +70,8 @@ public static void set_up_configuration(IList args, ChocolateyConfigurat var fileSystem = container.GetInstance(); var xmlService = container.GetInstance(); var configFileSettings = get_config_file_settings(fileSystem, xmlService); + // must be done prior to setting the file configuration + add_or_remove_licensed_source(license, configFileSettings); set_file_configuration(config, configFileSettings, fileSystem, notifyWarnLoggingAction); ConfigurationOptions.reset_options(); set_global_options(args, config, container); @@ -77,6 +79,7 @@ public static void set_up_configuration(IList args, ChocolateyConfigurat set_environment_variables(config); // must be done last for overrides set_licensed_options(config, license, configFileSettings); + // save all changes if there are any set_config_file_settings(configFileSettings, xmlService); } @@ -98,6 +101,36 @@ private static void set_config_file_settings(ConfigFileSettings configFileSettin logWarningInsteadOfError: true); } + private static void add_or_remove_licensed_source(ChocolateyLicense license, ConfigFileSettings configFileSettings) + { + // do not enable or disable the source, in case the user has disabled it + var addOrUpdate = license.IsValid; + var sources = configFileSettings.Sources.Where(s => !s.Disabled).or_empty_list_if_null().ToList(); + + var configSource = new ConfigFileSourceSetting + { + Id = ApplicationParameters.ChocolateyLicensedFeedSourceName, + Value = ApplicationParameters.ChocolateyLicensedFeedSource, + UserName = "customer", + Password = NugetEncryptionUtility.EncryptString(license.Id), + Priority = 10 + }; + + if (addOrUpdate && !sources.Any(s => + s.Id.is_equal_to(ApplicationParameters.ChocolateyLicensedFeedSourceName) + && NugetEncryptionUtility.DecryptString(s.Password).is_equal_to(license.Id) + ) + ) + { + configFileSettings.Sources.Add(configSource); + } + + if (!addOrUpdate) + { + configFileSettings.Sources.RemoveWhere(s => s.Id.is_equal_to(configSource.Id)); + } + } + private static void set_file_configuration(ChocolateyConfiguration config, ConfigFileSettings configFileSettings, IFileSystem fileSystem, Action notifyWarnLoggingAction) { var sources = new StringBuilder();