Skip to content

Commit

Permalink
Add a new command "view:domain" for displaying formatted Domain Name …
Browse files Browse the repository at this point in the history
…information
  • Loading branch information
flavioheleno committed Sep 23, 2023
1 parent 47aa081 commit b99fe85
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 5 deletions.
9 changes: 6 additions & 3 deletions bin/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
}

require_once dirname(__DIR__) . '/vendor/autoload.php';

use Composer\InstalledVersions;
use DI\ContainerBuilder;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
use Watchr\Console\Commands\Check\CheckAllCommand;
use Watchr\Console\Commands\Check\CheckCertificateCommand;
use Watchr\Console\Commands\Check\CheckDomainCommand;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\CommandLoader\ContainerCommandLoader;
use Watchr\Console\Commands\View\ViewDomainCommand;

define(
'__VERSION__',
Expand Down Expand Up @@ -52,7 +54,8 @@
[
CheckAllCommand::getDefaultName() => CheckAllCommand::class,
CheckCertificateCommand::getDefaultName() => CheckCertificateCommand::class,
CheckDomainCommand::getDefaultName() => CheckDomainCommand::class
CheckDomainCommand::getDefaultName() => CheckDomainCommand::class,
ViewDomainCommand::getDefaultName() => ViewDomainCommand::class
]
)
);
Expand Down
129 changes: 129 additions & 0 deletions src/Console/Commands/View/ViewDomainCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php
declare(strict_types = 1);

namespace Watchr\Console\Commands\View;

use DateTimeInterface;
use Exception;
use InvalidArgumentException;
use Psr\Clock\ClockInterface;
use RuntimeException;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Watchr\Console\Services\DomainService;
use Watchr\Console\Traits\DateUtilsTrait;

#[AsCommand('view:domain', 'View domain name details')]
final class ViewDomainCommand extends Command {
use DateUtilsTrait;

private ClockInterface $clock;
private DomainService $domainService;

protected function configure(): void {
$this
->addOption(
'json',
'j',
InputOption::VALUE_NONE,
'Format the output as a JSON string'
)
->addArgument(
'domain',
InputArgument::REQUIRED,
'Domain Name to be viewed'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
$jsonOutput = (bool)$input->getOption('json');
$domain = $input->getArgument('domain');
try {
if (
strpos($domain, '.') === false ||
filter_var($domain, FILTER_VALIDATE_DOMAIN, ['flags' => FILTER_FLAG_HOSTNAME]) === false
) {
throw new InvalidArgumentException('argument <options=bold>domain</> contains an invalid domain name');
}

$info = $this->domainService->lookup($domain);
if ($info === null) {
throw new RuntimeException('Failed to load domain information');
}

if ($jsonOutput === true) {
$output->write(json_encode($info));

return Command::SUCCESS;
}

$now = $this->clock->now();

$lines = [];
$lines[] = sprintf('Domain: <options=bold>%s</>', $info->domainName);
$lines[] = 'Name Servers';
foreach ($info->nameServers as $nameServer) {
$lines[] = sprintf(' * <options=bold>%s</>', $nameServer);
}

$lines[] = sprintf(
'Creation date: <options=bold>%s</> (%s)',
$info->creationDate->format(DateTimeInterface::ATOM),
$this->humanReadableInterval($now->diff($info->creationDate))
);
$lines[] = sprintf(
'Expiration date: <options=bold>%s</> (%s)',
$info->expirationDate->format(DateTimeInterface::ATOM),
$this->humanReadableInterval($now->diff($info->expirationDate))
);
$lines[] = sprintf(
'Last update: <options=bold>%s</> (%s)',
$info->updatedDate->format(DateTimeInterface::ATOM),
$this->humanReadableInterval($now->diff($info->updatedDate))
);
$lines[] = 'EPP Flags';
foreach ($info->states as $state) {
$lines[] = sprintf(' * <options=bold>%s</>', $state);
}

$lines[] = sprintf('Registrar: <options=bold>%s</>', $info->registrar);
$lines[] = sprintf('DNSSEC: <options=bold>%s</>', $info->dnssec === null ? 'NO' : 'YES');

$output->writeln($lines);

return Command::SUCCESS;
} catch (Exception $exception) {
if ($jsonOutput === true) {
$out = ['error' => $exception->getMessage()];
if ($output->isDebug() === true) {
$out['trace'] = $exception->getTrace();
}

$output->write(json_encode($out));

return Command::FAILURE;
}

$output->writeln($exception->getMessage());
if ($output->isDebug() === true) {
$output->writeln($exception->getTraceAsString());
}

return Command::FAILURE;
}
}

public function __construct(
ClockInterface $clock,
DomainService $domainService
) {
parent::__construct();

$this->clock = $clock;
$this->domainService = $domainService;
}
}
13 changes: 12 additions & 1 deletion src/Console/DataObjects/Domain/DnsSec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

namespace Watchr\Console\DataObjects\Domain;

final class DnsSec {
use JsonSerializable;

final class DnsSec implements JsonSerializable {
public readonly int|null $keyTag;
public readonly int|null $algorithm;
public readonly int|null $digestType;
Expand All @@ -20,4 +22,13 @@ public function __construct(
$this->digestType = $digestType;
$this->digest = $digest;
}

public function jsonSerialize(): mixed {
return [
'keyTag' => $this->keyTag,
'algorithm' => $this->algorithm,
'digestType' => $this->digestType,
'digest' => $this->digest
];
}
}
18 changes: 17 additions & 1 deletion src/Console/DataObjects/Domain/DomainInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
namespace Watchr\Console\DataObjects\Domain;

use DateTimeInterface;
use JsonSerializable;

final class DomainInfo {
final class DomainInfo implements JsonSerializable {
public readonly string $domainName;
public readonly string $whoisServer;
/**
Expand Down Expand Up @@ -50,4 +51,19 @@ public function __construct(
$this->registrar = $registrar;
$this->dnssec = $dnssec;
}

public function jsonSerialize(): mixed {
return [
'domainName' => $this->domainName,
'whoisServer' => $this->whoisServer,
'nameServers' => $this->nameServers,
'creationDate' => $this->creationDate->format(DateTimeInterface::ATOM),
'expirationDate' => $this->expirationDate->format(DateTimeInterface::ATOM),
'updatedDate' => $this->updatedDate->format(DateTimeInterface::ATOM),
'states' => $this->states,
'owner' => $this->owner,
'registrar' => $this->registrar,
'dnssec' => $this->dnssec
];
}
}

0 comments on commit b99fe85

Please sign in to comment.