2011-11-23 23:15:37 -02:00
<? php
2013-11-05 14:12:09 -02:00
/*
* This file is part of the Magallanes package.
*
* (c) Andrés Montañez <andres@andresmontanez.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2013-11-06 12:44:05 -02:00
namespace Mage\Command\BuiltIn ;
use Mage\Command\AbstractCommand ;
use Mage\Console ;
use Exception ;
/**
* Command for Adding elements to the Configuration.
* Currently elements allowed to add:
* - environments
*
* @author Andrés Montañez <andres@andresmontanez.com>
*/
class AddCommand extends AbstractCommand
2011-11-23 23:15:37 -02:00
{
2015-03-08 14:46:39 +01:00
public function __construct ()
{
$this -> setName ( 'Add command' )
-> setHelpMessage ( 'Generates new config for Magallanes. For now, only adding a environment is possible' )
-> setSyntaxMessage ( 'mage add [environment] [--name=env_name] [--enableReleases]' )
-> addUsageExample (
'mage add environment --name=production' ,
'Add a production environment'
)
-> addUsageExample (
'mage add environment --name=qa --enableReleases' ,
'Add a QA environment and enable releasing'
);
}
2014-08-06 14:01:39 -03:00
/**
* Adds new Configuration Elements
* @see \Mage\Command\AbstractCommand::run()
* @throws Exception
*/
2012-09-21 00:23:07 -03:00
public function run ()
2011-11-23 23:15:37 -02:00
{
2012-09-21 00:23:07 -03:00
$subCommand = $this -> getConfig () -> getArgument ( 1 );
try {
switch ( $subCommand ) {
case 'environment' :
2013-11-06 12:44:05 -02:00
$this -> addEnvironment ();
break ;
default ;
throw new Exception ( 'The Type of Add is needed.' );
2012-09-21 00:23:07 -03:00
break ;
}
2013-11-06 12:44:05 -02:00
} catch ( Exception $exception ) {
Console :: output ( '<red>' . $exception -> getMessage () . '</red>' , 1 , 2 );
2012-09-21 00:23:07 -03:00
}
}
2013-11-06 12:44:05 -02:00
/**
* Adds an Environment
*
* @throws Exception
*/
protected function addEnvironment ()
2012-09-21 00:23:07 -03:00
{
$withReleases = $this -> getConfig () -> getParameter ( 'enableReleases' , false );
$environmentName = strtolower ( $this -> getConfig () -> getParameter ( 'name' ));
if ( $environmentName == '' ) {
throw new Exception ( 'You must specify a name for the environment.' );
}
2014-07-16 15:52:23 +02:00
$environmentConfigFile = getcwd () . '/.mage/config/environment/' . $environmentName . '.yml' ;
2012-09-21 00:23:07 -03:00
2011-11-23 23:15:37 -02:00
if ( file_exists ( $environmentConfigFile )) {
2012-09-21 00:23:07 -03:00
throw new Exception ( 'The environment already exists.' );
}
2014-11-01 19:31:04 -02:00
Console :: output ( 'Adding new environment: <bold>' . $environmentName . '</bold>' );
2012-09-21 00:23:07 -03:00
$releasesConfig = 'releases:' . PHP_EOL
2014-08-06 14:01:39 -03:00
. ' enabled: true' . PHP_EOL
. ' max: 10' . PHP_EOL
. ' symlink: current' . PHP_EOL
. ' directory: releases' . PHP_EOL ;
2012-09-21 00:23:07 -03:00
$baseConfig = '#' . $environmentName . PHP_EOL
2014-08-06 14:01:39 -03:00
. 'deployment:' . PHP_EOL
. ' user: dummy' . PHP_EOL
. ' from: ./' . PHP_EOL
. ' to: /var/www/vhosts/example.com/www' . PHP_EOL
. ' excludes:' . PHP_EOL
. ( $withReleases ? $releasesConfig : '' )
. 'hosts:' . PHP_EOL
. 'tasks:' . PHP_EOL
. ' pre-deploy:' . PHP_EOL
. ' on-deploy:' . PHP_EOL
. ( $withReleases ? ( ' post-release:' . PHP_EOL ) : '' )
. ' post-deploy:' . PHP_EOL ;
2013-06-17 22:41:08 -03:00
2012-09-21 00:23:07 -03:00
$result = file_put_contents ( $environmentConfigFile , $baseConfig );
if ( $result ) {
2014-11-01 19:31:04 -02:00
Console :: output ( '<light_green>Success!!</light_green> Environment config file for <bold>' . $environmentName . '</bold> created successfully at <blue>' . $environmentConfigFile . '</blue>' );
Console :: output ( '<bold>So please! Review and adjust its configuration.</bold>' , 2 , 2 );
2011-11-23 23:15:37 -02:00
} else {
2014-11-01 19:31:04 -02:00
Console :: output ( '<light_red>Error!!</light_red> Unable to create config file for environment called <bold>' . $environmentName . '</bold>' , 1 , 2 );
2011-11-23 23:15:37 -02:00
}
}
2014-07-16 15:52:23 +02:00
}