Skip to content

Commit

Permalink
implemented list:backups and interact of get command (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes authored Apr 4, 2017
1 parent 8147929 commit fd69e33
Show file tree
Hide file tree
Showing 11 changed files with 474 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Bundle/Command/GetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

/**
* Get specified backup from another server.
Expand All @@ -18,6 +19,7 @@ protected function configure()
$this->setName('get')
->addArgument('source-server', InputArgument::REQUIRED, 'Source of backup which should be loaded.')
->addArgument('name', InputArgument::REQUIRED, 'Name of loading backup.')
->addOption('latest', null, InputOption::VALUE_NONE, 'Loads the latest file.')
->setDescription('Get specified backup from another server.')
->setHelp(
<<<EOT
Expand Down
47 changes: 47 additions & 0 deletions src/Bundle/Command/ListBackupsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Nanbando\Bundle\Command;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;

/**
* Lists available backups.
*/
class ListBackupsCommand extends BaseServerCommand
{
/**
* {@inheritdoc}
*/
protected function configure()
{
$this->setName('list:backups')
->addOption('server', 's', InputOption::VALUE_REQUIRED, 'Where should the command be called.', 'local')
->addOption('remote', 'r', InputOption::VALUE_NONE, 'Lists backups on remote storage.')
->setDescription('List all available backups.')
->setHelp(
<<<EOT
The <info>{$this->getName()}</info> command lists all available backups from local.
With the options <info>-s</info> or <info>-r</info> lists backups on servers or remote storage.
EOT
);
}

/**
* {@inheritdoc}
*/
protected function getServerName(InputInterface $input)
{
return $input->getOption('server');
}

/**
* {@inheritdoc}
*/
protected function getCommandOptions(InputInterface $input)
{
return ['remote' => $input->getOption('remote')];
}
}
7 changes: 7 additions & 0 deletions src/Bundle/Resources/config/local-commands.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,12 @@

<tag name="nanbando.server_command" server="local" command="information"/>
</service>

<service id="nanbando.server.local.list_backups" class="Nanbando\Core\Server\Command\Local\LocalListBackupsCommand">
<argument type="service" id="storage"/>
<argument type="service" id="output"/>

<tag name="nanbando.server_command" server="local" command="list:backups"/>
</service>
</services>
</container>
9 changes: 9 additions & 0 deletions src/Bundle/Resources/config/ssh-commands.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,14 @@

<tag name="nanbando.ssh.abstract_server_command" command="information"/>
</service>

<service id="nanbando.server.abstract_ssh.list_backups"
class="Nanbando\Core\Server\Command\Ssh\SshListBackupsCommand"
abstract="true">
<argument type="service"><!-- ssh --></argument>
<argument type="service" id="output"/>

<tag name="nanbando.ssh.abstract_server_command" command="list:backups"/>
</service>
</services>
</container>
71 changes: 71 additions & 0 deletions src/Core/Server/Command/Local/LocalListBackupsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Nanbando\Core\Server\Command\Local;

use Nanbando\Core\Server\Command\CommandInterface;
use Nanbando\Core\Storage\StorageInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Lists local backups.
*/
class LocalListBackupsCommand implements CommandInterface
{
/**
* @var StorageInterface
*/
private $storage;

/**
* @var OutputInterface
*/
private $output;

/**
* @param StorageInterface $storage
* @param OutputInterface $output
*/
public function __construct(StorageInterface $storage, OutputInterface $output)
{
$this->storage = $storage;
$this->output = $output;
}

/**
* {@inheritdoc}
*/
public function interact(InputInterface $input, OutputInterface $output)
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function execute(array $options = [])
{
$remote = array_key_exists('remote', $options) ? $options['remote'] : false;
$files = $this->getFiles($remote);

foreach ($files as $file) {
$this->output->writeln($file);
}
}

/**
* Returns local of remote files.
*
* @param bool $remote
*
* @return string[]
*/
private function getFiles($remote)
{
if ($remote) {
return $this->storage->remoteListing();
}

return $this->storage->localListing();
}
}
33 changes: 32 additions & 1 deletion src/Core/Server/Command/Ssh/SshGetCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

use Nanbando\Core\Server\Command\CommandInterface;
use Nanbando\Core\Storage\StorageInterface;
use Symfony\Component\Console\Helper\QuestionHelper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion;

/**
* Get a backup from a ssh connected server.
Expand Down Expand Up @@ -44,7 +46,36 @@ public function __construct(SshConnection $connection, StorageInterface $storage
*/
public function interact(InputInterface $input, OutputInterface $output)
{
// do nothing
if ($input->getArgument('name')) {
return;
}

$files = [];
$this->connection->executeNanbando(
'list:backups',
[],
function ($line) use (&$files) {
if (empty($line) || $line === "\n") {
return;
}

$files[] = $line;
}
);

if ($input->getOption('latest') && count($files) > 0) {
return $input->setArgument('name', end($files));
} elseif (count($files) === 1) {
return $input->setArgument('name', $files[0]);
}

$helper = new QuestionHelper();
$question = new ChoiceQuestion('Which backup', $files);
$question->setErrorMessage('Backup %s is invalid.');
$question->setAutocompleterValues([]);

$input->setArgument('name', $helper->ask($input, $output, $question));
$output->writeln('');
}

/**
Expand Down
60 changes: 60 additions & 0 deletions src/Core/Server/Command/Ssh/SshListBackupsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Nanbando\Core\Server\Command\Ssh;

use Nanbando\Core\Server\Command\CommandInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Lists ssh backups.
*/
class SshListBackupsCommand implements CommandInterface
{
/**
* @var SshConnection
*/
private $connection;

/**
* @var OutputInterface
*/
private $output;

/**
* @param SshConnection $connection
* @param OutputInterface $output
*/
public function __construct(SshConnection $connection, OutputInterface $output)
{
$this->connection = $connection;
$this->output = $output;
}

/**
* {@inheritdoc}
*/
public function interact(InputInterface $input, OutputInterface $output)
{
// do nothing
}

/**
* {@inheritdoc}
*/
public function execute(array $options = [])
{
$parameters = [];
if (array_key_exists('remote', $options) && $options['remote']) {
$parameters['-r'] = '';
}

$this->connection->executeNanbando(
'list:backups',
$parameters,
function ($line) {
$this->output->write($line);
}
);
}
}
66 changes: 66 additions & 0 deletions tests/Unit/Bundle/Command/GetCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Unit\Bundle\Command;

use Nanbando\Bundle\Command\GetCommand;
use Nanbando\Core\Server\Command\CommandInterface;
use Nanbando\Core\Server\ServerRegistry;
use Prophecy\Argument;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Tests for class "GetCommand".
*/
class GetCommandTest extends \PHPUnit_Framework_TestCase
{
/**
* @var ServerRegistry
*/
private $serverRegistry;

/**
* @var ContainerInterface
*/
private $container;

/**
* {@inheritdoc}
*/
protected function setUp()
{
$this->serverRegistry = $this->prophesize(ServerRegistry::class);
$this->container = $this->prophesize(ContainerInterface::class);
}

private function getCommandTester()
{
$this->container->get('nanbando.server_registry')->willReturn($this->serverRegistry->reveal());

$command = new GetCommand();
$command->setContainer($this->container->reveal());

$application = new Application();
$application->add($command);

$command = $application->find('get');

return new CommandTester($command);
}

public function testExecute()
{
$command = $this->prophesize(CommandInterface::class);
$command->interact(Argument::type(InputInterface::class), Argument::type(OutputInterface::class))
->shouldBeCalled();
$command->execute(['name' => '2017-01-01-12-00-00'])->shouldBeCalled();

$this->serverRegistry->getCommand('local', 'get')->willReturn($command->reveal());

$commandTester = $this->getCommandTester();
$commandTester->execute(['source-server' => 'local', 'name' => '2017-01-01-12-00-00']);
}
}
Loading

0 comments on commit fd69e33

Please sign in to comment.