Skip to content

Commit

Permalink
- Adding ability to specify context with "@alias" names, just like dr…
Browse files Browse the repository at this point in the history
…ush and provision 3.x.

- Use this new ability to pre-load the context to pre-load Robo Command files from services! Issue #26
- Add docker:command, docker:logs, and docker:shell commands to Contexts that are using Docker.
  • Loading branch information
jonpugh committed Mar 26, 2018
1 parent 3431be0 commit 70754c7
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 74 deletions.
6 changes: 3 additions & 3 deletions bin/provision-robo.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

use Aegir\Provision\Console\ProvisionStyle;
use Robo\Common\TimeKeeper;
use Symfony\Component\Console\Input\ArgvInput;
use Aegir\Provision\Console\ArgvInput;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Exception\CommandNotFoundException;

Expand Down Expand Up @@ -68,10 +68,10 @@
}

// Create the app.
$app = new \Aegir\Provision\Provision($config, $input, $output);
$provision = new \Aegir\Provision\Provision($config, $input, $output);

// Run the app.
$status_code = $app->run($input, $output);
$status_code = $provision->run($input, $output);

}
catch (Exception $e) {
Expand Down
8 changes: 4 additions & 4 deletions src/Provision/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ protected function doRunCommand( BaseCommand $command, InputInterface $input, Ou
$exitCode = parent::doRunCommand($command, $input, $output);
return $exitCode;
}

/**
* {@inheritdoc}
*
Expand All @@ -187,9 +187,9 @@ protected function getDefaultInputDefinition()
$inputDefinition = parent::getDefaultInputDefinition();
$inputDefinition->addOption(
new InputOption(
'--target',
'-t',
InputOption::VALUE_NONE,
'--context',
'-c',
InputOption::VALUE_OPTIONAL,
'The target context to act on.'
)
);
Expand Down
10 changes: 6 additions & 4 deletions src/Provision/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ abstract class Command extends BaseCommand
use ProvisionAwareTrait;
use LoggerAwareTrait;

const CONTEXT_REQUIRED = FALSE;

/**
* @var \Symfony\Component\Console\Input\InputInterface
*/
Expand Down Expand Up @@ -72,11 +74,11 @@ protected function initialize(
$this->io = new ProvisionStyle($input, $output);

// Load active context if a command uses the argument.
if ($this->input->hasArgument('context_name') && !empty($this->input->getArgument('context_name'))) {
if ($this->input->getOption('context') && !empty($this->input->getOption('context'))) {

try {
// Load context from context_name argument.
$this->context_name = $this->input->getArgument('context_name');
$this->context_name = $this->input->getOption('context');
$this->context = $this->getProvision()->getContext($this->context_name);
}
catch (\Exception $e) {
Expand All @@ -94,9 +96,9 @@ protected function initialize(
}

// If context_name is not specified, ask for it.
elseif ($this->input->hasArgument('context_name') && $this->getDefinition()->getArgument('context_name')->isRequired() && empty($this->input->getArgument('context_name'))) {
elseif ($this::CONTEXT_REQUIRED && empty($this->input->getOption('context'))) {
$this->askForContext();
$this->input->setArgument('context_name', $this->context_name);
$this->input->setOption('context', $this->context_name);

try {
$this->context = $this->getProvision()->getContext($this->context_name);
Expand Down
22 changes: 11 additions & 11 deletions src/Provision/Command/SaveCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
*/
class SaveCommand extends Command
{


/**
* This command needs a context.
*/
const CONTEXT_REQUIRED = TRUE;

/**
* @var string
*/
Expand Down Expand Up @@ -61,11 +66,6 @@ protected function configure()
protected function getCommandDefinition()
{
$inputDefinition = ServicesCommand::getCommandOptions();
$inputDefinition[] = new InputArgument(
'context_name',
InputArgument::REQUIRED,
'Context to save'
);
$inputDefinition[] = new InputOption(
'context_type',
null,
Expand Down Expand Up @@ -135,7 +135,7 @@ protected function getCommandDefinition()
protected function initialize(InputInterface $input, OutputInterface $output)
{
parent::initialize($input,$output);
$this->context_name = $input->getArgument('context_name');
$this->context_name = $input->getOption('context');
$this->context_type = $input->getOption('context_type');
}

Expand Down Expand Up @@ -199,7 +199,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$options['type'] = $this->context_type;

$class = Context::getClassName($this->input->getOption('context_type'));
$this->context = new $class($input->getArgument('context_name'), $this->getProvision(), $options);
$this->context = new $class($input->getOption('context'), $this->getProvision(), $options);
}
else {
$this->getProvision()->io()->helpBlock("Editing context {$this->context->name}...", ProvisionStyle::ICON_EDIT);
Expand Down Expand Up @@ -257,7 +257,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$this->io->warningLite('Context not saved.');
return;
}
// $command = 'drush provision-save '.$input->getArgument('context_name');
// $command = 'drush provision-save '.$input->getOption('context');
// $this->process($command);

// If editing a context, exit here.
Expand Down Expand Up @@ -437,7 +437,7 @@ protected function askForServices() {
}
$command = $this->getApplication()->find('services');
$arguments = [
'context_name' => $this->input->getArgument('context_name'),
'context_name' => $this->input->getOption('context'),
'sub_command' => 'add',
];
while ($this->io->confirm('Add a service?')) {
Expand Down Expand Up @@ -468,7 +468,7 @@ protected function askForServiceSubscriptions() {

$command = $this->getApplication()->find('services');
$arguments = [
'context_name' => $this->input->getArgument('context_name'),
'context_name' => $this->input->getOption('context'),
'sub_command' => 'add',
'service' => $type,
];
Expand Down
32 changes: 17 additions & 15 deletions src/Provision/Command/ServicesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
class ServicesCommand extends Command
{

/**
* This command needs a context.
*/
const CONTEXT_REQUIRED = TRUE;

/**
* "list" (default), "add", "remove", or "configure"
* @var string
Expand All @@ -45,7 +50,8 @@ protected function configure()
->setHelp(
'Use this command to add new services to servers, or to add service subscriptions to platforms and sites.'
)
->setDefinition($this->getCommandDefinition());
->setDefinition($this->getCommandDefinition())
;
}

/**
Expand All @@ -56,11 +62,7 @@ protected function configure()
protected function getCommandDefinition()
{
$inputDefinition = $this::getCommandOptions();
$inputDefinition[] = new InputArgument(
'context_name',
InputArgument::REQUIRED,
'Server to work on.'
);

$inputDefinition[] = new InputArgument(
'sub_command',
InputArgument::OPTIONAL,
Expand All @@ -83,7 +85,6 @@ protected function getCommandDefinition()
InputOption::VALUE_OPTIONAL,
'The name of the service type to use.'
);

return new InputDefinition($inputDefinition);
}

Expand Down Expand Up @@ -130,14 +131,15 @@ protected function initialize(
InputInterface $input,
OutputInterface $output
) {

if ($input->getArgument('context_name') == 'add') {
$this->sub_command = $input->getArgument('context_name');
$input->setArgument('context_name', NULL);
}
else {
$this->sub_command = $input->getArgument('sub_command');
}
//
// if ($input->getOption('context_name') == 'add') {
// $this->sub_command = $input->getArgument('context_name');
// $input->setArgument('context_name', NULL);
// }
// else {
// $this->sub_command = $input->getArgument('sub_command');
// }
$this->sub_command = $input->getArgument('sub_command');

parent::initialize(
$input,
Expand Down
11 changes: 2 additions & 9 deletions src/Provision/Command/StatusCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,6 @@ protected function configure()
->setName('status')
->setDescription('Display system status.')
->setHelp('Lists helpful information about your system.')
->setDefinition([
new InputArgument(
'context_name',
InputArgument::OPTIONAL,
'Context to show info for.'
)
])
;
}

Expand All @@ -49,14 +42,14 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$this->getProvision();

if ($input->getArgument('context_name')) {
if ($input->getOption('context')) {
$rows = [['Configuration File', $this->context->config_path]];
foreach ($this->context->getProperties() as $name => $value) {
if (is_string($value)) {
$rows[] = [$name, $value];
}
}
$this->io->table(['Provision Context:', $input->getArgument('context_name')], $rows);
$this->io->table(['Provision Context:', $input->getOption('context')], $rows);

// Display services.
$this->context->showServices($this->io);
Expand Down
10 changes: 5 additions & 5 deletions src/Provision/Command/VerifyCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
class VerifyCommand extends Command
{

/**
* This command needs a context.
*/
const CONTEXT_REQUIRED = TRUE;

/**
* {@inheritdoc}
*/
Expand All @@ -47,11 +52,6 @@ protected function configure()
protected function getCommandDefinition()
{
$inputDefinition = [];
$inputDefinition[] = new InputArgument(
'context_name',
InputArgument::REQUIRED,
'Context to verify'
);
return new InputDefinition($inputDefinition);
}

Expand Down
37 changes: 37 additions & 0 deletions src/Provision/Console/ArgvInput.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Aegir\Provision\Console;

use \Symfony\Component\Console\Input\ArgvInput as ArgvInputBase;
use \Symfony\Component\Console\Input\InputDefinition;

class ArgvInput extends ArgvInputBase {

/**
* @var string The name of the active context, extracted from the first argument if it has "@" prefix.
*/
public $activeContextName = NULL;

/**
* @param array|null $argv An array of parameters from the CLI (in the argv format)
* @param InputDefinition|null $definition A InputDefinition instance
*/
public function __construct(array $argv = null, InputDefinition $definition = null)
{
// If @alias is used, swap it out with --context=
if (isset($argv[1]) && strpos($argv[1], '@') === 0) {
$context_name = ltrim($argv[1], '@');
$argv[1] = "--context={$context_name}";
$this->activeContextName = $context_name;
}
// If --context option is used, use that.
elseif ($argv_filtered = array_filter($argv, function ($key) {
return strpos($key, '--context') === 0;
})) {
$context_option = array_pop($argv_filtered);
$context_name = substr($context_option, strlen('--context='));
$this->activeContextName = $context_name;
}
parent::__construct($argv, $definition);
}
}
Loading

0 comments on commit 70754c7

Please sign in to comment.