From 91c40c9cd68de2deb7cb84f441cca9924439a5e8 Mon Sep 17 00:00:00 2001 From: TheCakeIsNaOH Date: Wed, 12 Jan 2022 22:35:29 -0600 Subject: [PATCH] (#1764) source exit 2 if nothing to do If enhanced exit codes are enabled, this sets the source command to exit with 2 if there is nothing to do (i.e. if NOCHANGEMESSAGE is output). --- .../commands/ChocolateySourceCommand.cs | 7 ++++++- .../ChocolateyConfigSettingsService.cs | 20 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs index bf78c4ca26..8180fc70c3 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateySourceCommand.cs @@ -160,12 +160,17 @@ 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 (enhanced) + +NOTE: Starting in v0.10.12, if you have the feature '{0}' + turned on, then choco will provide enhanced exit codes that allow + better integration and scripting. If you find other exit codes that we have not yet documented, please file a ticket so we can document it at https://github.com/chocolatey/choco/issues/new/choose. -"); +".format_with(ApplicationParameters.Features.UseEnhancedExitCodes)); "chocolatey".Log().Info(ChocolateyLoggers.Important, "Options and Switches"); } diff --git a/src/chocolatey/infrastructure.app/services/ChocolateyConfigSettingsService.cs b/src/chocolatey/infrastructure.app/services/ChocolateyConfigSettingsService.cs index 9135db0e8b..b733fbb496 100644 --- a/src/chocolatey/infrastructure.app/services/ChocolateyConfigSettingsService.cs +++ b/src/chocolatey/infrastructure.app/services/ChocolateyConfigSettingsService.cs @@ -151,6 +151,11 @@ public void AddSource(ChocolateyConfiguration configuration) ) { if (!configuration.QuietOutput) this.Log().Warn(NoChangeMessage); + + if (configuration.Features.UseEnhancedExitCodes && Environment.ExitCode == 0) + { + Environment.ExitCode = 2; + } } else { @@ -183,6 +188,11 @@ public void RemoveSource(ChocolateyConfiguration configuration) else { if (!configuration.QuietOutput) this.Log().Warn(NoChangeMessage); + + if (configuration.Features.UseEnhancedExitCodes && Environment.ExitCode == 0) + { + Environment.ExitCode = 2; + } } } @@ -198,6 +208,11 @@ public void DisableSource(ChocolateyConfiguration configuration) else { if (!configuration.QuietOutput) this.Log().Warn(NoChangeMessage); + + if (configuration.Features.UseEnhancedExitCodes && Environment.ExitCode == 0) + { + Environment.ExitCode = 2; + } } } @@ -213,6 +228,11 @@ public void EnableSource(ChocolateyConfiguration configuration) else { if (!configuration.QuietOutput) this.Log().Warn(NoChangeMessage); + + if (configuration.Features.UseEnhancedExitCodes && Environment.ExitCode == 0) + { + Environment.ExitCode = 2; + } } }