Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add task id to list command output #82

Merged
merged 2 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion doc/cli-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ Help:
```

This command lists your currently defined schedule. It displays useful information
and potential issues. The `--detail` option gives even more detail.
and potential issues. The `--detail` option gives even more detail. Or add `--with-id`
to add the task ids to the table output.

It is advisable to run this command as a CI build step to ensure your schedule does
not make it to production with issues (has exit code `1` if there are issues).
Expand Down
2 changes: 1 addition & 1 deletion phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ parameters:

-
message: "#^Call to an undefined method Symfony\\\\Component\\\\Console\\\\Helper\\\\HelperInterface\\:\\:truncate\\(\\)\\.$#"
count: 1
count: 2
path: src/Command/ScheduleListCommand.php

-
Expand Down
23 changes: 19 additions & 4 deletions src/Command/ScheduleListCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Lorisleiva\CronTranslator\CronParsingException;
use Lorisleiva\CronTranslator\CronTranslator;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\TableCell;
use Symfony\Component\Console\Helper\TableSeparator;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\StringInput;
use Symfony\Component\Console\Output\OutputInterface;
Expand Down Expand Up @@ -58,6 +60,7 @@ protected function configure(): void
$this
->setDescription(self::getDefaultDescription()) // required for Symfony 4.4
->addOption('detail', null, null, 'Show detailed task list')
->addOption('with-ids', null, null, 'Show task ids in non-detailed list')
->setHelp(<<<EOF
Exit code 0: no issues.
Exit code 1: some issues.
Expand All @@ -78,7 +81,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int

$io->title(\sprintf('<info>%d</info> Scheduled Task%s Configured', \count($schedule->all()), \count($schedule->all()) > 1 ? 's' : ''));

$exit = $input->getOption('detail') ? $this->renderDetail($schedule, $io) : $this->renderTable($schedule, $io);
$exit = $input->getOption('detail') ? $this->renderDetail($schedule, $io) : $this->renderTable($schedule, $io, $input->getOption('with-ids'));

$this->renderExtenstions($io, 'Schedule', $schedule->getExtensions());

Expand Down Expand Up @@ -161,7 +164,7 @@ private function renderDefinitionList(SymfonyStyle $io, array $list): void
));
}

private function renderTable(Schedule $schedule, SymfonyStyle $io): int
private function renderTable(Schedule $schedule, SymfonyStyle $io, bool $withIds): int
{
/** @var array<\Throwable[]> $taskIssues */
$taskIssues = [];
Expand All @@ -173,16 +176,28 @@ private function renderTable(Schedule $schedule, SymfonyStyle $io): int

$rows[] = [
\count($issues) ? "<error>[!] {$task->getType()}</error>" : $task->getType(),
$this->getHelper('formatter')->truncate($task->getDescription(), 50),
$withIds ? $task->getId() : $this->getHelper('formatter')->truncate($task->getDescription(), 50),
\count($task->getExtensions()),
$this->renderFrequency($task),
$task->getNextRun()->format(\DATE_ATOM),
];
if ($withIds) {
$rows[] = [ // Additional row to prevent new line
null,
new TableCell($this->getHelper('formatter')->truncate($task->getDescription(), 120), ['colspan' => 4])
];
$rows[] = new TableSeparator();
}
}

if ($withIds) {
// Remove last table separator
array_pop($rows);
}

$taskIssues = \array_merge([], ...$taskIssues);

$io->table(['Type', 'Description', 'Extensions', 'Frequency', 'Next Run'], $rows);
$io->table(['Type', 'ID/Description', 'Extensions', 'Frequency', 'Next Run'], $rows);

if ($issueCount = \count($taskIssues)) {
$io->warning(\sprintf('%d task issue%s:', $issueCount, $issueCount > 1 ? 's' : ''));
Expand Down