Domain list command, abstraction and helpers
This commit is contained in:
parent
71735c752c
commit
92a560056e
|
@ -4,6 +4,7 @@
|
||||||
require_once __DIR__ . '/../vendor/autoload.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
use Deblan\Console\Application;
|
use Deblan\Console\Application;
|
||||||
|
use Deblan\Console\Command\Input\Input;
|
||||||
|
|
||||||
$app = new Application();
|
$app = new Application();
|
||||||
|
|
||||||
|
@ -12,4 +13,4 @@ $app->chdir(__DIR__.'/../');
|
||||||
$app->addCommandsPath('src/Deblan/PowerDNS/Command/', 'Deblan\\PowerDNS\\Command');
|
$app->addCommandsPath('src/Deblan/PowerDNS/Command/', 'Deblan\\PowerDNS\\Command');
|
||||||
$app->initPropel();
|
$app->initPropel();
|
||||||
$app->loadCommands();
|
$app->loadCommands();
|
||||||
$app->run();;
|
$app->run(new Input());
|
||||||
|
|
|
@ -5,6 +5,8 @@ namespace Deblan\Console\Command;
|
||||||
use Symfony\Component\Console\Command\Command;
|
use Symfony\Component\Console\Command\Command;
|
||||||
use Symfony\Component\Console\Output\OutputInterface;
|
use Symfony\Component\Console\Output\OutputInterface;
|
||||||
use Symfony\Component\Console\Input\InputInterface;
|
use Symfony\Component\Console\Input\InputInterface;
|
||||||
|
use Deblan\PowerDNS\Command\Helper\ZoneHelper;
|
||||||
|
use Deblan\PowerDNS\Command\Helper\DomainHelper;
|
||||||
|
|
||||||
abstract class AbstractCommand extends Command
|
abstract class AbstractCommand extends Command
|
||||||
{
|
{
|
||||||
|
@ -12,6 +14,19 @@ abstract class AbstractCommand extends Command
|
||||||
|
|
||||||
protected $output;
|
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)
|
protected function execute(InputInterface $input, OutputInterface $output)
|
||||||
{
|
{
|
||||||
$this->setInput($input);
|
$this->setInput($input);
|
||||||
|
|
53
src/Deblan/Console/Command/Helper/AbstractHelper.php
Normal file
53
src/Deblan/Console/Command/Helper/AbstractHelper.php
Normal 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();
|
||||||
|
}
|
26
src/Deblan/Console/Command/Input/Input.php
Normal file
26
src/Deblan/Console/Command/Input/Input.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,8 +3,6 @@
|
||||||
namespace Deblan\PowerDNS\Command;
|
namespace Deblan\PowerDNS\Command;
|
||||||
|
|
||||||
use Deblan\Console\Command\AbstractCommand;
|
use Deblan\Console\Command\AbstractCommand;
|
||||||
use Deblan\PowerDNS\Model\Zone;
|
|
||||||
use Deblan\PowerDNS\Model\ZoneVersion;
|
|
||||||
use Symfony\Component\Console\Input\InputOption;
|
use Symfony\Component\Console\Input\InputOption;
|
||||||
|
|
||||||
abstract class AbstractZoneCommand extends AbstractCommand
|
abstract class AbstractZoneCommand extends AbstractCommand
|
||||||
|
@ -15,56 +13,4 @@ abstract class AbstractZoneCommand extends AbstractCommand
|
||||||
->addOption('active', null, InputOption::VALUE_NONE, '')
|
->addOption('active', null, InputOption::VALUE_NONE, '')
|
||||||
->addOption('no-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()
|
|
||||||
));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
59
src/Deblan/PowerDNS/Command/DomainListCommand.php
Normal file
59
src/Deblan/PowerDNS/Command/DomainListCommand.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
27
src/Deblan/PowerDNS/Command/Helper/DomainHelper.php
Normal file
27
src/Deblan/PowerDNS/Command/Helper/DomainHelper.php
Normal 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';
|
||||||
|
}
|
||||||
|
}
|
67
src/Deblan/PowerDNS/Command/Helper/ZoneHelper.php
Normal file
67
src/Deblan/PowerDNS/Command/Helper/ZoneHelper.php
Normal 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';
|
||||||
|
}
|
||||||
|
}
|
|
@ -30,7 +30,7 @@ class ZoneListCommand extends AbstractZoneCommand
|
||||||
$zones = $query->find();
|
$zones = $query->find();
|
||||||
|
|
||||||
foreach ($zones as $key => $zone) {
|
foreach ($zones as $key => $zone) {
|
||||||
$this->showZone($zone, $key);
|
$this->getHelper('zone')->showZone($zone, $key);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -58,6 +58,10 @@
|
||||||
<column name="type" phpName="Type" type="VARCHAR" size="6" required="true"/>
|
<column name="type" phpName="Type" type="VARCHAR" size="6" required="true"/>
|
||||||
<column name="notified_serial" phpName="NotifiedSerial" type="INTEGER"/>
|
<column name="notified_serial" phpName="NotifiedSerial" type="INTEGER"/>
|
||||||
<column name="account" phpName="Account" type="VARCHAR" size="40"/>
|
<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 name="name_index">
|
||||||
<unique-column name="name"/>
|
<unique-column name="name"/>
|
||||||
</unique>
|
</unique>
|
||||||
|
|
Loading…
Reference in a new issue