Add --json to polls list and polls show
CI / Deploy / PHP 8.5 (push) Successful in 1m37s
CI / Deploy / PHP 8.4 (push) Successful in 1m39s
CI / Deploy / test (push) Successful in 2s
CI / Deploy / deploy (push) Skipped

Machine-readable output for scripts; never contains the admin token
unless --link is given.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Sonnet 5.5 committed 2026-11-16 13:59:38 +01:00
1 parent 664088e139
commit 3d6ade668f
6 files changed
+115 -2

No files matched your search

+38
View File
@@ -7,6 +7,7 @@ use App\Model\Entity\Poll;
use App\Model\Table\PollsTable;
use Cake\Command\Command;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOutput;
use Cake\I18n\DateTime;
use Cake\Routing\Router;
@@ -26,6 +27,43 @@ abstract class PollsCommand extends Command
return $polls;
}
/**
* Machine-readable form of a poll (for `--json`). Never contains the admin token.
*
* @param \App\Model\Entity\Poll $poll Poll
* @return array<string, mixed> Dates as ISO 8601 (server time zone), null where empty
*/
protected function pollData(Poll $poll): array
{
$date = fn(?DateTime $d): ?string => $d?->format(DATE_ATOM);
return [
'id' => $poll->id,
'title' => $poll->title,
'email' => $poll->email,
'joinCode' => $poll->join_code,
'joinUrl' => $this->joinLink($poll),
'created' => $date($poll->created),
'lastAdminAccess' => $date($poll->last_admin_access),
'expiresAt' => $date($poll->expiresAt()),
'expired' => $poll->isExpired(),
'reminderSentAt' => $date($poll->expiry_reminded_at),
];
}
/**
* Prints data as JSON only: nothing else on stdout, no markup interpretation (titles may contain "<").
*
* @param \Cake\Console\ConsoleIo $io Console
* @param mixed $data JSON serialisable data
* @return void
*/
protected function printJson(ConsoleIo $io, mixed $data): void
{
$io->setOutputAs(ConsoleOutput::RAW);
$io->out((string)json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
/**
* Finds a poll by numeric id or join code ("K7P2QX", "k7p-2qx").
*
+10
View File
@@ -37,6 +37,10 @@ class PollsListCommand extends PollsCommand
->addOption('email', [
'help' => __('Nur Umfragen, deren E-Mail-Adresse diesen Text enthält.'),
])
->addOption('json', [
'help' => __('Ausgabe als JSON statt als Tabelle (für Skripte).'),
'boolean' => true,
])
->addOption('sort', [
'help' => __('Sortierung (aufsteigend).'),
'default' => 'created',
@@ -75,6 +79,12 @@ class PollsListCommand extends PollsCommand
});
}
if ($args->getOption('json')) {
$this->printJson($io, array_map($this->pollData(...), $polls));
return static::CODE_SUCCESS;
}
if ($polls === []) {
$io->out(__('Keine Umfragen gefunden.'));
+19
View File
@@ -34,6 +34,10 @@ class PollsShowCommand extends PollsCommand
'help' => __('ID oder Beitrittscode der Umfrage.'),
'required' => true,
])
->addOption('json', [
'help' => __('Ausgabe als JSON statt als Text (für Skripte).'),
'boolean' => true,
])
->addOption('link', [
'help' => __('Auch den Admin-Link ausgeben (enthält das geheime Token!).'),
'boolean' => true,
@@ -55,6 +59,21 @@ class PollsShowCommand extends PollsCommand
}
$slides = $this->fetchTable('Slides')->find()->where(['poll_id' => $poll->id])->count();
if ($args->getOption('json')) {
$data = $this->pollData($poll) + ['slides' => $slides, 'adminUrl' => null];
if ($args->getOption('link')) {
$data['adminUrl'] = $this->adminLink($poll);
if ($data['adminUrl'] === null) {
$io->err(__('Der Admin-Link lässt sich nicht entschlüsseln (SECURITY_SALT geändert?).'));
return static::CODE_ERROR;
}
}
$this->printJson($io, $data);
return static::CODE_SUCCESS;
}
$io->out([
__('ID: {0}', $poll->id),
__('Titel: {0}', $poll->title),