Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Add an OCP API for console commands
Signed-off-by: Christoph Wurst <[email protected]>
  • Loading branch information
ChristophWurst committed Jun 9, 2022
commit 1479c5641ef18bb3b383ab1382972d4879a40e45
7 changes: 7 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,9 @@
'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\\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
7 changes: 7 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,9 @@ 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\\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
54 changes: 54 additions & 0 deletions lib/private/Command/SymfonyCommandAdapter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

/*
* @copyright 2022 Christoph Wurst <[email protected]>
*
* @author 2022 Christoph Wurst <[email protected]>
*
* @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) {
parent::__construct($command->getFullyQualifiedName());
$this->command = $command;
}

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

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

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)
)
}

}
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 <[email protected]>
*
* @author 2022 Christoph Wurst <[email protected]>
*
* @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);
}

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

declare(strict_types=1);

/*
* @copyright 2022 Christoph Wurst <[email protected]>
*
* @author 2022 Christoph Wurst <[email protected]>
*
* @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\IOutput;
use Symfony\Component\Console\Output\OutputInterface;

class SymfonyOutputAdapter implements IOutput {

private OutputInterface $output;

public function __construct(OutputInterface $output) {
$this->output = $output;
}

public function formatError(string $message): string {
return '<error>' . $message . '</error>';
}

public function formatInfo(string $message): string {
return '<info>' . $message . '</info>';
}

public function writeLn(string $message): void {
$this->output->writeln($message);
}
}
69 changes: 69 additions & 0 deletions lib/public/Command/Command.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

declare(strict_types=1);

/*
* @copyright 2022 Christoph Wurst <[email protected]>
*
* @author 2022 Christoph Wurst <[email protected]>
*
* @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 OCP\Command;

use function implode;

abstract class Command {

public const EXIT_CODE_SUCCESS = 0;
public const EXIT_CODE_ERROR = 1;

private string $appId;

public function __construct(string $appId) {
$this->appId = $appId;
}

public function getNamespace(): ?string {
return $this->appId;
}

public abstract function getName(): string;

public abstract function getDescription(): string;

public function getFullyQualifiedName(): string {
if ($this->getNamespace() === null) {
return $this->getName();
}

return implode(':', [
$this->getNamespace(),
$this->getName()
]);
}

public function isEnabled(): bool {
return true;
}

public function configure(IConfiguration $config): void {
}

public abstract function execute(IInput $input, IOutput $output);

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

declare(strict_types=1);

/*
* @copyright 2022 Christoph Wurst <[email protected]>
*
* @author 2022 Christoph Wurst <[email protected]>
*
* @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 OCP\Command;

interface IConfiguration {

public function addArgument(): void;

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

declare(strict_types=1);

/*
* @copyright 2022 Christoph Wurst <[email protected]>
*
* @author 2022 Christoph Wurst <[email protected]>
*
* @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 OCP\Command;

interface IInput {

public function isInteractive(): bool;

/**
* @return mixed
*/
public function getArgument(string $name);

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

declare(strict_types=1);

/*
* @copyright 2022 Christoph Wurst <[email protected]>
*
* @author 2022 Christoph Wurst <[email protected]>
*
* @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 OCP\Command;

interface IOutput {

public function formatError(string $message): string;

public function formatInfo(string $message): string;

public function writeLn(string $message): void;

}