From 606e8550e873b1a962e090948ad21c03c44b6b2e Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Thu, 14 Mar 2019 15:29:20 -0500 Subject: [PATCH] (GH-1758) control enhanced exit codes w/feature In #1602 and #1724, enhanced exit codes were added to provide more intentional exit codes that determine the state of what happened during the run. This would allow simply seeing a 0 from outdated and knowing that all packages are up to date, or seeing a 2 and knowing that packages need updated. This allows for better scripting based on exit codes. However, there are some existing integrations that might be broken on taking on a newer version of Chocolatey if the enhanced exit codes are not being looked for yet. To allow compatibility to older systems, add a feature switch that can be disabled to provide the older behavior of 0 or 1 exit codes. --- src/chocolatey/infrastructure.app/ApplicationParameters.cs | 1 + .../infrastructure.app/builders/ConfigurationBuilder.cs | 3 ++- .../infrastructure.app/commands/ChocolateyListCommand.cs | 2 +- .../configuration/ChocolateyConfiguration.cs | 3 ++- .../infrastructure.app/services/ChocolateyPackageService.cs | 3 ++- 5 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/chocolatey/infrastructure.app/ApplicationParameters.cs b/src/chocolatey/infrastructure.app/ApplicationParameters.cs index 87f51a308b..75cf0f7dcc 100644 --- a/src/chocolatey/infrastructure.app/ApplicationParameters.cs +++ b/src/chocolatey/infrastructure.app/ApplicationParameters.cs @@ -167,6 +167,7 @@ public static class Features public static readonly string FailOnInvalidOrMissingLicense = "failOnInvalidOrMissingLicense"; public static readonly string IgnoreInvalidOptionsSwitches = "ignoreInvalidOptionsSwitches"; public static readonly string UsePackageExitCodes = "usePackageExitCodes"; + public static readonly string UseEnhancedExitCodes = "useEnhancedExitCodes"; public static readonly string UseFipsCompliantChecksums = "useFipsCompliantChecksums"; public static readonly string ScriptsCheckLastExitCode = "scriptsCheckLastExitCode"; public static readonly string ShowNonElevatedWarnings = "showNonElevatedWarnings"; diff --git a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs index daff600795..84c373133c 100644 --- a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs +++ b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs @@ -291,7 +291,8 @@ private static void set_feature_flags(ChocolateyConfiguration config, ConfigFile config.Features.VirusCheck = set_feature_flag(ApplicationParameters.Features.VirusCheck, configFileSettings, defaultEnabled: false, description: "Virus Check - perform virus checking on downloaded files. Available in 0.9.10+. Licensed versions only."); config.Features.FailOnInvalidOrMissingLicense = set_feature_flag(ApplicationParameters.Features.FailOnInvalidOrMissingLicense, configFileSettings, defaultEnabled: false, description: "Fail On Invalid Or Missing License - allows knowing when a license is expired or not applied to a machine. Available in 0.9.10+."); config.Features.IgnoreInvalidOptionsSwitches = set_feature_flag(ApplicationParameters.Features.IgnoreInvalidOptionsSwitches, configFileSettings, defaultEnabled: true, description: "Ignore Invalid Options/Switches - If a switch or option is passed that is not recognized, should choco fail? Available in 0.9.10+."); - config.Features.UsePackageExitCodes = set_feature_flag(ApplicationParameters.Features.UsePackageExitCodes, configFileSettings, defaultEnabled: true, description: "Use Package Exit Codes - Package scripts can provide exit codes. With this on, package exit codes will be what choco uses for exit when non-zero (this value can come from a dependency package). Chocolatey defines valid exit codes as 0, 1605, 1614, 1641, 3010. With this feature off, choco will exit with a 0 or a 1 (matching previous behavior). Available in 0.9.10+."); + config.Features.UsePackageExitCodes = set_feature_flag(ApplicationParameters.Features.UsePackageExitCodes, configFileSettings, defaultEnabled: true, description: "Use Package Exit Codes - Package scripts can provide exit codes. With this on, package exit codes will be what choco uses for exit when non-zero (this value can come from a dependency package). Chocolatey defines valid exit codes as 0, 1605, 1614, 1641, 3010. With this feature off, choco will exit with 0, 1, or -1 (matching previous behavior). Available in 0.9.10+."); + config.Features.UseEnhancedExitCodes = set_feature_flag(ApplicationParameters.Features.UseEnhancedExitCodes, configFileSettings, defaultEnabled: true, description: "Use Enhanced Exit Codes - Chocolatey is able to provide enhanced exit codes surrounding list, search, info, outdated and other commands that don't deal directly with package operations. To see enhanced exit codes and their meanings, please run `choco [cmdname] -?`. With this feature off, choco will exit with 0, 1, or -1 (matching previous behavior). Available in 0.10.12+."); config.Features.UseFipsCompliantChecksums = set_feature_flag(ApplicationParameters.Features.UseFipsCompliantChecksums, configFileSettings, defaultEnabled: false, description: "Use FIPS Compliant Checksums - Ensure checksumming done by choco uses FIPS compliant algorithms. Not recommended unless required by FIPS Mode. Enabling on an existing installation could have unintended consequences related to upgrades/uninstalls. Available in 0.9.10+."); config.Features.ShowNonElevatedWarnings = set_feature_flag(ApplicationParameters.Features.ShowNonElevatedWarnings, configFileSettings, defaultEnabled: true, description: "Show Non-Elevated Warnings - Display non-elevated warnings. Available in 0.10.4+."); config.Features.ShowDownloadProgress = set_feature_flag(ApplicationParameters.Features.ShowDownloadProgress, configFileSettings, defaultEnabled: true, description: "Show Download Progress - Show download progress percentages in the CLI. Available in 0.10.4+."); diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs index e2fd6e2e83..46d49d8aab 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyListCommand.cs @@ -205,7 +205,7 @@ public virtual void run(ChocolateyConfiguration configuration) var packageResults = _packageService.list_run(configuration).ToList(); // if there are no results, exit with a 1. - if (packageResults.Count == 0) + if (configuration.Features.UseEnhancedExitCodes && packageResults.Count == 0 && Environment.ExitCode == 0) { Environment.ExitCode = 1; } diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index aaf876ca2e..263cf07f74 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -382,7 +382,8 @@ public sealed class FeaturesConfiguration public bool VirusCheck { get; set; } public bool FailOnInvalidOrMissingLicense { get; set; } public bool IgnoreInvalidOptionsSwitches { get; set; } - public bool UsePackageExitCodes { get; set; } + public bool UsePackageExitCodes { get; set; } + public bool UseEnhancedExitCodes { get; set; } public bool UseFipsCompliantChecksums { get; set; } public bool ShowNonElevatedWarnings { get; set; } public bool ShowDownloadProgress { get; set; } diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs index 3ba579f767..0b4295d93d 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyPackageService.cs @@ -656,7 +656,8 @@ Output is package name | current version | available version | pinned? } } - if (outdatedPackages.Count != 0 && Environment.ExitCode == 0) + // oudated packages, return 2 + if (config.Features.UseEnhancedExitCodes && outdatedPackages.Count != 0 && Environment.ExitCode == 0) { Environment.ExitCode = 2; }