Skip to content

Commit

Permalink
(chocolatey#1479) Implement using remembed arguments for uninstalls
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TheCakeIsNaOH committed Jan 10, 2024
1 parent 290644c commit d73b4d5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
;
}

Expand Down
21 changes: 13 additions & 8 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1662,8 +1662,8 @@ public virtual ConcurrentDictionary<string, PackageResult> 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);

Expand Down Expand Up @@ -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<string>();
Expand All @@ -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));
}

Expand Down Expand Up @@ -2383,6 +2386,8 @@ public virtual ConcurrentDictionary<string, PackageResult> Uninstall(ChocolateyC
continue;
}

config = GetPackageConfigFromRememberedArguments(config, pkgInfo);

if (performAction)
{
var allPackagesIdentities = allLocalPackages.Where(p => !p.Identity.Equals(installedPackage)).Select(p => p.SearchMetadata.Identity).ToList();
Expand Down

0 comments on commit d73b4d5

Please sign in to comment.