Skip to content

Commit

Permalink
(chocolatey#1479) Rework set package config method
Browse files Browse the repository at this point in the history
This brings out the functionality from the
set_package_config_for_upgrade method to a more generic name, and adds
in another parameter to prepare for setting remembered args for
uninstall as well as upgrade. It also updates the logging and comments
to make them generic for both upgrades and uninstalls.

An alias/forwarding method is added for backwards compatibility.
  • Loading branch information
TheCakeIsNaOH committed Sep 25, 2022
1 parent 3892cfb commit e31fe33
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -988,15 +988,29 @@ public virtual void remove_rollback_directory_if_exists(string packageName)
/// <returns>The original unmodified configuration, so it can be reset after upgrade</returns>
protected virtual ChocolateyConfiguration set_package_config_for_upgrade(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
{
if (!config.Features.UseRememberedArgumentsForUpgrades || string.IsNullOrWhiteSpace(packageInfo.Arguments)) return config;
return set_package_config_from_remembered_args(config, packageInfo, CommandNameType.upgrade);
}

/// <summary>
/// Sets the configuration from remembered args
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="packageInfo">The package information.</param>
/// <param name="commandType">The command (like uninstall/upgrade) that is being run.</param>
/// <returns>The original unmodified configuration, so it can be reset</returns>
protected virtual ChocolateyConfiguration set_package_config_from_remembered_args(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo, CommandNameType commandType)
{
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;

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 for upgrade: {1}".format_with(packageInfo.Package.Id, packageArgumentsUnencrypted.escape_curly_braces()));
this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding remembered arguments: {1}".format_with(packageInfo.Package.Id, packageArgumentsUnencrypted.escape_curly_braces()));
}

var packageArgumentsSplit = packageArgumentsUnencrypted.Split(new[] { " --" }, StringSplitOptions.RemoveEmptyEntries);
Expand All @@ -1014,7 +1028,7 @@ protected virtual ChocolateyConfiguration set_package_config_for_upgrade(Chocola

if (sensitiveArgs)
{
this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding '{1}' to upgrade arguments. Values not shown due to detected sensitive arguments".format_with(packageInfo.Package.Id, optionName.escape_curly_braces()));
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()));
}
packageArguments.Add("--{0}{1}".format_with(optionName, string.IsNullOrWhiteSpace(optionValue) ? string.Empty : "=" + optionValue));
}
Expand Down

0 comments on commit e31fe33

Please sign in to comment.