Skip to content

Commit

Permalink
Merge pull request #1083 from staabm/req-task-ctx
Browse files Browse the repository at this point in the history
Provide a usefull error when ask*() is not used wihtin a task()
  • Loading branch information
antonmedv authored Mar 11, 2017
2 parents 9077bde + 4b9eb3b commit fb745e8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions src/Task/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
*/
Expand Down
10 changes: 9 additions & 1 deletion src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
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;
use Symfony\Component\Console\Question\Question;
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;
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -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');
}
Expand Down Expand Up @@ -624,6 +628,8 @@ function askChoice($message, array $availableChoices, $default = null, $multisel
*/
function askConfirmation($message, $default = false)
{
Context::required(__FUNCTION__);

if (isQuiet()) {
return $default;
}
Expand All @@ -645,6 +651,8 @@ function askConfirmation($message, $default = false)
*/
function askHiddenResponse($message)
{
Context::required(__FUNCTION__);

if (isQuiet()) {
return '';
}
Expand Down

0 comments on commit fb745e8

Please sign in to comment.