From 079b914c01bceb619ba304dca5c9043de4ffaa8b Mon Sep 17 00:00:00 2001 From: Rob Reynolds Date: Mon, 25 Apr 2016 08:13:35 -0500 Subject: [PATCH] (GH-512) Allow configuring package exit codes Package exit codes provide the ability for the exit code of an underlying installer to be passed through to Chocolatey's exit code. This value is the last non-zero exit code from a package or dependency that Chocolatey is currently installing. This change, while very enabling, may be surprising for some folks and they may wish to shut this feature off. As this feature may need to be shut off, provide it as both a feature and switches to turn it on/off per install, upgrade, and uninstall. Add the switches to tab expansion as well. --- .../helpers/ChocolateyTabExpansion.ps1 | 6 +++--- .../infrastructure.app/ApplicationParameters.cs | 1 + .../builders/ConfigurationBuilder.cs | 1 + .../commands/ChocolateyInstallCommand.cs | 14 ++++++++++++++ .../commands/ChocolateyUninstallCommand.cs | 13 +++++++++++++ .../commands/ChocolateyUpgradeCommand.cs | 13 +++++++++++++ .../configuration/ChocolateyConfiguration.cs | 1 + 7 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1 b/src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1 index bb4a65945b..b785555227 100644 --- a/src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1 +++ b/src/chocolatey.resources/helpers/ChocolateyTabExpansion.ps1 @@ -42,11 +42,11 @@ $commandOptions = @{ list = "--lo --pre --exact --by-id-only --id-starts-with --detailed --approved-only --not-broken --source='' --user= --password= --local-only --prerelease --include-programs --page= --page-size= --order-by-popularity --download-cache-only" + $allcommands search = "--pre --exact --by-id-only --id-starts-with --detailed --approved-only --not-broken --source='' --user= --password= --local-only --prerelease --include-programs --page= --page-size= --order-by-popularity --download-cache-only" + $allcommands info = "--pre --lo --source='' --user= --password= --local-only --prerelease" + $allcommands - install = "-y -whatif -? --pre --version= --params='' --install-arguments='' --override-arguments --ignore-dependencies --source='' --source='windowsfeatures' --source='webpi' --user= --password= --prerelease --forcex86 --not-silent --package-parameters='' --allow-downgrade --force-dependencies --skip-automation-scripts --allow-multiple-versions --ignore-checksums" + $allcommands + $proInstallUpgradeOptions + install = "-y -whatif -? --pre --version= --params='' --install-arguments='' --override-arguments --ignore-dependencies --source='' --source='windowsfeatures' --source='webpi' --user= --password= --prerelease --forcex86 --not-silent --package-parameters='' --allow-downgrade --force-dependencies --use-package-exit-codes --ignore-package-exit-codes --skip-automation-scripts --allow-multiple-versions --ignore-checksums" + $allcommands + $proInstallUpgradeOptions pin = "--name= --version= -?" + $allcommands outdated = "-? --source='' --user= --password=" + $allcommands - upgrade = "-y -whatif -? --pre --version= --except='' --params='' --install-arguments='' --override-arguments --ignore-dependencies --source='' --source='windowsfeatures' --source='webpi' --user= --password= --prerelease --forcex86 --not-silent --package-parameters='' --allow-downgrade --allow-multiple-versions --skip-automation-scripts --fail-on-unfound --fail-on-not-installed --ignore-checksums" + $allcommands + $proInstallUpgradeOptions - uninstall = "-y -whatif -? --force-dependencies --remove-dependencies --all-versions --source='windowsfeatures' --source='webpi' --version= --uninstall-arguments='' --override-arguments --not-silent --params='' --package-parameters='' --skip-automation-scripts" + $allcommands + upgrade = "-y -whatif -? --pre --version= --except='' --params='' --install-arguments='' --override-arguments --ignore-dependencies --source='' --source='windowsfeatures' --source='webpi' --user= --password= --prerelease --forcex86 --not-silent --package-parameters='' --allow-downgrade --allow-multiple-versions --use-package-exit-codes --ignore-package-exit-codes --skip-automation-scripts --fail-on-unfound --fail-on-not-installed --ignore-checksums" + $allcommands + $proInstallUpgradeOptions + uninstall = "-y -whatif -? --force-dependencies --remove-dependencies --all-versions --source='windowsfeatures' --source='webpi' --version= --uninstall-arguments='' --override-arguments --not-silent --params='' --package-parameters='' --use-package-exit-codes --ignore-package-exit-codes --skip-automation-scripts" + $allcommands new = "--template-name= --automaticpackage --version= --maintainer='' packageversion= maintainername='' maintainerrepo='' installertype= url='' url64='' silentargs='' -?" + $allcommands pack = "--version= -?" + $allcommands push = "--source='' --api-key= --timeout= -?" + $allcommands diff --git a/src/chocolatey/infrastructure.app/ApplicationParameters.cs b/src/chocolatey/infrastructure.app/ApplicationParameters.cs index 28c68cb8b3..546d43907d 100644 --- a/src/chocolatey/infrastructure.app/ApplicationParameters.cs +++ b/src/chocolatey/infrastructure.app/ApplicationParameters.cs @@ -114,6 +114,7 @@ public static class Features public static readonly string VirusCheck = "virusCheck"; public static readonly string FailOnInvalidOrMissingLicense = "failOnInvalidOrMissingLicense"; public static readonly string IgnoreInvalidOptionsSwitches = "ignoreInvalidOptionsSwitches"; + public static readonly string UsePackageExitCodes = "usePackageExitCodes"; } public static class Messages diff --git a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs index 097865e38f..32a0c8147a 100644 --- a/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs +++ b/src/chocolatey/infrastructure.app/builders/ConfigurationBuilder.cs @@ -246,6 +246,7 @@ private static void set_feature_flags(ChocolateyConfiguration config, ConfigFile config.Features.VirusCheck = set_feature_flag(ApplicationParameters.Features.VirusCheck, configFileSettings, defaultEnabled: false, description: "Virus Check [licensed versions only] - perform virus checking on downloaded files."); 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."); 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?"); + 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.PromptForConfirmation = !set_feature_flag(ApplicationParameters.Features.AllowGlobalConfirmation, configFileSettings, defaultEnabled: false, description: "Prompt for confirmation in scripts or bypass."); } diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs index ef82b92050..96a5b5b5c7 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyInstallCommand.cs @@ -21,6 +21,7 @@ namespace chocolatey.infrastructure.app.commands using commandline; using configuration; using infrastructure.commands; + using infrastructure.configuration; using logging; using services; @@ -88,6 +89,19 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon { if (option != null) configuration.Features.CheckSumFiles = false; }) + .Add("ignorepackagecodes|ignorepackageexitcodes|ignore-package-codes|ignore-package-exit-codes", + "IgnorePackageExitCodes - Exit with a 0 for success and 1 for non-success, no matter what package scripts provide for exit codes. Overrides the default feature '{0}' set to '{1}'. Available in 0.9.10+.".format_with(ApplicationParameters.Features.UsePackageExitCodes, configuration.Features.UsePackageExitCodes.to_string()), + option => + { + if (option != null) + { + configuration.Features.UsePackageExitCodes = false; + } + }) + .Add("usepackagecodes|usepackageexitcodes|use-package-codes|use-package-exit-codes", + "UsePackageExitCodes - Package scripts can provide exit codes. Use those for choco's exit code when non-zero (this value can come from a dependency package). Chocolatey defines valid exit codes as 0, 1605, 1614, 1641, 3010. Overrides the default feature '{0}' set to '{1}'. Available in 0.9.10+.".format_with(ApplicationParameters.Features.UsePackageExitCodes, configuration.Features.UsePackageExitCodes.to_string()), + option => configuration.Features.UsePackageExitCodes = option != null + ) ; //todo: Checksum / ChecksumType defaults to md5 / package name can be a url / installertype diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs index b6012fc31e..030fe39291 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyUninstallCommand.cs @@ -64,6 +64,19 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon .Add("n|skippowershell|skip-powershell|skipscripts|skip-scripts|skip-automation-scripts", "Skip Powershell - Do not run chocolateyUninstall.ps1. Defaults to false.", option => configuration.SkipPackageInstallProvider = option != null) + .Add("ignorepackagecodes|ignorepackageexitcodes|ignore-package-codes|ignore-package-exit-codes", + "IgnorePackageExitCodes - Exit with a 0 for success and 1 for non-success, no matter what package scripts provide for exit codes. Overrides the default feature '{0}' set to '{1}'. Available in 0.9.10+.".format_with(ApplicationParameters.Features.UsePackageExitCodes, configuration.Features.UsePackageExitCodes.to_string()), + option => + { + if (option != null) + { + configuration.Features.UsePackageExitCodes = false; + } + }) + .Add("usepackagecodes|usepackageexitcodes|use-package-codes|use-package-exit-codes", + "UsePackageExitCodes - Package scripts can provide exit codes. Use those for choco's exit code when non-zero (this value can come from a dependency package). Chocolatey defines valid exit codes as 0, 1605, 1614, 1641, 3010. Overrides the default feature '{0}' set to '{1}'. Available in 0.9.10+.".format_with(ApplicationParameters.Features.UsePackageExitCodes, configuration.Features.UsePackageExitCodes.to_string()), + option => configuration.Features.UsePackageExitCodes = option != null + ) ; } diff --git a/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs b/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs index 389f2f802a..e08651f867 100644 --- a/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs +++ b/src/chocolatey/infrastructure.app/commands/ChocolateyUpgradeCommand.cs @@ -93,6 +93,19 @@ public virtual void configure_argument_parser(OptionSet optionSet, ChocolateyCon { if (option != null) configuration.Features.CheckSumFiles = false; }) + .Add("ignorepackagecodes|ignorepackageexitcodes|ignore-package-codes|ignore-package-exit-codes", + "IgnorePackageExitCodes - Exit with a 0 for success and 1 for non-success, no matter what package scripts provide for exit codes. Overrides the default feature '{0}' set to '{1}'. Available in 0.9.10+.".format_with(ApplicationParameters.Features.UsePackageExitCodes, configuration.Features.UsePackageExitCodes.to_string()), + option => + { + if (option != null) + { + configuration.Features.UsePackageExitCodes = false; + } + }) + .Add("usepackagecodes|usepackageexitcodes|use-package-codes|use-package-exit-codes", + "UsePackageExitCodes - Package scripts can provide exit codes. Use those for choco's exit code when non-zero (this value can come from a dependency package). Chocolatey defines valid exit codes as 0, 1605, 1614, 1641, 3010. Overrides the default feature '{0}' set to '{1}'. Available in 0.9.10+.".format_with(ApplicationParameters.Features.UsePackageExitCodes, configuration.Features.UsePackageExitCodes.to_string()), + option => configuration.Features.UsePackageExitCodes = option != null + ) .Add("except=", "Except - a comma-separated list of package names that should not be upgraded when upgrading 'all'. Defaults to empty.", option => configuration.UpgradeCommand.PackageNamesToSkip = option.remove_surrounding_quotes()) diff --git a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs index de802a5b02..509cecb245 100644 --- a/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs +++ b/src/chocolatey/infrastructure.app/configuration/ChocolateyConfiguration.cs @@ -342,6 +342,7 @@ public sealed class FeaturesConfiguration public bool VirusCheck { get; set; } public bool FailOnInvalidOrMissingLicense { get; set; } public bool IgnoreInvalidOptionsSwitches { get; set; } + public bool UsePackageExitCodes { get; set; } } //todo: retrofit other command configs this way