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 Apr 28, 2024
1 parent f65c262 commit f6227e7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 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
26 changes: 20 additions & 6 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
Expand All @@ -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));
}

Expand Down Expand Up @@ -2598,6 +2610,8 @@ public void UninstallDryRun(ChocolateyConfiguration config, Action<PackageResult
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 f6227e7

Please sign in to comment.