Domain list command, abstraction and helpers

This commit is contained in:
Simon Vieille 2015-02-18 22:48:56 +01:00
parent 71735c752c
commit 92a560056e
10 changed files with 254 additions and 56 deletions

View File

@ -4,6 +4,7 @@
require_once __DIR__ . '/../vendor/autoload.php';
use Deblan\Console\Application;
use Deblan\Console\Command\Input\Input;
$app = new Application();
@ -12,4 +13,4 @@ $app->chdir(__DIR__.'/../');
$app->addCommandsPath('src/Deblan/PowerDNS/Command/', 'Deblan\\PowerDNS\\Command');
$app->initPropel();
$app->loadCommands();
$app->run();;
$app->run(new Input());

View File

@ -5,6 +5,8 @@ namespace Deblan\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputInterface;
use Deblan\PowerDNS\Command\Helper\ZoneHelper;
use Deblan\PowerDNS\Command\Helper\DomainHelper;
abstract class AbstractCommand extends Command
{
@ -12,6 +14,19 @@ abstract class AbstractCommand extends Command
protected $output;
public function getHelper($helper)
{
if ($helper === 'zone') {
return ZoneHelper::getInstance($this->getInput(), $this->getOutput());
}
if ($helper === 'domain') {
return DomainHelper::getInstance($this->getInput(), $this->getOutput());
}
throw new \InvalidArgumentException(sprintf('Invalid helper "%s"', $helper));
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->setInput($input);

View File

@ -0,0 +1,53 @@
<?php
namespace Deblan\Console\Command\Helper;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
abstract class AbstractHelper
{
protected static $instances = [];
public static function getInstance(InputInterface $input, OutputInterface $output)
{
$class = get_called_class();
$name = $class::getName();
if (empty(self::$instances[$name])) {
$instance = (new $class)
->setInput($input)
->setOutput($output);
self::$instances[$name] = $instance;
}
return self::$instances[$name];
}
public function setInput(InputInterface $input)
{
$this->input = $input;
return $this;
}
public function getInput()
{
return $this->input;
}
public function setOutput(OutputInterface $output)
{
$this->output = $output;
return $this;
}
public function getOutput()
{
return $this->output;
}
abstract public static function getName();
}

View File

@ -0,0 +1,26 @@
<?php
namespace Deblan\Console\Command\Input;
use Symfony\Component\Console\Input\ArgvInput;
class Input extends ArgvInput
{
public function getArgument($name)
{
try {
return parent::getArgument($name);
} catch (\InvalidArgumentException $e) {
return null;
}
}
public function getOption($name)
{
try {
return parent::getOption($name);
} catch (\InvalidArgumentException $e) {
return null;
}
}
}

View File

@ -3,8 +3,6 @@
namespace Deblan\PowerDNS\Command;
use Deblan\Console\Command\AbstractCommand;
use Deblan\PowerDNS\Model\Zone;
use Deblan\PowerDNS\Model\ZoneVersion;
use Symfony\Component\Console\Input\InputOption;
abstract class AbstractZoneCommand extends AbstractCommand
@ -15,56 +13,4 @@ abstract class AbstractZoneCommand extends AbstractCommand
->addOption('active', null, InputOption::VALUE_NONE, '')
->addOption('no-active', null, InputOption::VALUE_NONE, '');
}
protected function showZone(Zone $zone, $key)
{
$this->getOutput()->writeln(sprintf('<info>%s</info>.', $zone->getName()));
if ($zone->getDescription()) {
$this->getOutput()->writeln($zone->getDescription());
}
foreach ($zone->getZoneVersions() as $key => $zoneVersion) {
$this->showZoneVersion($zoneVersion, $key);
}
}
protected function showZoneVersion(ZoneVersion $zoneVersion, $key)
{
if ($this->getInput()->getOption('active') && false === $zoneVersion->getIsActive()) {
return;
}
if ($this->getInput()->getOption('no-active') && true === $zoneVersion->getIsActive()) {
return;
}
$this->getOutput()->writeln('');
$this->getOutput()->writeln(sprintf(
'<info>Version</info>: <comment>%d</comment> - <info>Active</info>: %s',
$zoneVersion->getVersion(),
$zoneVersion->getIsActive() ? 'Yes' : 'No'
));
$this->showZoneVersionRecords($zoneVersion);
}
protected function showZoneVersionRecords(ZoneVersion $zoneVersion)
{
$this->getOutput()->writeln('');
$this->getOutput()->writeln('<comment> ID | NAME | TYPE | TTL | PRIO | CONTENT</comment>');
$this->getOutput()->writeln('<comment>----------------------------------------------------------------------</comment>');
foreach ($zoneVersion->getZoneRecords() as $zoneRecord) {
$this->getOutput()->writeln(sprintf(
'%5d | %s | %s | %s | %s | %s',
$zoneRecord->getId(),
str_pad($zoneRecord->getName(), 21),
str_pad($zoneRecord->getType(), 9),
str_pad($zoneRecord->getTtl(), 6),
str_pad($zoneRecord->getPrio(), 7),
$zoneRecord->getContent()
));
}
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace Deblan\PowerDNS\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Command\Command;
use Deblan\PowerDNS\Model\DomainQuery;
use Deblan\Console\Command\AbstractCommand;
class DomainListCommand extends AbstractCommand
{
protected function configure()
{
parent::configure();
$this
->setName('domain:list')
->setDescription('List domains')
->addOption('short', null, InputOption::VALUE_NONE, '')
->addOption('zone', null, InputOption::VALUE_NONE, '')
->addOption('active', null, InputOption::VALUE_NONE, '')
->setHelp("The <info>%command.name%</info> ");
}
protected function execute(InputInterface $input, OutputInterface $output)
{
parent::execute($input, $output);
$query = $this->getDomainQuery();
$domains = $query->find();
foreach ($domains as $key => $domain) {
if ($this->getInput()->getOption('short')) {
$this->getHelper('domain')->showShortDomain($domain, $key);
continue;
}
$this->getHelper('domain')->showDomain($domain, $key);
if ($this->getInput()->getOption('zone') && $domain->getZone()) {
$this->getOutput()->writeln('');
$this->getHelper('zone')->showZone($domain->getZone());
$this->getOutput()->writeln('');
$this->getOutput()->writeln('');
}
}
}
protected function getDomainQuery()
{
$query = DomainQuery::create()->orderByName();
return $query;
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Deblan\PowerDNS\Command\Helper;
use Deblan\Console\Command\Helper\AbstractHelper;
use Deblan\PowerDNS\Model\Base\Domain;
class DomainHelper extends AbstractHelper
{
public function showDomain(Domain $domain, $key = 0)
{
$this->getOutput()->writeln(sprintf('DOMAIN: <info>%s</info>', $domain->getName()));
$this->getOutput()->writeln(sprintf('ID : <info>%d</info>', $domain->getId()));
$this->getOutput()->writeln(sprintf('TYPE : <info>%s</info>', $domain->getType()));
$this->getOutput()->writeln(sprintf('MASTER: <info>%s</info>', $domain->getMaster()));
}
public function showShortDomain(Domain $domain, $key = 0)
{
$this->getOutput()->writeln(sprintf('<info>%s</info>', $domain->getName()));
}
public static function getName()
{
return 'domain';
}
}

View File

@ -0,0 +1,67 @@
<?php
namespace Deblan\PowerDNS\Command\Helper;
use Deblan\Console\Command\Helper\AbstractHelper;
use Deblan\PowerDNS\Model\Base\Zone;
use Deblan\PowerDNS\Model\Base\ZoneVersion;
class ZoneHelper extends AbstractHelper
{
public function showZone(Zone $zone, $key = 0)
{
$this->getOutput()->writeln(sprintf('<info>%s</info>.', $zone->getName()));
if ($zone->getDescription()) {
$this->getOutput()->writeln($zone->getDescription());
}
foreach ($zone->getZoneVersions() as $key => $zoneVersion) {
$this->showZoneVersion($zoneVersion, $key);
}
}
public function showZoneVersion(ZoneVersion $zoneVersion, $key = 0)
{
if ($this->getInput()->getOption('active') && false === $zoneVersion->getIsActive()) {
return;
}
if ($this->getInput()->getOption('no-active') && true === $zoneVersion->getIsActive()) {
return;
}
$this->getOutput()->writeln('');
$this->getOutput()->writeln(sprintf(
'<info>Version</info>: <comment>%d</comment> - <info>Active</info>: %s',
$zoneVersion->getVersion(),
$zoneVersion->getIsActive() ? 'Yes' : 'No'
));
$this->showZoneVersionRecords($zoneVersion);
}
public function showZoneVersionRecords(ZoneVersion $zoneVersion)
{
$this->getOutput()->writeln('');
$this->getOutput()->writeln('<comment> ID | NAME | TYPE | TTL | PRIO | CONTENT</comment>');
$this->getOutput()->writeln('<comment>----------------------------------------------------------------------</comment>');
foreach ($zoneVersion->getZoneRecords() as $zoneRecord) {
$this->getOutput()->writeln(sprintf(
'%5d | %s | %s | %s | %s | %s',
$zoneRecord->getId(),
str_pad($zoneRecord->getName(), 21),
str_pad($zoneRecord->getType(), 9),
str_pad($zoneRecord->getTtl(), 6),
str_pad($zoneRecord->getPrio(), 7),
$zoneRecord->getContent()
));
}
}
public static function getName()
{
return 'zone';
}
}

View File

@ -30,7 +30,7 @@ class ZoneListCommand extends AbstractZoneCommand
$zones = $query->find();
foreach ($zones as $key => $zone) {
$this->showZone($zone, $key);
$this->getHelper('zone')->showZone($zone, $key);
}
}

View File

@ -58,6 +58,10 @@
<column name="type" phpName="Type" type="VARCHAR" size="6" required="true"/>
<column name="notified_serial" phpName="NotifiedSerial" type="INTEGER"/>
<column name="account" phpName="Account" type="VARCHAR" size="40"/>
<column name="zone_id" type="INTEGER" required="false" />
<foreign-key foreignTable="zone" onDelete="setNull" onUpdate="cascade">
<reference local="zone_id" foreign="id"/>
</foreign-key>
<unique name="name_index">
<unique-column name="name"/>
</unique>