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 Mar 11, 2022
1 parent 102d90a commit 4b97897
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
})
;
}

Expand Down
20 changes: 14 additions & 6 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>();
Expand All @@ -990,10 +985,17 @@ 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 (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));
}

Expand Down Expand Up @@ -1441,6 +1443,9 @@ public void uninstall_noop(ChocolateyConfiguration config, Action<PackageResult>

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)
{
Expand All @@ -1452,6 +1457,8 @@ public void uninstall_noop(ChocolateyConfiguration config, Action<PackageResult>
continue;
}

set_package_config_from_remembered_args(config, pkgInfo, CommandNameType.uninstall);

if (performAction)
{
try
Expand Down Expand Up @@ -1498,6 +1505,7 @@ public void uninstall_noop(ChocolateyConfiguration config, Action<PackageResult>
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);
}
}

Expand Down

0 comments on commit 4b97897

Please sign in to comment.