Browse Source

Proper exit code. Issue #52

1.0
Andrés Montañez 10 years ago
parent
commit
5d728a9105
  1. 1
      Mage/Command/AbstractCommand.php
  2. 4
      Mage/Command/BuiltIn/CompileCommand.php
  3. 12
      Mage/Command/BuiltIn/DeployCommand.php
  4. 5
      Mage/Command/BuiltIn/InitCommand.php
  5. 4
      Mage/Command/BuiltIn/InstallCommand.php
  6. 9
      Mage/Command/BuiltIn/ListCommand.php
  7. 2
      Mage/Command/BuiltIn/LockCommand.php
  8. 19
      Mage/Command/BuiltIn/ReleasesCommand.php
  9. 15
      Mage/Command/BuiltIn/RollbackCommand.php
  10. 2
      Mage/Command/BuiltIn/UnlockCommand.php
  11. 6
      Mage/Command/BuiltIn/UpdateCommand.php
  12. 6
      Mage/Command/BuiltIn/UpgradeCommand.php
  13. 2
      Mage/Command/BuiltIn/VersionCommand.php
  14. 3
      Mage/Console.php

1
Mage/Command/AbstractCommand.php

@ -28,6 +28,7 @@ abstract class AbstractCommand
/**
* Runs the Command
* @return integer exit code
* @throws \Exception
*/
public abstract function run();

4
Mage/Command/BuiltIn/CompileCommand.php

@ -28,12 +28,14 @@ class CompileCommand extends AbstractCommand
{
if (ini_get('phar.readonly')) {
Console::output('The <purple>php.ini</purple> variable <light_red>phar.readonly</light_red> must be <yellow>Off</yellow>.', 1, 2);
return;
return 300;
}
$compiler = new Compiler;
$compiler->compile();
Console::output('<light_purple>mage.phar</light_purple> compiled <light_green>successfully</light_green>', 0, 2);
return 0;
}
}

12
Mage/Command/BuiltIn/DeployCommand.php

@ -108,18 +108,20 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
*/
public function run()
{
$exitCode = 1000;
// Check if Environment is not Locked
$lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile)) {
Console::output('<red>This environment is locked!</red>', 1, 2);
echo file_get_contents($lockFile);
return;
return 1010;
}
// Check for running instance and Lock
if (file_exists(getcwd() . '/.mage/~working.lock')) {
Console::output('<red>There is already an instance of Magallanes running!</red>', 1, 2);
return;
return 1020;
} else {
touch(getcwd() . '/.mage/~working.lock');
}
@ -193,6 +195,12 @@ class DeployCommand extends AbstractCommand implements RequiresEnvironment
if (file_exists(getcwd() . '/.mage/~working.lock')) {
unlink(getcwd() . '/.mage/~working.lock');
}
if (self::$failedTasks === 0) {
$exitCode = 0;
}
return $exitCode;
}
/**

5
Mage/Command/BuiltIn/InitCommand.php

@ -27,6 +27,7 @@ class InitCommand extends AbstractCommand
*/
public function run()
{
$exitCode = 50;
$configDir = getcwd() . '/.mage';
Console::output('Initiating managing process for application with <dark_gray>Magallanes</dark_gray>');
@ -49,9 +50,13 @@ class InitCommand extends AbstractCommand
if (!in_array(false, $results)) {
Console::output('<light_green>Success!!</light_green> The configuration for <dark_gray>Magallanes</dark_gray> has been generated at <blue>.mage</blue> directory.');
Console::output('<dark_gray>Please!! Review and adjust the configuration.</dark_gray>', 2, 2);
$exitCode = 0;
} else {
Console::output('<light_red>Error!!</light_red> Unable to generate the configuration.', 1, 2);
}
return $exitCode;
}
}

4
Mage/Command/BuiltIn/InstallCommand.php

@ -26,6 +26,7 @@ class InstallCommand extends AbstractCommand
*/
public function run()
{
$exitCode = 88;
Console::output('Installing <dark_gray>Magallanes</dark_gray>... ', 1, 0);
// Vars
@ -74,7 +75,10 @@ class InstallCommand extends AbstractCommand
}
Console::output('<light_green>Success!</light_green>', 0, 2);
$exitCode = 0;
}
return $exitCode;
}
/**

9
Mage/Command/BuiltIn/ListCommand.php

@ -31,12 +31,13 @@ class ListCommand extends AbstractCommand
*/
public function run()
{
$exitCode = 600;
$subCommand = $this->getConfig()->getArgument(1);
try {
switch ($subCommand) {
case 'environments':
$this->listEnvironments();
$exitCode = $this->listEnvironments();
break;
default;
@ -46,6 +47,8 @@ class ListCommand extends AbstractCommand
} catch (Exception $e) {
Console::output('<red>' . $e->getMessage() . '</red>', 1, 2);
}
return $exitCode;
}
/**
@ -53,6 +56,7 @@ class ListCommand extends AbstractCommand
*/
protected function listEnvironments()
{
$exitCode = 600;
$environments = array();
$content = scandir(getcwd() . '/.mage/config/environment/');
foreach ($content as $file) {
@ -68,9 +72,12 @@ class ListCommand extends AbstractCommand
Console::output('* <light_red>' . $environment . '</light_red>', 2, 1);
}
Console::output('', 1, 1);
$exitCode = 0;
} else {
Console::output('<dark_gray>You don\'t have any environment configured.</dark_gray>', 1, 2);
}
return $exitCode;
}
}

2
Mage/Command/BuiltIn/LockCommand.php

@ -43,6 +43,8 @@ class LockCommand extends AbstractCommand implements RequiresEnvironment
file_put_contents($lockFile, 'Locked environment at date: ' . date('Y-m-d H:i:s') . $lockmsg);
Console::output('Locked deployment to <light_purple>' . $this->getConfig()->getEnvironment() . '</light_purple> environment', 1, 2);
return 0;
}
}

19
Mage/Command/BuiltIn/ReleasesCommand.php

@ -28,7 +28,7 @@ class ReleasesCommand extends AbstractCommand implements RequiresEnvironment
*/
public function run()
{
$result = false;
$exitCode = 400;
$subCommand = $this->getConfig()->getArgument(1);
// Run Tasks for Deployment
@ -40,9 +40,10 @@ class ReleasesCommand extends AbstractCommand implements RequiresEnvironment
1, 3
);
return false;
return 401;
}
$result = true;
foreach ($hosts as $host) {
$this->getConfig()->setHost($host);
@ -50,14 +51,14 @@ class ReleasesCommand extends AbstractCommand implements RequiresEnvironment
case 'list':
$task = Factory::get('releases/list', $this->getConfig());
$task->init();
$result = $task->run();
$result = $task->run() && $result;
break;
case 'rollback':
if (!is_numeric($this->getConfig()->getParameter('release', ''))) {
Console::output('<red>Missing required releaseid.</red>', 1, 2);
return false;
return 410;
}
$lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock';
@ -65,18 +66,22 @@ class ReleasesCommand extends AbstractCommand implements RequiresEnvironment
Console::output('<red>This environment is locked!</red>', 1, 2);
echo file_get_contents($lockFile);
return false;
return 420;
}
$releaseId = $this->getConfig()->getParameter('release', '');
$this->getConfig()->setReleaseId($releaseId);
$task = Factory::get('releases/rollback', $this->getConfig());
$task->init();
$result = $task->run();
$result = $task->run() && $result;
break;
}
}
return $result;
if ($result) {
$exitCode = 0;
}
return $exitCode;
}
}

15
Mage/Command/BuiltIn/RollbackCommand.php

@ -28,19 +28,19 @@ class RollbackCommand extends AbstractCommand implements RequiresEnvironment
*/
public function run()
{
$result = false;
$exitCode = 450;
$releaseId = $this->getConfig()->getArgument(1);
if (!is_numeric($releaseId)) {
Console::output('<red>This release is mandatory.</red>', 1, 2);
return false;
return 451;
}
$lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock';
if (file_exists($lockFile)) {
Console::output('<red>This environment is locked!</red>', 1, 2);
echo file_get_contents($lockFile);
return null;
return 20;
}
// Run Tasks for Deployment
@ -50,16 +50,21 @@ class RollbackCommand extends AbstractCommand implements RequiresEnvironment
Console::output('<light_purple>Warning!</light_purple> <dark_gray>No hosts defined, unable to get releases.</dark_gray>', 1, 3);
} else {
$result = true;
foreach ($hosts as $host) {
$this->getConfig()->setHost($host);
$this->getConfig()->setReleaseId($releaseId);
$task = Factory::get('releases/rollback', $this->getConfig());
$task->init();
$result = $task->run();
$result = $task->run() && $result;
}
if ($result) {
$exitCode = 0;
}
}
return $result;
return $exitCode;
}
}

2
Mage/Command/BuiltIn/UnlockCommand.php

@ -34,6 +34,8 @@ class UnlockCommand
}
Console::output('Unlocked deployment to <light_purple>' . $this->getConfig()->getEnvironment() . '</light_purple> environment', 1, 2);
return 0;
}
}

6
Mage/Command/BuiltIn/UpdateCommand.php

@ -27,6 +27,8 @@ class UpdateCommand extends AbstractCommand
*/
public function run()
{
$exitCode = 200;
$task = Factory::get('scm/update', $this->getConfig());
$task->init();
@ -35,9 +37,13 @@ class UpdateCommand extends AbstractCommand
if ($result == true) {
Console::output('<green>OK</green>' . PHP_EOL, 0);
$exitCode = 0;
} else {
Console::output('<red>FAIL</red>' . PHP_EOL, 0);
}
return $exitCode;
}
}

6
Mage/Command/BuiltIn/UpgradeCommand.php

@ -38,6 +38,7 @@ class UpgradeCommand extends AbstractCommand
*/
public function run()
{
$exitCode = 100;
Console::output('Upgrading <dark_gray>Magallanes</dark_gray> ... ', 1, 0);
$user = '';
@ -59,10 +60,12 @@ class UpgradeCommand extends AbstractCommand
if ($versionCompare == 0) {
Console::output('<yellow>SKIP</yellow>', 0, 1);
Console::output('Your current version is up to date.', 2);
$exitCode = 0;
} else if ($versionCompare == 1) {
Console::output('<yellow>SKIP</yellow>', 0, 1);
Console::output('Your current version is newer.', 2);
$exitCode = 0;
} else if ($versionCompare == -1) {
// Download Package
@ -82,6 +85,7 @@ class UpgradeCommand extends AbstractCommand
Console::executeCommand('mv ' . dirname($tarballFile) . '/magallanes ' . MAGALLANES_DIRECTORY);
Console::output('<green>OK</green>', 0, 1);
$exitCode = 0;
}
} else {
@ -93,5 +97,7 @@ class UpgradeCommand extends AbstractCommand
Console::output('Invalid version.', 2);
}
}
return $exitCode;
}
}

2
Mage/Command/BuiltIn/VersionCommand.php

@ -27,6 +27,8 @@ class VersionCommand extends AbstractCommand
public function run()
{
Console::output('Running <blue>Magallanes</blue> version <dark_gray>' . MAGALLANES_VERSION . '</dark_gray>', 0, 2);
return 0;
}
}

3
Mage/Console.php

@ -135,8 +135,7 @@ class Console
throw new Exception('You must specify an environment for this command.');
}
}
$command->run();
$exitCode = 0;
$exitCode = $command->run();
} catch (Exception $exception) {
self::output('<red>' . $exception->getMessage() . '</red>', 1, 2);

Loading…
Cancel
Save