From d68a8ff8495ba15308cf7eea0611fb4ed31d9da2 Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Wed, 9 Mar 2022 23:56:31 -0600 Subject: [PATCH] (#1479) Implement using remembed arguments for uninstalls This adds the ability for the remembered argument to be reused for uninstalls. It can be controlled via the userememberedargs and ignorerememberedargs arguments, or via the previously added feature. --- .../commands/ChocolateyUninstallCommand.cs | 12 +++++++++++ .../services/NugetService.cs | 21 +++++++++++++------ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs index 16385eb1ad..47462fab76 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs @@ -133,6 +133,18 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon configuration.Features.ExitOnRebootDetected = false; } }) + .Add("userememberedargs|userememberedarguments|userememberedoptions|use-remembered-args|use-remembered-arguments|use-remembered-options", + "Use Remembered Options for Uninstall - use the arguments and options used during install/upgrade for uninstall. Does not override arguments being passed at runtime. Overrides the default feature '{0}' set to '{1}'. Available in 1.1.0+.".format_with(ApplicationParameters.Features.UseRememberedArgumentsForUninstalls, configuration.Features.UseRememberedArgumentsForUninstalls.to_string()), + option => + { + if (option != null) configuration.Features.UseRememberedArgumentsForUninstalls = true; + }) + .Add("ignorerememberedargs|ignorerememberedarguments|ignorerememberedoptions|ignore-remembered-args|ignore-remembered-arguments|ignore-remembered-options", + "Ignore Remembered Options for Uninstall - ignore the arguments and options used during install for Uninstall. Overrides the default feature '{0}' set to '{1}'. Available in 1.1.0+.".format_with(ApplicationParameters.Features.UseRememberedArgumentsForUninstalls, configuration.Features.UseRememberedArgumentsForUninstalls.to_string()), + option => + { + if (option != null) configuration.Features.UseRememberedArgumentsForUninstalls = false; + }) ; } diff --git a/src/chocolatey/infrastructure.app/services/NugetService.cs b/src/chocolatey/infrastructure.app/services/NugetService.cs index 76d72f2fa6..432578ced3 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -970,12 +970,7 @@ protected virtual ChocolateyConfiguration set_package_config_from_remembered_arg var packageArgumentsUnencrypted = packageInfo.Arguments.contains(" --") && packageInfo.Arguments.to_string().Length > 4 ? packageInfo.Arguments : NugetEncryptionUtility.DecryptString(packageInfo.Arguments); - var sensitiveArgs = true; - if (!ArgumentsUtility.arguments_contain_sensitive_information(packageArgumentsUnencrypted)) - { - sensitiveArgs = false; - this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding remembered arguments: {1}".format_with(packageInfo.Package.Id, packageArgumentsUnencrypted.escape_curly_braces())); - } + var sensitiveArgs = ArgumentsUtility.arguments_contain_sensitive_information(packageArgumentsUnencrypted); var packageArgumentsSplit = packageArgumentsUnencrypted.Split(new[] { " --" }, StringSplitOptions.RemoveEmptyEntries); var packageArguments = new List(); @@ -990,10 +985,18 @@ protected virtual ChocolateyConfiguration set_package_config_from_remembered_arg if (optionValue.StartsWith("'")) optionValue.remove_surrounding_quotes(); } + //Don't add install arguments during uninstall. We don't want a argument for the installer to be passed to the uninstaller. + if (string.Equals(optionName, "install-arguments") && command == CommandNameType.uninstall) continue; + if (string.Equals(optionName, "override-argument") && command == CommandNameType.uninstall) continue; + if (sensitiveArgs) { 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())); } + else + { + this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding '{1}' to arguments with a value of '{2}'".format_with(packageInfo.Package.Id, optionName.escape_curly_braces(), optionValue.escape_curly_braces())); + } packageArguments.Add("--{0}{1}".format_with(optionName, string.IsNullOrWhiteSpace(optionValue) ? string.Empty : "=" + optionValue)); } @@ -1441,6 +1444,9 @@ public virtual ConcurrentDictionary uninstall_run(Chocola foreach (var packageVersion in packageVersionsToRemove) { + // reset config each time through + config = originalConfig.deep_copy(); + var pkgInfo = _packageInfoService.get_package_information(packageVersion); if (pkgInfo != null && pkgInfo.IsPinned) { @@ -1452,6 +1458,8 @@ public virtual ConcurrentDictionary uninstall_run(Chocola continue; } + set_package_config_from_remembered_args(config, pkgInfo, CommandNameType.uninstall); + if (performAction) { try @@ -1498,6 +1506,7 @@ public virtual ConcurrentDictionary uninstall_run(Chocola var result = packageUninstalls.GetOrAdd(packageVersion.Id.to_lower() + "." + packageVersion.Version.to_string(), new PackageResult(packageVersion, _fileSystem.combine_paths(ApplicationParameters.PackagesLocation, packageVersion.Id))); if (continueAction != null) continueAction.Invoke(result); } + resetConfigAction.Invoke(originalConfig); } }