diff --git a/CHANGELOG.md b/CHANGELOG.md index 89ec27410..ebdd33754 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Add a way to retrieve a defined task [#1008](https://github.com/deployphp/deployer/pull/1008) - Add support for configFile in the NativeSsh implementation [#979](https://github.com/deployphp/deployer/pull/979) - Add `--no-hooks` option for running commands without `before()` and `after()` [#1061](https://github.com/deployphp/deployer/pull/1061) +- Added a usefull error when ask*() is not used wihtin a task() [#1083](https://github.com/deployphp/deployer/pull/1083) ### Changed - Parse hyphens in environment setting names [#1073](https://github.com/deployphp/deployer/pull/1074) diff --git a/src/Task/Context.php b/src/Task/Context.php index d37efad64..ce5a7897c 100644 --- a/src/Task/Context.php +++ b/src/Task/Context.php @@ -9,6 +9,7 @@ use Deployer\Server\Environment; use Deployer\Server\ServerInterface; +use Deployer\Exception\Exception; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; @@ -69,6 +70,23 @@ public static function get() return end(self::$contexts); } + /** + * Returns the current context when available. + * + * Throws a Exception when not called within a task-context and therefore no Context is available. + * + * This method provides a usefull error to the end-user to make him/her aware + * to use a function in the required task-context. + * + * @throws Exception + */ + public static function required($callerName) + { + if (!self::get()) { + throw new Exception("'$callerName' can only be used within a 'task()'-function!"); + } + } + /** * @return Context */ diff --git a/src/functions.php b/src/functions.php index 91672706b..03d81805a 100644 --- a/src/functions.php +++ b/src/functions.php @@ -16,6 +16,7 @@ use Deployer\Task\Context; use Deployer\Task\GroupTask; use Deployer\Type\Result; +use Deployer\Cluster\ClusterFactory; use Monolog\Logger; use Symfony\Component\Console\Question\ChoiceQuestion; use Symfony\Component\Console\Question\ConfirmationQuestion; @@ -23,7 +24,6 @@ use Symfony\Component\Finder\Finder; use Symfony\Component\Process\Exception\ProcessFailedException; use Symfony\Component\Process\Process; -use Deployer\Cluster\ClusterFactory; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; @@ -560,6 +560,8 @@ function has($name) */ function ask($message, $default = null, $suggestedChoices = null) { + Context::required(__FUNCTION__); + if (($suggestedChoices !== null) && (empty($suggestedChoices))) { throw new \InvalidArgumentException('Suggested choices should not be empty'); } @@ -591,6 +593,8 @@ function ask($message, $default = null, $suggestedChoices = null) */ function askChoice($message, array $availableChoices, $default = null, $multiselect = false) { + Context::required(__FUNCTION__); + if (empty($availableChoices)) { throw new \InvalidArgumentException('Available choices should not be empty'); } @@ -624,6 +628,8 @@ function askChoice($message, array $availableChoices, $default = null, $multisel */ function askConfirmation($message, $default = false) { + Context::required(__FUNCTION__); + if (isQuiet()) { return $default; } @@ -645,6 +651,8 @@ function askConfirmation($message, $default = false) */ function askHiddenResponse($message) { + Context::required(__FUNCTION__); + if (isQuiet()) { return ''; }