From e31fe336dc48c44762d14cf1a5ff3fda53a5e0cf Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Wed, 9 Mar 2022 23:55:22 -0600 Subject: [PATCH] (#1479) Rework set package config method This brings out the functionality from the set_package_config_for_upgrade method to a more generic name, and adds in another parameter to prepare for setting remembered args for uninstall as well as upgrade. It also updates the logging and comments to make them generic for both upgrades and uninstalls. An alias/forwarding method is added for backwards compatibility. --- .../services/NugetService.cs | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index 651cd84e68..827016bf24 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -988,7 +988,21 @@ public virtual void remove_rollback_directory_if_exists(string packageName) /// The original unmodified configuration, so it can be reset after upgrade protected virtual ChocolateyConfiguration set_package_config_for_upgrade(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo) { - if (!config.Features.UseRememberedArgumentsForUpgrades || string.IsNullOrWhiteSpace(packageInfo.Arguments)) return config; + return set_package_config_from_remembered_args(config, packageInfo, CommandNameType.upgrade); + } + + /// + /// Sets the configuration from remembered args + /// + /// The configuration. + /// The package information. + /// The command (like uninstall/upgrade) that is being run. + /// The original unmodified configuration, so it can be reset + protected virtual ChocolateyConfiguration set_package_config_from_remembered_args(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo, CommandNameType commandType) + { + if (string.IsNullOrWhiteSpace(packageInfo.Arguments)) return config; + if (commandType == CommandNameType.upgrade && !config.Features.UseRememberedArgumentsForUpgrades) return config; + if (commandType == CommandNameType.uninstall && !config.Features.UseRememberedArgumentsForUninstalls) return config; var packageArgumentsUnencrypted = packageInfo.Arguments.contains(" --") && packageInfo.Arguments.to_string().Length > 4 ? packageInfo.Arguments : NugetEncryptionUtility.DecryptString(packageInfo.Arguments); @@ -996,7 +1010,7 @@ protected virtual ChocolateyConfiguration set_package_config_for_upgrade(Chocola if (!ArgumentsUtility.arguments_contain_sensitive_information(packageArgumentsUnencrypted)) { sensitiveArgs = false; - this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding remembered arguments for upgrade: {1}".format_with(packageInfo.Package.Id, packageArgumentsUnencrypted.escape_curly_braces())); + this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding remembered arguments: {1}".format_with(packageInfo.Package.Id, packageArgumentsUnencrypted.escape_curly_braces())); } var packageArgumentsSplit = packageArgumentsUnencrypted.Split(new[] { " --" }, StringSplitOptions.RemoveEmptyEntries); @@ -1014,7 +1028,7 @@ protected virtual ChocolateyConfiguration set_package_config_for_upgrade(Chocola if (sensitiveArgs) { - this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding '{1}' to upgrade arguments. Values not shown due to detected sensitive arguments".format_with(packageInfo.Package.Id, optionName.escape_curly_braces())); + this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding '{1}' to arguments. Values not shown due to detected sensitive arguments".format_with(packageInfo.Package.Id, optionName.escape_curly_braces())); } packageArguments.Add("--{0}{1}".format_with(optionName, string.IsNullOrWhiteSpace(optionValue) ? string.Empty : "=" + optionValue)); }