Commit 04735600 authored by Jon Pugh's avatar Jon Pugh

Create Aegir\Provision\Context::save()! Try `bin/provision save server_master...

Create Aegir\Provision\Context::save()! Try `bin/provision save server_master --remote_host=localhost`
parent 699bb1d1
......@@ -2,6 +2,7 @@
namespace Aegir\Provision;
use Drupal\Console\Core\Style\DrupalStyle;
use Symfony\Component\Console\Command\Command as BaseCommand;
use Drupal\Console\Core\Command\Shared\CommandTrait;
use Symfony\Component\Console\Input\InputInterface;
......@@ -27,6 +28,11 @@ abstract class Command extends BaseCommand
* @var \Symfony\Component\Console\Output\OutputInterface
*/
protected $output;
/**
* @var DrupalStyle;
*/
protected $io;
/**
* @var \Aegir\Provision\Console\Config
......@@ -43,6 +49,9 @@ abstract class Command extends BaseCommand
) {
$this->input = $input;
$this->output = $output;
$this->io = new DrupalStyle($input, $output);
$this->config = $this->getApplication()->getConfig();
}
......
......@@ -85,8 +85,23 @@ class SaveCommand extends Command
$context = new $class($input->getArgument('context_name'), $input->getOptions(), $this->getApplication()->getConfig()->all());
foreach ($context->getProperties() as $name => $value) {
$rows[] = [$name, $value];
}
$this->io->table(['Saving Context:', $context->name], $rows);
if ($this->io->confirm("Write configuration for <question>{$context->type}</question> context <question>{$context->name}</question> to <question>{$context->config_path}</question>?")) {
if ($context->save()) {
$this->io->success("Configuration saved to {$context->config_path}");
}
else {
$this->io->error("Unable to save configuration to {$context->config_path}. ");
}
}
$output->writeln(
"Saving context: ".print_r($context,1)
"Context Object: ".print_r($context,1)
);
// $command = 'drush provision-save '.$input->getArgument('context_name');
......
......@@ -7,6 +7,9 @@
namespace Aegir\Provision;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Dumper;
use Symfony\Component\Yaml\Yaml;
/**
......@@ -45,12 +48,15 @@ class Context
$this->console_config = $console_config;
$this->config_path = $console_config['config_path'] . '/provision/' . $this->type . '.' . $this->name . '.yml';
$configs = [];
$configs = [];
try {
$processor = new Processor();
if (file_exists($this->config_path)) {
$configs[] = Yaml::parse(file_get_contents($this->config_path));
$this->properties = Yaml::parse(file_get_contents($this->config_path));
$configs[] = $this->properties;
}
else {
// Load command line options into properties
......@@ -58,12 +64,53 @@ class Context
$this->properties[$option] = $options[$option];
}
}
$this->properties['context_type'] = $options['context_type'];
$this->config = $processor->processConfiguration($this, $configs);
} catch (\Exception $e) {
throw new \Exception(
"There is an error with the configuration for $this->type $this->name: " . $e->getMessage()
);
}
}
/**
* Return all properties for this context.
*
* @return array
*/
public function getProperties() {
return $this->properties;
}
/**
* Return all properties for this context.
*
* @return array
*/
public function getProperty($name) {
return $this->properties[$name];
}
/**
* 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 (IOException $e) {
return false;
}
}
}
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