From f7dab8145f461d66f2dcdd4bacbdb9a1b3ba2874 Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Fri, 14 Jan 2022 21:53:28 -0600 Subject: [PATCH] (#2200) upgrade exit 2 if nothing to do 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. --- .../commands/ChocolateyUpgradeCommand.cs | 7 +++++- .../services/ChocolateyPackageService.cs | 25 +++++++++++-------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs index 2b5d47b94a..c7b6e94b56 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs @@ -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 @@ -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(@" diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index 1e0f1d2bfb..c3a3747d1f 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -812,8 +812,8 @@ public virtual ConcurrentDictionary 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; } @@ -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); @@ -1177,12 +1177,17 @@ public virtual ConcurrentDictionary 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); } @@ -1274,13 +1279,13 @@ public virtual ConcurrentDictionary 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 @@ -1428,7 +1433,7 @@ private void BuildInstallExample(string packageName, StringBuilder sb, string co } } - private int ReportActionSummary(ConcurrentDictionary packageResults, string actionName) + private (int Successes, int Failures, int Warnings, int RebootPackages) ReportActionSummary(ConcurrentDictionary 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); @@ -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)