Skip to content

Commit 8bcb5ad

Browse files
committed
(#2886) Switch remembered args to only change local configuration
This renames set_package_config_for_upgrade to get_package_config_from_remembered_args and changes how it operates. First, a option set 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 renames the function 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 8bcb5ad

1 file changed

Lines changed: 73 additions & 11 deletions

File tree

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

Lines changed: 73 additions & 11 deletions
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

@@ -1576,12 +1576,12 @@ public virtual ConcurrentDictionary<string, PackageResult> GetOutdated(Chocolate
15761576
}
15771577

15781578
/// <summary>
1579-
/// Sets the configuration for the package upgrade
1579+
/// Gets the configuration from remembered arguments
15801580
/// </summary>
1581-
/// <param name="config">The configuration.</param>
1581+
/// <param name="config">The original configuration.</param>
15821582
/// <param name="packageInfo">The package information.</param>
1583-
/// <returns>The original unmodified configuration, so it can be reset after upgrade</returns>
1584-
protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
1583+
/// <returns>The modified configuration, so it can be used</returns>
1584+
protected virtual ChocolateyConfiguration GetPackageConfigFromRememberedArguments(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
15851585
{
15861586
if (!config.Features.UseRememberedArgumentsForUpgrades || string.IsNullOrWhiteSpace(packageInfo.Arguments)) return config;
15871587

@@ -1591,7 +1591,7 @@ protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(Choco
15911591
if (!ArgumentsUtility.SensitiveArgumentsProvided(packageArgumentsUnencrypted))
15921592
{
15931593
sensitiveArgs = false;
1594-
this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding remembered arguments for upgrade: {1}".FormatWith(packageInfo.Package.Id, packageArgumentsUnencrypted.EscapeCurlyBraces()));
1594+
this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding remembered arguments: {1}".FormatWith(packageInfo.Package.Id, packageArgumentsUnencrypted.EscapeCurlyBraces()));
15951595
}
15961596

15971597
var packageArgumentsSplit = packageArgumentsUnencrypted.Split(new[] { " --" }, StringSplitOptions.RemoveEmptyEntries);
@@ -1609,14 +1609,74 @@ protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(Choco
16091609

16101610
if (sensitiveArgs)
16111611
{
1612-
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()));
1612+
this.Log().Debug(ChocolateyLoggers.Verbose, "{0} - Adding '{1}' to arguments. Values not shown due to detected sensitive arguments".FormatWith(packageInfo.Package.Id, optionName.EscapeCurlyBraces()));
16131613
}
16141614
packageArguments.Add("--{0}{1}".FormatWith(optionName, string.IsNullOrWhiteSpace(optionValue) ? string.Empty : "=" + optionValue));
16151615
}
16161616

16171617
var originalConfig = config.DeepCopy();
1618-
// this changes config globally
1619-
ConfigurationOptions.OptionSet.Parse(packageArguments);
1618+
var rememberedOptionSet = new OptionSet();
1619+
1620+
rememberedOptionSet
1621+
.Add("pre|prerelease",
1622+
"Prerelease - Include Prereleases? Defaults to false.",
1623+
option => config.Prerelease = option != null)
1624+
.Add("i|ignoredependencies|ignore-dependencies",
1625+
"IgnoreDependencies - Ignore dependencies when installing package(s). Defaults to false.",
1626+
option => config.IgnoreDependencies = option != null)
1627+
.Add("x86|forcex86",
1628+
"ForceX86 - Force x86 (32bit) installation on 64 bit systems. Defaults to false.",
1629+
option => config.ForceX86 = option != null)
1630+
.Add("ia=|installargs=|install-args=|installarguments=|install-arguments=",
1631+
"InstallArguments - Install Arguments to pass to the native installer in the package. Defaults to unspecified.",
1632+
option => config.InstallArguments = option.remove_surrounding_quotes())
1633+
.Add("o|override|overrideargs|overridearguments|override-arguments",
1634+
"OverrideArguments - Should install arguments be used exclusively without appending to current package passed arguments? Defaults to false.",
1635+
option => config.OverrideArguments = option != null)
1636+
.Add("argsglobal|args-global|installargsglobal|install-args-global|applyargstodependencies|apply-args-to-dependencies|apply-install-arguments-to-dependencies",
1637+
"Apply Install Arguments To Dependencies - Should install arguments be applied to dependent packages? Defaults to false.",
1638+
option => config.ApplyInstallArgumentsToDependencies = option != null)
1639+
.Add("params=|parameters=|pkgparameters=|packageparameters=|package-parameters=",
1640+
"PackageParameters - Parameters to pass to the package. Defaults to unspecified.",
1641+
option => config.PackageParameters = option.remove_surrounding_quotes())
1642+
.Add("paramsglobal|params-global|packageparametersglobal|package-parameters-global|applyparamstodependencies|apply-params-to-dependencies|apply-package-parameters-to-dependencies",
1643+
"Apply Package Parameters To Dependencies - Should package parameters be applied to dependent packages? Defaults to false.",
1644+
option => config.ApplyPackageParametersToDependencies = option != null)
1645+
.Add("allowdowngrade|allow-downgrade",
1646+
"AllowDowngrade - Should an attempt at downgrading be allowed? Defaults to false.",
1647+
option => config.AllowDowngrade = option != null)
1648+
.Add("u=|user=",
1649+
"User - used with authenticated feeds. Defaults to empty.",
1650+
option => config.SourceCommand.Username = option.remove_surrounding_quotes())
1651+
.Add("p=|password=",
1652+
"Password - the user's password to the source. Defaults to empty.",
1653+
option => config.SourceCommand.Password = option.remove_surrounding_quotes())
1654+
.Add("cert=",
1655+
"Client certificate - PFX pathname for an x509 authenticated feeds. Defaults to empty. Available in 0.9.10+.",
1656+
option => config.SourceCommand.Certificate = option.remove_surrounding_quotes())
1657+
.Add("cp=|certpassword=",
1658+
"Certificate Password - the client certificate's password to the source. Defaults to empty. Available in 0.9.10+.",
1659+
option => config.SourceCommand.CertificatePassword = option.remove_surrounding_quotes())
1660+
.Add("timeout=|execution-timeout=",
1661+
"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()),
1662+
option =>
1663+
{
1664+
int timeout = 0;
1665+
var timeoutString = option.remove_surrounding_quotes();
1666+
int.TryParse(timeoutString, out timeout);
1667+
if (timeout > 0 || timeoutString.is_equal_to("0"))
1668+
{
1669+
config.CommandExecutionTimeoutSeconds = timeout;
1670+
}
1671+
})
1672+
.Add("c=|cache=|cachelocation=|cache-location=",
1673+
"CacheLocation - Location for download cache, defaults to %TEMP% or value in chocolatey.config file.",
1674+
option => config.CacheLocation = option.remove_surrounding_quotes())
1675+
.Add("use-system-powershell",
1676+
"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+.",
1677+
option => config.Features.UsePowerShellHost = option != null);
1678+
1679+
rememberedOptionSet.Parse(packageArguments);
16201680

16211681
// there may be overrides from the user running upgrade
16221682
if (!string.IsNullOrWhiteSpace(originalConfig.PackageParameters)) config.PackageParameters = originalConfig.PackageParameters;
@@ -1626,7 +1686,9 @@ protected virtual ChocolateyConfiguration SetConfigFromRememberedArguments(Choco
16261686
if (!string.IsNullOrWhiteSpace(originalConfig.SourceCommand.Certificate)) config.SourceCommand.Certificate = originalConfig.SourceCommand.Certificate;
16271687
if (!string.IsNullOrWhiteSpace(originalConfig.SourceCommand.CertificatePassword)) config.SourceCommand.CertificatePassword = originalConfig.SourceCommand.CertificatePassword;
16281688

1629-
return originalConfig;
1689+
// 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
1690+
1691+
return config;
16301692
}
16311693

16321694
private bool HasMissingDependency(PackageResult package, List<PackageResult> allLocalPackages)
@@ -2724,7 +2786,7 @@ public virtual ConcurrentDictionary<string, PackageResult> get_outdated(Chocolat
27242786

27252787
[Obsolete("This overload is deprecated and will be removed in v3.")]
27262788
protected virtual ChocolateyConfiguration set_package_config_for_upgrade(ChocolateyConfiguration config, ChocolateyPackageInformation packageInfo)
2727-
=> SetConfigFromRememberedArguments(config, packageInfo);
2789+
=> GetPackageConfigFromRememberedArguments(config, packageInfo);
27282790

27292791
[Obsolete("This overload is deprecated and will be removed in v3.")]
27302792
protected virtual void ensure_package_files_have_compatible_attributes(ChocolateyConfiguration config, IPackageMetadata installedPackage)

0 commit comments

Comments
 (0)