Skip to content

Commit 5dba563

Browse files
committed
(#2886) Switch remembered args to only change local configuration
This adds a new method, GetPackageConfigFromRememberedArguments which is similar to SetConfigFromRememberedArguments, but operates using a different method. First, a OptionSet is set up, so only the config that is passed in is modified, instead of the config that the global options parser was set with with. Second, it returns the modified configuration instead of the original configuration, because it is the local configuration being modified. Third, it has a more general name and changes log messages to be more general, so it later can more easily be reused for uninstall and export. This change fixes remembered arguments when Chocolatey is used as a library, like in ChocolateyGUI, where the config that is passed to the install_run method is not necessarily the same config object that was used to set up the global argument parser. The downside of using a new commandline parser is that it opens up the possibility of drift between the upgrade/global arguments and this added parser. However, this is not an issue because the format of the saved arguments is known, and any added arguments there would not work without being added here as well, which would be picked up during development.
1 parent 6630a2f commit 5dba563

File tree

1 file changed

+118
-7
lines changed

1 file changed

+118
-7
lines changed

src/chocolatey/infrastructure.app/services/NugetService.cs

+118-7
Original file line numberDiff line numberDiff line change
@@ -1044,7 +1044,7 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
10441044
continue;
10451045
}
10461046

1047-
SetConfigFromRememberedArguments(config, pkgInfo);
1047+
config = GetPackageConfigFromRememberedArguments(config, pkgInfo);
10481048
var pathResolver = NugetCommon.GetPathResolver(_fileSystem);
10491049
var nugetProject = new FolderNuGetProject(ApplicationParameters.PackagesLocation, pathResolver, NuGetFramework.AnyFramework);
10501050

@@ -1575,12 +1575,7 @@ public virtual ConcurrentDictionary<string, PackageResult> GetOutdated(Chocolate
15751575
return outdatedPackages;
15761576
}
15771577

1578-
/// <summary>
1579-
/// Sets the configuration for the package upgrade
1580-
/// </summary>
1581-
/// <param name="config">The configuration.</param>
1582-
/// <param name="packageInfo">The package information.</param>
1583-
/// <returns>The original unmodified configuration, so it can be reset after upgrade</returns>
1578+
[Obsolete("This method is deprecated and will be removed in v3.")]
15841579
protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
15851580
{
15861581
if (!config.Features.UseRememberedArgumentsForUpgrades || string.IsNullOrWhiteSpace(packageInfo.Arguments)) return config;
@@ -1629,6 +1624,122 @@ protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(Choco
16291624
return originalConfig;
16301625
}
16311626

1627+
/// <summary>
1628+
/// Gets the configuration from remembered arguments
1629+
/// </summary>
1630+
/// <param name="config">The original configuration.</param>
1631+
/// <param name="packageInfo">The package information.</param>
1632+
/// <returns>The modified configuration, so it can be used</returns>
1633+
public virtual ChocolateyConfiguration GetPackageConfigFromRememberedArguments(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
1634+
{
1635+
if (!config.Features.UseRememberedArgumentsForUpgrades || string.IsNullOrWhiteSpace(packageInfo.Arguments)) return config;
1636+
1637+
var packageArgumentsUnencrypted = packageInfo.Arguments.ContainsSafe(" --") && packageInfo.Arguments.ToStringSafe().Length > 4 ? packageInfo.Arguments : NugetEncryptionUtility.DecryptString(packageInfo.Arguments);
1638+
1639+
var sensitiveArgs = true;
1640+
if (!ArgumentsUtility.SensitiveArgumentsProvided(packageArgumentsUnencrypted))
1641+
{
1642+
sensitiveArgs = false;
1643+
this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding remembered arguments: {1}".FormatWith(packageInfo.Package.Id, packageArgumentsUnencrypted.EscapeCurlyBraces()));
1644+
}
1645+
1646+
var packageArgumentsSplit = packageArgumentsUnencrypted.Split(new[] { " --" }, StringSplitOptions.RemoveEmptyEntries);
1647+
var packageArguments = new List<string>();
1648+
foreach (var packageArgument in packageArgumentsSplit.OrEmpty())
1649+
{
1650+
var packageArgumentSplit = packageArgument.Split(new[] { '=' }, 2, StringSplitOptions.RemoveEmptyEntries);
1651+
var optionName = packageArgumentSplit[0].ToStringSafe();
1652+
var optionValue = string.Empty;
1653+
if (packageArgumentSplit.Length == 2)
1654+
{
1655+
optionValue = packageArgumentSplit[1].ToStringSafe().UnquoteSafe();
1656+
if (optionValue.StartsWith("'")) optionValue.UnquoteSafe();
1657+
}
1658+
1659+
if (sensitiveArgs)
1660+
{
1661+
this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding '{1}' to arguments. Values not shown due to detected sensitive arguments".FormatWith(packageInfo.Package.Id, optionName.EscapeCurlyBraces()));
1662+
}
1663+
packageArguments.Add("--{0}{1}".FormatWith(optionName, string.IsNullOrWhiteSpace(optionValue) ? string.Empty : "=" + optionValue));
1664+
}
1665+
1666+
var originalConfig = config.DeepCopy();
1667+
var rememberedOptionSet = new OptionSet();
1668+
1669+
rememberedOptionSet
1670+
.Add("pre|prerelease",
1671+
"Prerelease - Include Prereleases? Defaults to false.",
1672+
option => config.Prerelease = option != null)
1673+
.Add("i|ignoredependencies|ignore-dependencies",
1674+
"IgnoreDependencies - Ignore dependencies when installing package(s). Defaults to false.",
1675+
option => config.IgnoreDependencies = option != null)
1676+
.Add("x86|forcex86",
1677+
"ForceX86 - Force x86 (32bit) installation on 64 bit systems. Defaults to false.",
1678+
option => config.ForceX86 = option != null)
1679+
.Add("ia=|installargs=|install-args=|installarguments=|install-arguments=",
1680+
"InstallArguments - Install Arguments to pass to the native installer in the package. Defaults to unspecified.",
1681+
option => config.InstallArguments = option.remove_surrounding_quotes())
1682+
.Add("o|override|overrideargs|overridearguments|override-arguments",
1683+
"OverrideArguments - Should install arguments be used exclusively without appending to current package passed arguments? Defaults to false.",
1684+
option => config.OverrideArguments = option != null)
1685+
.Add("argsglobal|args-global|installargsglobal|install-args-global|applyargstodependencies|apply-args-to-dependencies|apply-install-arguments-to-dependencies",
1686+
"Apply Install Arguments To Dependencies - Should install arguments be applied to dependent packages? Defaults to false.",
1687+
option => config.ApplyInstallArgumentsToDependencies = option != null)
1688+
.Add("params=|parameters=|pkgparameters=|packageparameters=|package-parameters=",
1689+
"PackageParameters - Parameters to pass to the package. Defaults to unspecified.",
1690+
option => config.PackageParameters = option.remove_surrounding_quotes())
1691+
.Add("paramsglobal|params-global|packageparametersglobal|package-parameters-global|applyparamstodependencies|apply-params-to-dependencies|apply-package-parameters-to-dependencies",
1692+
"Apply Package Parameters To Dependencies - Should package parameters be applied to dependent packages? Defaults to false.",
1693+
option => config.ApplyPackageParametersToDependencies = option != null)
1694+
.Add("allowdowngrade|allow-downgrade",
1695+
"AllowDowngrade - Should an attempt at downgrading be allowed? Defaults to false.",
1696+
option => config.AllowDowngrade = option != null)
1697+
.Add("u=|user=",
1698+
"User - used with authenticated feeds. Defaults to empty.",
1699+
option => config.SourceCommand.Username = option.remove_surrounding_quotes())
1700+
.Add("p=|password=",
1701+
"Password - the user's password to the source. Defaults to empty.",
1702+
option => config.SourceCommand.Password = option.remove_surrounding_quotes())
1703+
.Add("cert=",
1704+
"Client certificate - PFX pathname for an x509 authenticated feeds. Defaults to empty. Available in 0.9.10+.",
1705+
option => config.SourceCommand.Certificate = option.remove_surrounding_quotes())
1706+
.Add("cp=|certpassword=",
1707+
"Certificate Password - the client certificate's password to the source. Defaults to empty. Available in 0.9.10+.",
1708+
option => config.SourceCommand.CertificatePassword = option.remove_surrounding_quotes())
1709+
.Add("timeout=|execution-timeout=",
1710+
"CommandExecutionTimeout (in seconds) - The time to allow a command to finish before timing out. Overrides the default execution timeout in the configuration of {0} seconds. '0' for infinite starting in 0.10.4.".format_with(config.CommandExecutionTimeoutSeconds.to_string()),
1711+
option =>
1712+
{
1713+
int timeout = 0;
1714+
var timeoutString = option.remove_surrounding_quotes();
1715+
int.TryParse(timeoutString, out timeout);
1716+
if (timeout > 0 || timeoutString.is_equal_to("0"))
1717+
{
1718+
config.CommandExecutionTimeoutSeconds = timeout;
1719+
}
1720+
})
1721+
.Add("c=|cache=|cachelocation=|cache-location=",
1722+
"CacheLocation - Location for download cache, defaults to %TEMP% or value in chocolatey.config file.",
1723+
option => config.CacheLocation = option.remove_surrounding_quotes())
1724+
.Add("use-system-powershell",
1725+
"UseSystemPowerShell - Execute PowerShell using an external process instead of the built-in PowerShell host. Should only be used when internal host is failing. Available in 0.9.10+.",
1726+
option => config.Features.UsePowerShellHost = option != null);
1727+
1728+
rememberedOptionSet.Parse(packageArguments);
1729+
1730+
// there may be overrides from the user running upgrade
1731+
if (!string.IsNullOrWhiteSpace(originalConfig.PackageParameters)) config.PackageParameters = originalConfig.PackageParameters;
1732+
if (!string.IsNullOrWhiteSpace(originalConfig.InstallArguments)) config.InstallArguments = originalConfig.InstallArguments;
1733+
if (!string.IsNullOrWhiteSpace(originalConfig.SourceCommand.Username)) config.SourceCommand.Username = originalConfig.SourceCommand.Username;
1734+
if (!string.IsNullOrWhiteSpace(originalConfig.SourceCommand.Password)) config.SourceCommand.Password = originalConfig.SourceCommand.Password;
1735+
if (!string.IsNullOrWhiteSpace(originalConfig.SourceCommand.Certificate)) config.SourceCommand.Certificate = originalConfig.SourceCommand.Certificate;
1736+
if (!string.IsNullOrWhiteSpace(originalConfig.SourceCommand.CertificatePassword)) config.SourceCommand.CertificatePassword = originalConfig.SourceCommand.CertificatePassword;
1737+
1738+
// We can't override cache location, execution timeout, or the switches because we don't know here if they were set on the command line
1739+
1740+
return config;
1741+
}
1742+
16321743
private bool HasMissingDependency(PackageResult package, List<PackageResult> allLocalPackages)
16331744
{
16341745
foreach (var dependency in package.PackageMetadata.DependencyGroups.SelectMany(d => d.Packages))

0 commit comments

Comments
 (0)