Added status codes returned by CLI commands

This commit is contained in:
Alejandro Celaya 2019-02-16 22:15:55 +01:00
parent 397b350cfc
commit eab5659163
18 changed files with 51 additions and 25 deletions

View File

@ -32,7 +32,7 @@ class DisableKeyCommand extends Command
->addArgument('apiKey', InputArgument::REQUIRED, 'The API key to disable');
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$apiKey = $input->getArgument('apiKey');
$io = new SymfonyStyle($input, $output);
@ -40,8 +40,10 @@ class DisableKeyCommand extends Command
try {
$this->apiKeyService->disable($apiKey);
$io->success(sprintf('API key "%s" properly disabled', $apiKey));
return 0;
} catch (InvalidArgumentException $e) {
$io->error(sprintf('API key "%s" does not exist.', $apiKey));
return -1;
}
}
}

View File

@ -38,11 +38,12 @@ class GenerateKeyCommand extends Command
);
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$expirationDate = $input->getOption('expirationDate');
$apiKey = $this->apiKeyService->create(isset($expirationDate) ? Chronos::parse($expirationDate) : null);
(new SymfonyStyle($input, $output))->success(sprintf('Generated API key: "%s"', $apiKey));
return 0;
}
}

View File

@ -44,7 +44,7 @@ class ListKeysCommand extends Command
);
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$enabledOnly = $input->getOption('enabledOnly');
@ -66,6 +66,7 @@ class ListKeysCommand extends Command
! $enabledOnly ? 'Is enabled' : null,
'Expiration date',
]), $rows);
return 0;
}
private function determineMessagePattern(ApiKey $apiKey): string

View File

@ -28,9 +28,10 @@ class GenerateCharsetCommand extends Command
->setHelp('<fg=red;options=bold>This command is deprecated. Better leave shlink generate the charset.</>');
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$charSet = str_shuffle(UrlShortenerOptions::DEFAULT_CHARS);
(new SymfonyStyle($input, $output))->success(sprintf('Character set: "%s"', $charSet));
return 0;
}
}

View File

@ -27,9 +27,10 @@ class GenerateSecretCommand extends Command
);
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$secret = $this->generateRandomString(32);
(new SymfonyStyle($input, $output))->success(sprintf('Secret key: "%s"', $secret));
return 0;
}
}

View File

@ -43,7 +43,7 @@ class DeleteShortUrlCommand extends Command
);
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
$shortCode = $input->getArgument('shortCode');
@ -51,14 +51,16 @@ class DeleteShortUrlCommand extends Command
try {
$this->runDelete($io, $shortCode, $ignoreThreshold);
return 0;
} catch (Exception\InvalidShortCodeException $e) {
$io->error(sprintf('Provided short code "%s" could not be found.', $shortCode));
return -1;
} catch (Exception\DeleteShortUrlException $e) {
$this->retry($io, $shortCode, $e);
return $this->retry($io, $shortCode, $e);
}
}
private function retry(SymfonyStyle $io, string $shortCode, Exception\DeleteShortUrlException $e): void
private function retry(SymfonyStyle $io, string $shortCode, Exception\DeleteShortUrlException $e): int
{
$warningMsg = sprintf(
'It was not possible to delete the short URL with short code "%s" because it has more than %s visits.',
@ -73,6 +75,8 @@ class DeleteShortUrlCommand extends Command
} else {
$io->warning('Short URL was not deleted.');
}
return $forceDelete ? 0 : 1;
}
private function runDelete(SymfonyStyle $io, string $shortCode, bool $ignoreThreshold): void

View File

@ -39,7 +39,7 @@ class GeneratePreviewCommand extends Command
);
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$page = 1;
do {
@ -52,6 +52,7 @@ class GeneratePreviewCommand extends Command
} while ($page <= $shortUrls->count());
(new SymfonyStyle($input, $output))->success('Finished processing all URLs');
return 0;
}
private function processUrl($url, OutputInterface $output): void

View File

@ -102,13 +102,13 @@ class GenerateShortUrlCommand extends Command
}
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
$longUrl = $input->getArgument('longUrl');
if (empty($longUrl)) {
$io->error('A URL was not provided!');
return;
return -1;
}
$explodeWithComma = curry('explode')(',');
@ -134,12 +134,15 @@ class GenerateShortUrlCommand extends Command
sprintf('Processed long URL: <info>%s</info>', $longUrl),
sprintf('Generated short URL: <info>%s</info>', $shortUrl),
]);
return 0;
} catch (InvalidUrlException $e) {
$io->error(sprintf('Provided URL "%s" is invalid. Try with a different one.', $longUrl));
return -1;
} catch (NonUniqueSlugException $e) {
$io->error(
sprintf('Provided slug "%s" is already in use by another URL. Try with a different one.', $customSlug)
);
return -1;
}
}

View File

@ -68,7 +68,7 @@ class GetVisitsCommand extends Command
}
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$shortCode = $input->getArgument('shortCode');
$startDate = $this->getDateOption($input, 'startDate');
@ -83,6 +83,7 @@ class GetVisitsCommand extends Command
return select_keys($rowData, ['referer', 'date', 'userAgent', 'country']);
}, $visits);
ShlinkTable::fromOutput($output)->render(['Referer', 'Date', 'User agent', 'Country'], $rows);
return 0;
}
private function getDateOption(InputInterface $input, $key)

View File

@ -74,7 +74,7 @@ class ListShortUrlsCommand extends Command
->addOption('showTags', null, InputOption::VALUE_NONE, 'Whether to display the tags or not');
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
$page = (int) $input->getOption('page');
@ -95,6 +95,7 @@ class ListShortUrlsCommand extends Command
$io->newLine();
$io->success('Short URLs properly listed');
return 0;
}
private function renderPage(

View File

@ -50,7 +50,7 @@ class ResolveUrlCommand extends Command
}
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
$shortCode = $input->getArgument('shortCode');
@ -58,10 +58,13 @@ class ResolveUrlCommand extends Command
try {
$url = $this->urlShortener->shortCodeToUrl($shortCode);
$output->writeln(sprintf('Long URL: <info>%s</info>', $url->getLongUrl()));
return 0;
} catch (InvalidShortCodeException $e) {
$io->error(sprintf('Provided short code "%s" has an invalid format.', $shortCode));
return -1;
} catch (EntityDoesNotExistException $e) {
$io->error(sprintf('Provided short code "%s" could not be found.', $shortCode));
return -1;
}
}
}

View File

@ -36,17 +36,18 @@ class CreateTagCommand extends Command
);
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
$tagNames = $input->getOption('name');
if (empty($tagNames)) {
$io->warning('You have to provide at least one tag name');
return;
return 1;
}
$this->tagService->createTags($tagNames);
$io->success('Tags properly created');
return 0;
}
}

View File

@ -36,17 +36,18 @@ class DeleteTagsCommand extends Command
);
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
$tagNames = $input->getOption('name');
if (empty($tagNames)) {
$io->warning('You have to provide at least one tag name');
return;
return 1;
}
$this->tagService->deleteTags($tagNames);
$io->success('Tags properly deleted');
return 0;
}
}

View File

@ -31,9 +31,10 @@ class ListTagsCommand extends Command
->setDescription('Lists existing tags.');
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
ShlinkTable::fromOutput($output)->render(['Name'], $this->getTagsRows());
return 0;
}
private function getTagsRows(): array

View File

@ -34,7 +34,7 @@ class RenameTagCommand extends Command
->addArgument('newName', InputArgument::REQUIRED, 'New name of the tag.');
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
$oldName = $input->getArgument('oldName');
@ -43,8 +43,10 @@ class RenameTagCommand extends Command
try {
$this->tagService->renameTag($oldName, $newName);
$io->success('Tag properly renamed.');
return 0;
} catch (EntityDoesNotExistException $e) {
$io->error(sprintf('A tag with name "%s" was not found', $oldName));
return -1;
}
}
}

View File

@ -48,7 +48,7 @@ class ProcessVisitsCommand extends Command
->setDescription('Processes visits where location is not set yet');
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$this->output = $output;
$io = new SymfonyStyle($input, $output);
@ -56,7 +56,7 @@ class ProcessVisitsCommand extends Command
$lock = $this->locker->createLock(self::NAME);
if (! $lock->acquire()) {
$io->warning(sprintf('There is already an instance of the "%s" command in execution', self::NAME));
return;
return 1;
}
try {
@ -70,6 +70,7 @@ class ProcessVisitsCommand extends Command
$io->success('Finished processing all IPs');
} finally {
$lock->release();
return 0;
}
}

View File

@ -35,7 +35,7 @@ class UpdateDbCommand extends Command
);
}
protected function execute(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): ?int
{
$io = new SymfonyStyle($input, $output);
$progressBar = new ProgressBar($output);
@ -51,6 +51,7 @@ class UpdateDbCommand extends Command
$io->writeln('');
$io->success('GeoLite2 database properly updated');
return 0;
} catch (RuntimeException $e) {
$progressBar->finish();
$io->writeln('');
@ -59,6 +60,7 @@ class UpdateDbCommand extends Command
if ($io->isVerbose()) {
$this->getApplication()->renderException($e, $output);
}
return -1;
}
}
}

View File

@ -25,7 +25,6 @@ class ShortUrlDataTransformer implements DataTransformerInterface
*/
public function transform($value): array
{
$dateCreated = $value->getDateCreated();
$longUrl = $value->getLongUrl();
$shortCode = $value->getShortCode();
@ -33,7 +32,7 @@ class ShortUrlDataTransformer implements DataTransformerInterface
'shortCode' => $shortCode,
'shortUrl' => $this->buildShortUrl($this->domainConfig, $shortCode),
'longUrl' => $longUrl,
'dateCreated' => $dateCreated !== null ? $dateCreated->toAtomString() : null,
'dateCreated' => $value->getDateCreated()->toAtomString(),
'visitsCount' => $value->getVisitsCount(),
'tags' => invoke($value->getTags(), '__toString'),