Skip to content

Commit

Permalink
(chocolatey#1479) Refactor set package config method
Browse files Browse the repository at this point in the history
This renames the set_package_config_for_upgrade to a more generic name,
and adds in another parameter to prepare for setting remembered args
for uninstall as well as upgrade
  • Loading branch information
TheCakeIsNaOH committed Mar 10, 2022
1 parent 58ba2ee commit 72924f3
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -819,7 +819,7 @@ public virtual void remove_rollback_directory_if_exists(string packageName)
continue;
}

set_package_config_for_upgrade(config, pkgInfo);
set_package_config_from_remembered_args(config, pkgInfo, CommandNameType.upgrade);

if (performAction)
{
Expand Down Expand Up @@ -957,22 +957,24 @@ public virtual void remove_rollback_directory_if_exists(string packageName)
}

/// <summary>
/// Sets the configuration for the package upgrade
/// Sets the configuration from remembered args
/// </summary>
/// <param name="config">The configuration.</param>
/// <param name="packageInfo">The package information.</param>
/// <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)
/// <returns>The original unmodified configuration, so it can be reset after upgrade or uninstall</returns>
protected virtual ChocolateyConfiguration set_package_config_from_remembered_args(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo, CommandNameType command)
{
if (!config.Features.UseRememberedArgumentsForUpgrades || string.IsNullOrWhiteSpace(packageInfo.Arguments)) return config;
if (string.IsNullOrWhiteSpace(packageInfo.Arguments)) return config;
if (command == CommandNameType.upgrade && !config.Features.UseRememberedArgumentsForUpgrades) return config;
if (command == 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 @@ -990,7 +992,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 72924f3

Please sign in to comment.