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 Jan 10, 2024
1 parent 1a012ce commit 004f51e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/chocolatey/infrastructure.app/services/INugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ public interface INugetService : ISourceRunner
/// </summary>
/// <param name="config">The original configuration.</param>
/// <param name="packageInfo">The package information.</param>
/// <param name="commandType">The command type</param>
/// <returns>The modified configuration, so it can be used</returns>
ChocolateyConfiguration GetPackageConfigFromRememberedArguments(ChocolateyConfiguration config,
ChocolateyPackageInformation packageInfo);
ChocolateyPackageInformation packageInfo, CommandNameType commandType = CommandNameType.Upgrade);

#pragma warning disable IDE1006
[Obsolete("This overload is deprecated and will be removed in v3.")]
Expand Down
17 changes: 11 additions & 6 deletions src/chocolatey/infrastructure.app/services/NugetService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1659,17 +1659,19 @@ public virtual ConcurrentDictionary<string, PackageResult> GetOutdated(Chocolate
}

[Obsolete("This method is deprecated and will be removed in v3.")]
protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo, CommandNameType commandType = CommandNameType.Upgrade)
{
if (!config.Features.UseRememberedArgumentsForUpgrades || string.IsNullOrWhiteSpace(packageInfo.Arguments)) return config;
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.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 for upgrade: {1}".FormatWith(packageInfo.Package.Id, packageArgumentsUnencrypted.EscapeCurlyBraces()));
this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding remembered arguments: {1}".FormatWith(packageInfo.Package.Id, packageArgumentsUnencrypted.EscapeCurlyBraces()));
}

var packageArgumentsSplit = packageArgumentsUnencrypted.Split(new[] { " --" }, StringSplitOptions.RemoveEmptyEntries);
Expand All @@ -1687,7 +1689,7 @@ protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(Choco

if (sensitiveArgs)
{
this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding '{1}' to upgrade arguments. Values not shown due to detected sensitive arguments".FormatWith(packageInfo.Package.Id, optionName.EscapeCurlyBraces()));
this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding '{1}' to arguments. Values not shown due to detected sensitive arguments".FormatWith(packageInfo.Package.Id, optionName.EscapeCurlyBraces()));
}
packageArguments.Add("--{0}{1}".FormatWith(optionName, string.IsNullOrWhiteSpace(optionValue) ? string.Empty : "=" + optionValue));
}
Expand Down Expand Up @@ -1722,10 +1724,13 @@ protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(Choco
/// </summary>
/// <param name="config">The original configuration.</param>
/// <param name="packageInfo">The package information.</param>
/// <param name="commandType">The command type</param>
/// <returns>The modified configuration, so it can be used</returns>
public virtual ChocolateyConfiguration GetPackageConfigFromRememberedArguments(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
public virtual ChocolateyConfiguration GetPackageConfigFromRememberedArguments(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo, CommandNameType commandType = CommandNameType.Upgrade)
{
if (!config.Features.UseRememberedArgumentsForUpgrades || string.IsNullOrWhiteSpace(packageInfo.Arguments)) return config;
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.ContainsSafe(" --") && packageInfo.Arguments.ToStringSafe().Length > 4 ? packageInfo.Arguments : NugetEncryptionUtility.DecryptString(packageInfo.Arguments);

Expand Down

0 comments on commit 004f51e

Please sign in to comment.