-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new command "view:domain" for displaying formatted Domain Name …
…information
- Loading branch information
1 parent
47aa081
commit b99fe85
Showing
4 changed files
with
164 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters