Permalink
Browse files

adjusted all framework commands to use proper exit codes

use console exit codes introduced in pull request #647
  • Loading branch information...
1 parent a6d8222 commit 4fd934d0bd74fb06223906b2a45f2b446092d933 @cebe cebe committed Apr 27, 2012
View
87 framework/cli/commands/MigrateCommand.php
@@ -15,6 +15,13 @@
* the yii-dbmigrations extension ((https://github.com/pieterclaerhout/yii-dbmigrations),
* authored by Pieter Claerhout.
*
+ * Since version 1.1.11 this command will exit with the following exit status codes:
+ * <ul>
+ * <li>0 on success</li>
+ * <li>1 on general error</li>
+ * <li>2 on failing migration.</li>
+ * </ul>
+ *
* @author Qiang Xue <qiang.xue@gmail.com>
* @version $Id$
* @package system.cli.commands
@@ -60,22 +67,24 @@ class MigrateCommand extends CConsoleCommand
public function beforeAction($action,$params)
{
$path=Yii::getPathOfAlias($this->migrationPath);
- if($path===false || !is_dir($path))
- die('Error: The migration directory does not exist: '.$this->migrationPath."\n");
+ if($path===false || !is_dir($path)) {
+ echo 'Error: The migration directory does not exist: '.$this->migrationPath."\n";
+ exit(1);
+ }
$this->migrationPath=$path;
$yiiVersion=Yii::getVersion();
echo "\nYii Migration Tool v1.0 (based on Yii v{$yiiVersion})\n\n";
- return true;
+ return parent::beforeAction($action,$params);
}
public function actionUp($args)
{
if(($migrations=$this->getNewMigrations())===array())
{
echo "No new migration found. Your system is up-to-date.\n";
- return;
+ return 0;
}
$total=count($migrations);
@@ -100,7 +109,7 @@ public function actionUp($args)
if($this->migrateUp($migration)===false)
{
echo "\nMigration failed. All later migrations are canceled.\n";
- return;
+ return 2;
}
}
echo "\nMigrated up successfully.\n";
@@ -110,13 +119,15 @@ public function actionUp($args)
public function actionDown($args)
{
$step=isset($args[0]) ? (int)$args[0] : 1;
- if($step<1)
- die("Error: The step parameter must be greater than 0.\n");
+ if($step<1) {
+ echo "Error: The step parameter must be greater than 0.\n";
+ return 1;
+ }
if(($migrations=$this->getMigrationHistory($step))===array())
{
echo "No migration has been done before.\n";
- return;
+ return 0;
}
$migrations=array_keys($migrations);
@@ -133,7 +144,7 @@ public function actionDown($args)
if($this->migrateDown($migration)===false)
{
echo "\nMigration failed. All later migrations are canceled.\n";
- return;
+ return 2;
}
}
echo "\nMigrated down successfully.\n";
@@ -143,13 +154,15 @@ public function actionDown($args)
public function actionRedo($args)
{
$step=isset($args[0]) ? (int)$args[0] : 1;
- if($step<1)
- die("Error: The step parameter must be greater than 0.\n");
+ if($step<1) {
+ echo "Error: The step parameter must be greater than 0.\n";
+ return 1;
+ }
if(($migrations=$this->getMigrationHistory($step))===array())
{
echo "No migration has been done before.\n";
- return;
+ return 0;
}
$migrations=array_keys($migrations);
@@ -166,15 +179,15 @@ public function actionRedo($args)
if($this->migrateDown($migration)===false)
{
echo "\nMigration failed. All later migrations are canceled.\n";
- return;
+ return 2;
}
}
foreach(array_reverse($migrations) as $migration)
{
if($this->migrateUp($migration)===false)
{
echo "\nMigration failed. All later migrations are canceled.\n";
- return;
+ return 2;
}
}
echo "\nMigration redone successfully.\n";
@@ -191,18 +204,17 @@ public function actionTo($args)
$originalVersion=$version;
if(preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/',$version,$matches))
$version='m'.$matches[1];
- else
- die("Error: The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).\n");
+ else {
+ echo "Error: The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).\n";
+ return 1;
+ }
// try migrate up
$migrations=$this->getNewMigrations();
foreach($migrations as $i=>$migration)
{
if(strpos($migration,$version.'_')===0)
- {
- $this->actionUp(array($i+1));
- return;
- }
+ return $this->actionUp(array($i+1));
}
// try migrate down
@@ -211,15 +223,16 @@ public function actionTo($args)
{
if(strpos($migration,$version.'_')===0)
{
- if($i===0)
+ if($i===0) {
echo "Already at '$originalVersion'. Nothing needs to be done.\n";
- else
- $this->actionDown(array($i));
- return;
+ return 0;
+ } else
+ return $this->actionDown(array($i));
}
}
- die("Error: Unable to find the version '$originalVersion'.\n");
+ echo "Error: Unable to find the version '$originalVersion'.\n";
+ return 1;
}
public function actionMark($args)
@@ -231,8 +244,10 @@ public function actionMark($args)
$originalVersion=$version;
if(preg_match('/^m?(\d{6}_\d{6})(_.*?)?$/',$version,$matches))
$version='m'.$matches[1];
- else
- die("Error: The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).\n");
+ else {
+ echo "Error: The version option must be either a timestamp (e.g. 101129_185401)\nor the full name of a migration (e.g. m101129_185401_create_user_table).\n";
+ return 1;
+ }
$db=$this->getDbConnection();
@@ -254,7 +269,7 @@ public function actionMark($args)
}
echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n";
}
- return;
+ return 0;
}
}
@@ -276,11 +291,12 @@ public function actionMark($args)
echo "The migration history is set at $originalVersion.\nNo actual migration was performed.\n";
}
}
- return;
+ return 0;
}
}
- die("Error: Unable to find the version '$originalVersion'.\n");
+ echo "Error: Unable to find the version '$originalVersion'.\n";
+ return 1;
}
public function actionHistory($args)
@@ -330,8 +346,10 @@ public function actionCreate($args)
else
$this->usageError('Please provide the name of the new migration.');
- if(!preg_match('/^\w+$/',$name))
- die("Error: The name of the migration must contain letters, digits and/or underscore characters only.\n");
+ if(!preg_match('/^\w+$/',$name)) {
+ echo "Error: The name of the migration must contain letters, digits and/or underscore characters only.\n";
+ return 1;
+ }
$name='m'.gmdate('ymd_His').'_'.$name;
$content=strtr($this->getTemplate(), array('{ClassName}'=>$name));
@@ -418,8 +436,9 @@ protected function getDbConnection()
return $this->_db;
else if(($this->_db=Yii::app()->getComponent($this->connectionID)) instanceof CDbConnection)
return $this->_db;
- else
- die("Error: CMigrationCommand.connectionID '{$this->connectionID}' is invalid. Please make sure it refers to the ID of a CDbConnection application component.\n");
+
+ echo "Error: CMigrationCommand.connectionID '{$this->connectionID}' is invalid. Please make sure it refers to the ID of a CDbConnection application component.\n";
+ exit(1);
}
protected function getMigrationHistory($limit)
View
3 framework/cli/commands/shell/ControllerCommand.php
@@ -77,14 +77,15 @@ public function getHelp()
/**
* Execute the action.
* @param array command line parameters specific for this command
+ * @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
if(!isset($args[0]))
{
echo "Error: controller name is required.\n";
echo $this->getHelp();
- return;
+ return 1;
}
$module=Yii::app();
View
3 framework/cli/commands/shell/CrudCommand.php
@@ -84,14 +84,15 @@ public function getHelp()
/**
* Execute the action.
* @param array command line parameters specific for this command
+ * @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
if(!isset($args[0]))
{
echo "Error: data model class is required.\n";
echo $this->getHelp();
- return;
+ return 1;
}
$module=Yii::app();
$modelClass=$args[0];
View
3 framework/cli/commands/shell/FormCommand.php
@@ -61,14 +61,15 @@ class file (e.g. 'application.models.ContactForm'). The former can
/**
* Execute the action.
* @param array command line parameters specific for this command
+ * @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
if(!isset($args[0],$args[1]))
{
echo "Error: both model class and view name are required.\n";
echo $this->getHelp();
- return;
+ return 1;
}
$scenario=isset($args[2]) ? $args[2] : '';
$modelClass=Yii::import($args[0],true);
View
2 framework/cli/commands/shell/HelpCommand.php
@@ -24,6 +24,7 @@ class HelpCommand extends CConsoleCommand
/**
* Execute the action.
* @param array command line parameters specific for this command
+ * @return integer non zero application exit code for help
*/
public function run($args)
{
@@ -54,6 +55,7 @@ public function run($args)
}
else
echo $runner->createCommand($name)->getHelp();
+ return 1;
}
/**
View
7 framework/cli/commands/shell/ModelCommand.php
@@ -246,14 +246,15 @@ protected function generateRelationName($tableName, $fkName, $multiple)
/**
* Execute the action.
* @param array command line parameters specific for this command
+ * @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
if(!isset($args[0]))
{
echo "Error: model class name is required.\n";
echo $this->getHelp();
- return;
+ return 1;
}
$className=$args[0];
@@ -262,7 +263,7 @@ public function run($args)
echo "Error: an active 'db' connection is required.\n";
echo "If you already added 'db' component in application configuration,\n";
echo "please quit and re-enter the yiic shell.\n";
- return;
+ return 1;
}
$db->active=true;
@@ -271,7 +272,7 @@ public function run($args)
if(!preg_match('/^[\w\.\-\*]*(.*?)$/',$className,$matches))
{
echo "Error: model class name is invalid.\n";
- return;
+ return 1;
}
if(empty($matches[1])) // without regular expression
View
3 framework/cli/commands/shell/ModuleCommand.php
@@ -44,14 +44,15 @@ public function getHelp()
/**
* Execute the action.
* @param array command line parameters specific for this command
+ * @return integer|null non zero application exit code for help or null on success
*/
public function run($args)
{
if(!isset($args[0]))
{
echo "Error: module ID is required.\n";
echo $this->getHelp();
- return;
+ return 1;
}
$moduleID=$args[0];
View
2 framework/console/CHelpCommand.php
@@ -33,6 +33,7 @@ class CHelpCommand extends CConsoleCommand
/**
* Execute the action.
* @param array $args command line parameters specific for this command
+ * @return integer non zero application exit code after printing help
*/
public function run($args)
{
@@ -62,6 +63,7 @@ public function run($args)
}
else
echo $runner->createCommand($name)->getHelp();
+ return 1;
}
/**

0 comments on commit 4fd934d

Please sign in to comment.