From f6227e7abe6937044c736ce7522e6485907ba9b2 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 | 26 ++++++++++++++----- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs index 8abb40f6f4..1b6abee572 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs @@ -146,6 +146,18 @@ public virtual void ConfigureArgumentParser(OptionSet optionSet, ChocolateyConfi "Skip hooks - Do not run hook scripts. Available in 1.2.0+", option => configuration.SkipHookScripts = option != null ) + .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 2.4.0+.".FormatWith(ApplicationParameters.Features.UseRememberedArgumentsForUninstalls, configuration.Features.UseRememberedArgumentsForUninstalls.ToString()), + 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 2.4.0+.".FormatWith(ApplicationParameters.Features.UseRememberedArgumentsForUninstalls, configuration.Features.UseRememberedArgumentsForUninstalls.ToString()), + 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 af758ff5a9..3b121c976a 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -1886,12 +1886,7 @@ public virtual ChocolateyConfiguration GetPackageConfigFromRememberedArguments(C var packageArgumentsUnencrypted = packageInfo.Arguments.ContainsSafe(" --") && packageInfo.Arguments.ToStringSafe().Length > 4 ? packageInfo.Arguments : NugetEncryptionUtility.DecryptString(packageInfo.Arguments); - var sensitiveArgs = true; - if (!ArgumentsUtility.SensitiveArgumentsProvided(packageArgumentsUnencrypted)) - { - sensitiveArgs = false; - this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding remembered arguments: {1}".FormatWith(packageInfo.Package.Id, packageArgumentsUnencrypted.EscapeCurlyBraces())); - } + var sensitiveArgs = ArgumentsUtility.SensitiveArgumentsProvided(packageArgumentsUnencrypted); var packageArgumentsSplit = packageArgumentsUnencrypted.Split(new[] { " --" }, StringSplitOptions.RemoveEmptyEntries); var packageArguments = new List(); @@ -1909,10 +1904,27 @@ public virtual ChocolateyConfiguration GetPackageConfigFromRememberedArguments(C } } + //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", StringComparison.OrdinalIgnoreCase) && + commandType == CommandNameType.Uninstall) + { + continue; + } + + if (string.Equals(optionName, "override-argument", StringComparison.OrdinalIgnoreCase) && + commandType == CommandNameType.Uninstall) + { + continue; + } + if (sensitiveArgs) { this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding '{1}' to arguments. Values not shown due to detected sensitive arguments".FormatWith(packageInfo.Package.Id, optionName.EscapeCurlyBraces())); } + else + { + this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding '{1}' to arguments with a value of '{2}'".FormatWith(packageInfo.Package.Id, optionName.EscapeCurlyBraces(), optionValue.EscapeCurlyBraces())); + } packageArguments.Add("--{0}{1}".FormatWith(optionName, string.IsNullOrWhiteSpace(optionValue) ? string.Empty : "=" + optionValue)); } @@ -2598,6 +2610,8 @@ public virtual ConcurrentDictionary Uninstall(ChocolateyC continue; } + config = GetPackageConfigFromRememberedArguments(config, pkgInfo); + if (performAction) { var allPackagesIdentities = allLocalPackages.Where(p => !p.Identity.Equals(installedPackage)).Select(p => p.SearchMetadata.Identity).ToList();