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 an OCP API for console commands #32780

Closed
wants to merge 3 commits into from
Closed
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
1 change: 0 additions & 1 deletion apps/dav/appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
<command>OCA\DAV\Command\CreateCalendar</command>
<command>OCA\DAV\Command\DeleteCalendar</command>
<command>OCA\DAV\Command\MoveCalendar</command>
<command>OCA\DAV\Command\ListCalendars</command>
<command>OCA\DAV\Command\RetentionCleanupCommand</command>
<command>OCA\DAV\Command\SendEventReminders</command>
<command>OCA\DAV\Command\SyncBirthdayCalendar</command>
Expand Down
6 changes: 6 additions & 0 deletions apps/dav/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use OCA\DAV\CardDAV\ContactsManager;
use OCA\DAV\CardDAV\PhotoCache;
use OCA\DAV\CardDAV\SyncService;
use OCA\DAV\Command\ListCalendars;
use OCA\DAV\Events\AddressBookCreatedEvent;
use OCA\DAV\Events\AddressBookDeletedEvent;
use OCA\DAV\Events\AddressBookShareUpdatedEvent;
Expand Down Expand Up @@ -122,6 +123,11 @@ public function register(IRegistrationContext $context): void {
*/
$context->registerCapability(Capabilities::class);

/**
* Register commands
*/
$context->registerCommand(ListCalendars::class);

/*
* Register Search Providers
*/
Expand Down
50 changes: 28 additions & 22 deletions apps/dav/lib/Command/ListCalendars.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2018, Georg Ehrke
*
Expand All @@ -25,14 +28,14 @@
*/
namespace OCA\DAV\Command;

use OCA\DAV\AppInfo\Application;
use OCA\DAV\CalDAV\BirthdayService;
use OCA\DAV\CalDAV\CalDavBackend;
use OCP\Command\Command;
use OCP\Command\IConfiguration;
use OCP\Command\IInput;
use OCP\Command\IOutput;
use OCP\IUserManager;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class ListCalendars extends Command {

Expand All @@ -42,26 +45,29 @@ class ListCalendars extends Command {
/** @var CalDavBackend */
private $caldav;

/**
* @param IUserManager $userManager
* @param CalDavBackend $caldav
*/
public function __construct(IUserManager $userManager, CalDavBackend $caldav) {
parent::__construct();
parent::__construct(Application::APP_ID);
$this->userManager = $userManager;
$this->caldav = $caldav;
}

protected function configure() {
$this
->setName('dav:list-calendars')
->setDescription('List all calendars of a user')
->addArgument('uid',
InputArgument::REQUIRED,
'User for whom all calendars will be listed');
public function getName(): string {
return 'list-calendars';
}

public function getDescription(): string {
return 'List all calendars of a user';
}

public function configure(IConfiguration $configuration): void {

Check failure

Code scanning / Psalm

ParamNameMismatch

Argument 1 of OCA\DAV\Command\ListCalendars::configure has wrong name $configuration, expecting $config as defined by OCP\Command\Command::configure
$configuration->addArgument(
'uid',
true,
'User for whom all calendars will be listed'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int {
public function execute(IInput $input, IOutput $output): int {
$user = $input->getArgument('uid');
if (!$this->userManager->userExists($user)) {
throw new \InvalidArgumentException("User <$user> is unknown.");
Expand Down Expand Up @@ -92,11 +98,11 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

if (count($calendarTableData) > 0) {
$table = new Table($output);
$table->setHeaders(['uri', 'displayname', 'owner\'s userid', 'owner\'s displayname', 'writable'])
->setRows($calendarTableData);
//$table = new Table($output);
//$table->setHeaders(['uri', 'displayname', 'owner\'s userid', 'owner\'s displayname', 'writable'])
// ->setRows($calendarTableData);

$table->render();
//$table->render();
} else {
$output->writeln("<info>User <$user> has no calendars</info>");
}
Expand Down
8 changes: 8 additions & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,12 @@
'OCP\\Collaboration\\Resources\\IResource' => $baseDir . '/lib/public/Collaboration/Resources/IResource.php',
'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => $baseDir . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php',
'OCP\\Collaboration\\Resources\\ResourceException' => $baseDir . '/lib/public/Collaboration/Resources/ResourceException.php',
'OCP\\Command\\Command' => $baseDir . '/lib/public/Command/Command.php',
'OCP\\Command\\IBus' => $baseDir . '/lib/public/Command/IBus.php',
'OCP\\Command\\ICommand' => $baseDir . '/lib/public/Command/ICommand.php',
'OCP\\Command\\IConfiguration' => $baseDir . '/lib/public/Command/IConfiguration.php',
'OCP\\Command\\IInput' => $baseDir . '/lib/public/Command/IInput.php',
'OCP\\Command\\IOutput' => $baseDir . '/lib/public/Command/IOutput.php',
'OCP\\Comments\\CommentsEntityEvent' => $baseDir . '/lib/public/Comments/CommentsEntityEvent.php',
'OCP\\Comments\\CommentsEvent' => $baseDir . '/lib/public/Comments/CommentsEvent.php',
'OCP\\Comments\\IComment' => $baseDir . '/lib/public/Comments/IComment.php',
Expand Down Expand Up @@ -827,6 +831,10 @@
'OC\\Command\\CronBus' => $baseDir . '/lib/private/Command/CronBus.php',
'OC\\Command\\FileAccess' => $baseDir . '/lib/private/Command/FileAccess.php',
'OC\\Command\\QueueBus' => $baseDir . '/lib/private/Command/QueueBus.php',
'OC\\Command\\SymfonyCommandAdapter' => $baseDir . '/lib/private/Command/SymfonyCommandAdapter.php',
'OC\\Command\\SymfonyConfigurationAdapter' => $baseDir . '/lib/private/Command/SymfonyConfigurationAdapter.php',
'OC\\Command\\SymfonyInputAdapter' => $baseDir . '/lib/private/Command/SymfonyInputAdapter.php',
'OC\\Command\\SymfonyOutputAdapter' => $baseDir . '/lib/private/Command/SymfonyOutputAdapter.php',
'OC\\Comments\\Comment' => $baseDir . '/lib/private/Comments/Comment.php',
'OC\\Comments\\Manager' => $baseDir . '/lib/private/Comments/Manager.php',
'OC\\Comments\\ManagerFactory' => $baseDir . '/lib/private/Comments/ManagerFactory.php',
Expand Down
8 changes: 8 additions & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,12 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OCP\\Collaboration\\Resources\\IResource' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/IResource.php',
'OCP\\Collaboration\\Resources\\LoadAdditionalScriptsEvent' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/LoadAdditionalScriptsEvent.php',
'OCP\\Collaboration\\Resources\\ResourceException' => __DIR__ . '/../../..' . '/lib/public/Collaboration/Resources/ResourceException.php',
'OCP\\Command\\Command' => __DIR__ . '/../../..' . '/lib/public/Command/Command.php',
'OCP\\Command\\IBus' => __DIR__ . '/../../..' . '/lib/public/Command/IBus.php',
'OCP\\Command\\ICommand' => __DIR__ . '/../../..' . '/lib/public/Command/ICommand.php',
'OCP\\Command\\IConfiguration' => __DIR__ . '/../../..' . '/lib/public/Command/IConfiguration.php',
'OCP\\Command\\IInput' => __DIR__ . '/../../..' . '/lib/public/Command/IInput.php',
'OCP\\Command\\IOutput' => __DIR__ . '/../../..' . '/lib/public/Command/IOutput.php',
'OCP\\Comments\\CommentsEntityEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/CommentsEntityEvent.php',
'OCP\\Comments\\CommentsEvent' => __DIR__ . '/../../..' . '/lib/public/Comments/CommentsEvent.php',
'OCP\\Comments\\IComment' => __DIR__ . '/../../..' . '/lib/public/Comments/IComment.php',
Expand Down Expand Up @@ -860,6 +864,10 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Command\\CronBus' => __DIR__ . '/../../..' . '/lib/private/Command/CronBus.php',
'OC\\Command\\FileAccess' => __DIR__ . '/../../..' . '/lib/private/Command/FileAccess.php',
'OC\\Command\\QueueBus' => __DIR__ . '/../../..' . '/lib/private/Command/QueueBus.php',
'OC\\Command\\SymfonyCommandAdapter' => __DIR__ . '/../../..' . '/lib/private/Command/SymfonyCommandAdapter.php',
'OC\\Command\\SymfonyConfigurationAdapter' => __DIR__ . '/../../..' . '/lib/private/Command/SymfonyConfigurationAdapter.php',
'OC\\Command\\SymfonyInputAdapter' => __DIR__ . '/../../..' . '/lib/private/Command/SymfonyInputAdapter.php',
'OC\\Command\\SymfonyOutputAdapter' => __DIR__ . '/../../..' . '/lib/private/Command/SymfonyOutputAdapter.php',
'OC\\Comments\\Comment' => __DIR__ . '/../../..' . '/lib/private/Comments/Comment.php',
'OC\\Comments\\Manager' => __DIR__ . '/../../..' . '/lib/private/Comments/Manager.php',
'OC\\Comments\\ManagerFactory' => __DIR__ . '/../../..' . '/lib/private/Comments/ManagerFactory.php',
Expand Down
25 changes: 25 additions & 0 deletions lib/private/AppFramework/Bootstrap/RegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Closure;
use OCP\Calendar\Resource\IBackend as IResourceBackend;
use OCP\Calendar\Room\IBackend as IRoomBackend;
use OCP\Command\Command;
use OCP\Talk\ITalkBackend;
use RuntimeException;
use function array_shift;
Expand Down Expand Up @@ -61,6 +62,9 @@ class RegistrationContext {
/** @var ServiceRegistration<ICapability>[] */
private $capabilities = [];

/** @var ServiceRegistration<Command>[] */
private $commands = [];

/** @var ServiceRegistration<IReporter>[] */
private $crashReporters = [];

Expand Down Expand Up @@ -151,6 +155,13 @@ public function registerCapability(string $capability): void {
);
}

public function registerCommand(string $command): void {
$this->context->registerCommand(
$this->appId,
$command
);
}

public function registerCrashReporter(string $reporterClass): void {
$this->context->registerCrashReporter(
$this->appId,
Expand Down Expand Up @@ -314,6 +325,13 @@ public function registerCapability(string $appId, string $capability): void {
$this->capabilities[] = new ServiceRegistration($appId, $capability);
}

/**
* @psalm-param class-string<ICapability> $capability
*/
public function registerCommand(string $appId, string $command): void {
$this->commands[] = new ServiceRegistration($appId, $command);
}

/**
* @psalm-param class-string<IReporter> $capability
*/
Expand Down Expand Up @@ -612,6 +630,13 @@ public function delegateMiddlewareRegistrations(array $apps): void {
}
}

/**
* @return ServiceRegistration<Command>[]
*/
public function getCommands(): array {
return $this->commands;
}

/**
* @return ServiceRegistration<IProvider>[]
*/
Expand Down
59 changes: 59 additions & 0 deletions lib/private/Command/SymfonyCommandAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/*
* @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OC\Command;

use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SymfonyCommandAdapter extends Command {

private \OCP\Command\Command $command;

public function __construct(\OCP\Command\Command $command) {
$this->command = $command;

parent::__construct($command->getFullyQualifiedName());
}

protected function configure() {
parent::configure();

$this->setDescription($this->command->getDescription());

$this->command->configure(
new SymfonyConfigurationAdapter($this)
);
}

protected function execute(InputInterface $input, OutputInterface $output) {

Check failure

Code scanning / Psalm

InvalidReturnType

No return statements were found for method OC\Command\SymfonyCommandAdapter::execute but return type 'int' was expected
$this->command->execute(
new SymfonyInputAdapter($input),
new SymfonyOutputAdapter($output)
);
}

}
52 changes: 52 additions & 0 deletions lib/private/Command/SymfonyConfigurationAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

/*
* @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OC\Command;

use OCP\Command\IConfiguration;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;

class SymfonyConfigurationAdapter implements IConfiguration {

private Command $command;

public function __construct(Command $command) {
$this->command = $command;
}

public function addArgument(string $name,
bool $required = false,
string $description = '',
$default = null): void {
$this->command->addArgument(
$name,
$required ? InputArgument::REQUIRED : InputArgument::OPTIONAL,
$description,
$default
);
}

}
47 changes: 47 additions & 0 deletions lib/private/Command/SymfonyInputAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

/*
* @copyright 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author 2022 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OC\Command;

use OCP\Command\IInput;
use Symfony\Component\Console\Input\InputInterface;

class SymfonyInputAdapter implements IInput {

private InputInterface $input;

public function __construct(InputInterface $input) {
$this->input = $input;
}

public function isInteractive(): bool {
return $this->input->isInteractive();
}

public function getArgument(string $name) {
return $this->input->getArgument($name);
}

}
Loading