Commit e6f86ede authored by Jon Pugh's avatar Jon Pugh

Adding Console\Config class for CLI configuration loaded from ~/.provision.yml

parent e15f2617
...@@ -8,7 +8,8 @@ ...@@ -8,7 +8,8 @@
"require": { "require": {
"drupal/console-core": "1.0.2", "drupal/console-core": "1.0.2",
"psy/psysh": "^0.8.11", "psy/psysh": "^0.8.11",
"symfony/console": "^3.3" "symfony/console": "^3.3",
"symfony/yaml": "^3.3"
}, },
"bin": ["bin/provision"], "bin": ["bin/provision"],
"authors": [ "authors": [
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "19713b21f2fe87c48f905e494077b6f9", "content-hash": "835ca80eb26b4d7e438015632aaeaaa2",
"packages": [ "packages": [
{ {
"name": "dflydev/dot-access-configuration", "name": "dflydev/dot-access-configuration",
......
...@@ -4,6 +4,10 @@ namespace Aegir\Provision; ...@@ -4,6 +4,10 @@ namespace Aegir\Provision;
use Aegir\Provision\Command\SaveCommand; use Aegir\Provision\Command\SaveCommand;
use Aegir\Provision\Command\ShellCommand; use Aegir\Provision\Command\ShellCommand;
use Aegir\Provision\Command\StatusCommand;
use Aegir\Provision\Console\Config;
use Symfony\Component\Console\Command\HelpCommand;
use Symfony\Component\Console\Command\ListCommand;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputDefinition; use Symfony\Component\Console\Input\InputDefinition;
...@@ -19,7 +23,7 @@ use Symfony\Component\Console\Application as BaseApplication; ...@@ -19,7 +23,7 @@ use Symfony\Component\Console\Application as BaseApplication;
/** /**
* Class Application * Class Application
* *
* @package Drupal\Console * @package Aegir\Provision
*/ */
class Application extends BaseApplication class Application extends BaseApplication
{ {
...@@ -33,6 +37,11 @@ class Application extends BaseApplication ...@@ -33,6 +37,11 @@ class Application extends BaseApplication
*/ */
const VERSION = '4.x'; const VERSION = '4.x';
/**
* @var string
*/
const CONSOLE_CONFIG = '.provision.yml';
/** /**
* @var string * @var string
*/ */
...@@ -46,17 +55,49 @@ class Application extends BaseApplication ...@@ -46,17 +55,49 @@ class Application extends BaseApplication
date_default_timezone_set($this::DEFAULT_TIMEZONE); date_default_timezone_set($this::DEFAULT_TIMEZONE);
} }
// Load Configs
$this->config = new Config();
parent::__construct($this::NAME, $this::VERSION); parent::__construct($this::NAME, $this::VERSION);
} }
/**
* @var Config
*/
private $config;
/**
* Getter for Configuration.
*
* @return Config
* Configuration object.
*/
public function getConfig()
{
return $this->config;
}
/**
* Setter for Configuration.
*
* @param Config $config
* Configuration object.
*/
public function setConfig(Config $config)
{
$this->config = $config;
}
/** /**
* Initializes all the default commands. * Initializes all the default commands.
*/ */
protected function getDefaultCommands() { protected function getDefaultCommands() {
$commands = parent::getDefaultCommands(); $commands[] = new HelpCommand();
$commands[] = new SaveCommand(); $commands[] = new ListCommand();
$commands[] = new ShellCommand(); $commands[] = new SaveCommand();
return $commands; $commands[] = new ShellCommand();
$commands[] = new StatusCommand();
return $commands;
} }
/** /**
......
...@@ -26,14 +26,20 @@ abstract class Command extends BaseCommand ...@@ -26,14 +26,20 @@ abstract class Command extends BaseCommand
*/ */
protected $output; protected $output;
/**
* @var Config
*/
private $config;
/** /**
* @param InputInterface $input An InputInterface instance * @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance * @param OutputInterface $output An OutputInterface instance
*/ */
protected function initialize(InputInterface $input, OutputInterface $output) protected function initialize(InputInterface $input, OutputInterface $output)
{ {
$this->input = $input; $this->input = $input;
$this->output = $output; $this->output = $output;
$this->config = $this->getApplication()->getConfig();
} }
/** /**
...@@ -45,4 +51,16 @@ abstract class Command extends BaseCommand ...@@ -45,4 +51,16 @@ abstract class Command extends BaseCommand
$this->output->writeln(["Running: $cmd"]); $this->output->writeln(["Running: $cmd"]);
shell_exec($cmd); shell_exec($cmd);
} }
/**
* Gets the application instance for this command.
*
* @return \Aegir\Provision\Application
*
* @api
*/
public function getApplication()
{
return parent::getApplication();
}
} }
<?php
namespace Aegir\Provision\Command;
use Aegir\Provision\Command;
use Drupal\Console\Core\Style\DrupalStyle;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Class StatusCommand
*
* @package Aegir\Provision\Command
*/
class StatusCommand extends Command
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this
->setName('status')
->setDescription('Display system status.')
->setHelp('Lists helpful information about your system.')
;
}
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$io->comment('Provision Status');
$headers = ['Name', 'Value'];
$config = $this->getApplication()->getConfig()->all();
foreach ($config as $key => $value) {
$rows[] = [$key, $value];
}
$io->table($headers, $rows);
}
}
<?php
namespace Aegir\Provision\Console;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
use Symfony\Component\Yaml\Yaml;
/**
* Class Config.
*/
class Config implements ConfigurationInterface
{
/**
* Configuration values array.
*
* @var array
*/
private $config = array();
/**
* Path to config YML file.
*
* @var string
*/
private $config_path = '';
/**
* Filename of config YML file.
*
* @var string
*/
private $config_filename = '.provision.yml';
/**
* {@inheritdoc}
*/
public function __construct()
{
$this->config_path = $this->getHomeDir() . '/' . $this->config_filename;
try {
$processor = new Processor();
$configs = func_get_args();
if (file_exists($this->config_path)) {
$configs[] = Yaml::parse(file_get_contents($this->config_path));
}
$this->config = $processor->processConfiguration($this, $configs);
} catch (\Exception $e) {
throw new \Exception('There is an error with your configuration: '.$e->getMessage());
}
}
/**
* {@inheritdoc}
*/
public function getConfigTreeBuilder()
{
$tree_builder = new TreeBuilder();
$root_node = $tree_builder->root('aegir');
$root_node
->children()
->scalarNode('aegir_root')
->defaultValue('/var/aegir')
->end()
->scalarNode('script_user')
->defaultValue('aegir')
->end()
->scalarNode('config_path')
->defaultValue('/var/aegir/config')
->end()
->end();
;
return $tree_builder;
}
/**
* Get a config param value.
*
* @param string $key
* Key of the param to get.
*
* @return mixed|null
* Value of the config param, or NULL if not present.
*/
public function get($key, $name = null)
{
if ($name) {
return array_key_exists($name, $this->config[$key]) ? $this->config[$key][$name] : null;
} else {
return $this->has($key) ? $this->config[$key] : null;
}
}
/**
* Check if config param is present.
*
* @param string $key
* Key of the param to check.
*
* @return bool
* TRUE if key exists.
*/
public function has($key)
{
return array_key_exists($key, $this->config);
}
/**
* Set a config param value.
*
* @param string $key
* Key of the param to get.
* @param mixed $val
* Value of the param to set.
*
* @return bool
*/
public function set($key, $val)
{
return $this->config[$key] = $val;
}
/**
* Get all config values.
*
* @return array
* All config galues.
*/
public function all()
{
return $this->config;
}
/**
* Add a config param value to a config array.
*
* @param string $key
* Key of the group to set to.
* @param string|array $names
* Name of the new object to set.
* @param mixed $val
* Value of the new object to set.
*
* @return bool
*/
public function add($key, $names, $val)
{
if (is_array($names)) {
$array_piece = &$this->config[$key];
foreach ($names as $name_key) {
$array_piece = &$array_piece[$name_key];
}
return $array_piece = $val;
} else {
return $this->config[$key][$names] = $val;
}
}
/**
* Remove a config param from a config array.
*
* @param $key
* @param $name
*
* @return bool
*/
public function remove($key, $name)
{
if (isset($this->config[$key][$name])) {
unset($this->config[$key][$name]);
return true;
} else {
return false;
}
}
/**
* Saves the config class to file.
* @return bool
*/
public function save()
{
// Create config folder if it does not exist.
$fs = new Filesystem();
$dumper = new Dumper();
try {
$fs->dumpFile($this->config_path, $dumper->dump($this->config, 10));
return true;
} catch (IOExceptionInterface $e) {
return false;
}
}
/**
* Returns the appropriate home directory.
*
* Adapted from Terminus Package Manager by Ed Reel
* @author Ed Reel <@uberhacker>
* @url https://github.com/uberhacker/tpm
*
* @return string
*/
protected function getHomeDir()
{
$home = getenv('HOME');
if (!$home) {
$system = '';
if (getenv('MSYSTEM') !== null) {
$system = strtoupper(substr(getenv('MSYSTEM'), 0, 4));
}
if ($system != 'MING') {
$home = getenv('HOMEPATH');
}
}
return $home;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment