Skip to content

Commit

Permalink
(chocolatey#2200) upgrade command - use exit code 2
Browse files Browse the repository at this point in the history
If enhanced exit codes are enabled, this sets the upgrade command to
exit with 2 if there is nothing to do (i.e. if no packages are
upgraded). This entails returning the number of successful package
results from the report_action_summary method.
  • Loading branch information
TheCakeIsNaOH authored and gep13 committed Apr 23, 2024
1 parent fbc69dc commit bcc3146
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,11 @@ Exit codes that normally result from running this command.
Normal:
- 0: operation was successful, no issues detected
- -1 or 1: an error has occurred
- 2: nothing to do, no packages outdated (enhanced)
NOTE: Starting in v2.3.0, if you have the feature '{2}'
turned on, then choco will provide enhanced exit codes that allow
better integration and scripting.
Package Exit Codes:
- 1641: success, reboot initiated
Expand All @@ -368,7 +373,7 @@ turned on. Uninstall command has additional valid exit codes.
In addition to the above exit codes, you may also see reboot exit codes
when the feature '{1}' is turned on. It typically requires
the feature '{0}' to also be turned on to work properly.
".FormatWith(ApplicationParameters.Features.UsePackageExitCodes, ApplicationParameters.Features.ExitOnRebootDetected));
".FormatWith(ApplicationParameters.Features.UsePackageExitCodes, ApplicationParameters.Features.ExitOnRebootDetected, ApplicationParameters.Features.UseEnhancedExitCodes));

"chocolatey".Log().Info(ChocolateyLoggers.Important, "See It In Action");
"chocolatey".Log().Info(@"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -812,8 +812,8 @@ public virtual ConcurrentDictionary<string, PackageResult> Install(ChocolateyCon
}
finally
{
var installFailures = ReportActionSummary(packageInstalls, "installed");
if (installFailures != 0 && Environment.ExitCode == 0)
var actionSummaryResult = ReportActionSummary(packageInstalls, "installed");
if (actionSummaryResult.Failures != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}
Expand Down Expand Up @@ -1117,7 +1117,7 @@ public void UpgradeDryRun(ChocolateyConfiguration config)

if (config.RegularOutput)
{
var noopFailures = ReportActionSummary(noopUpgrades, "can upgrade");
var actionSummaryResult = ReportActionSummary(noopUpgrades, "can upgrade");
}

RandomlyNotifyAboutLicensedFeatures(config);
Expand Down Expand Up @@ -1177,12 +1177,17 @@ public virtual ConcurrentDictionary<string, PackageResult> Upgrade(ChocolateyCon
}
finally
{
var upgradeFailures = ReportActionSummary(packageUpgrades, "upgraded");
if (upgradeFailures != 0 && Environment.ExitCode == 0)
var actionSummaryResult = ReportActionSummary(packageUpgrades, "upgraded");
if (actionSummaryResult.Failures != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}

if (config.Features.UseEnhancedExitCodes && (actionSummaryResult.Successes + actionSummaryResult.Failures == 0) && Environment.ExitCode == 0)
{
Environment.ExitCode = 2;
}

RandomlyNotifyAboutLicensedFeatures(config);
}

Expand Down Expand Up @@ -1274,13 +1279,13 @@ public virtual ConcurrentDictionary<string, PackageResult> Uninstall(ChocolateyC
}
finally
{
var uninstallFailures = ReportActionSummary(packageUninstalls, "uninstalled");
if (uninstallFailures != 0 && Environment.ExitCode == 0)
var actionSummaryResult = ReportActionSummary(packageUninstalls, "uninstalled");
if (actionSummaryResult.Failures != 0 && Environment.ExitCode == 0)
{
Environment.ExitCode = 1;
}

if (uninstallFailures != 0)
if (actionSummaryResult.Failures != 0)
{
this.Log().Warn(@"
If a package uninstall is failing and/or you've already uninstalled the
Expand Down Expand Up @@ -1428,7 +1433,7 @@ private void BuildInstallExample(string packageName, StringBuilder sb, string co
}
}

private int ReportActionSummary(ConcurrentDictionary<string, PackageResult> packageResults, string actionName)
private (int Successes, int Failures, int Warnings, int RebootPackages) ReportActionSummary(ConcurrentDictionary<string, PackageResult> packageResults, string actionName)
{
var successes = packageResults.OrEmpty().Where(p => p.Value.Success && !p.Value.Inconclusive).OrderBy(p => p.Value.Name);
var failures = packageResults.Count(p => !p.Value.Success);
Expand Down Expand Up @@ -1496,7 +1501,7 @@ The recent package changes indicate a reboot is necessary.
}
}

return failures;
return (successes.Count(), failures, warnings, rebootPackages);
}

public virtual void HandlePackageUninstall(PackageResult packageResult, ChocolateyConfiguration config)
Expand Down

0 comments on commit bcc3146

Please sign in to comment.