Skip to content

Commit

Permalink
(GH-1758) control enhanced exit codes w/feature
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ferventcoder committed Mar 14, 2019
1 parent a7868f5 commit 606e855
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/chocolatey/infrastructure.app/ApplicationParameters.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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+.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 606e855

Please sign in to comment.