From d73b4d56ab95d2d722705444cda645628d435af2 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, 25 insertions(+), 8 deletions(-) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs index dc468bfb80..94495df198 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 6a12ffaeed..b2037b2ebf 100644 --- a/src/chocolatey/infrastructure.app/services/NugetService.cs +++ b/src/chocolatey/infrastructure.app/services/NugetService.cs @@ -1662,8 +1662,8 @@ public virtual ConcurrentDictionary GetOutdated(Chocolate protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo, CommandNameType commandType = CommandNameType.Upgrade) { 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; + if (commandType == CommandNameType.Upgrade && !config.Features.UseRememberedArgumentsForUpgrades) return config; + if (commandType == CommandNameType.Uninstall && !config.Features.UseRememberedArgumentsForUninstalls) return config; var packageArgumentsUnencrypted = packageInfo.Arguments.ContainsSafe(" --") && packageInfo.Arguments.ToStringSafe().Length > 4 ? packageInfo.Arguments : NugetEncryptionUtility.DecryptString(packageInfo.Arguments); @@ -1734,12 +1734,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(); @@ -1754,10 +1749,18 @@ public virtual ChocolateyConfiguration GetPackageConfigFromRememberedArguments(C if (optionValue.StartsWith("'")) optionValue.UnquoteSafe(); } + //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)); } @@ -2383,6 +2386,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();