2023-10-24 10:42:26 +02:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
|
|
|
|
|
*
|
|
|
|
|
* @author Côme Chilliet <come.chilliet@nextcloud.com>
|
|
|
|
|
*
|
|
|
|
|
* @license AGPL-3.0
|
|
|
|
|
*
|
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* 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, version 3,
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace OC\Core\Command;
|
|
|
|
|
|
|
|
|
|
use OCP\SetupCheck\ISetupCheckManager;
|
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
|
|
|
|
|
|
class SetupChecks extends Base {
|
|
|
|
|
public function __construct(
|
|
|
|
|
private ISetupCheckManager $setupCheckManager,
|
|
|
|
|
) {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function configure(): void {
|
|
|
|
|
parent::configure();
|
|
|
|
|
|
|
|
|
|
$this
|
|
|
|
|
->setName('setupchecks')
|
|
|
|
|
->setDescription('Run setup checks and output the results')
|
|
|
|
|
;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-04 16:27:18 +01:00
|
|
|
|
/**
|
2024-01-09 16:44:48 +01:00
|
|
|
|
* @TODO move this method to a common service used by notifications, activity and this command
|
2024-01-04 16:27:18 +01:00
|
|
|
|
* @throws \InvalidArgumentException if a parameter has no name or no type
|
|
|
|
|
*/
|
|
|
|
|
private function richToParsed(string $message, array $parameters): string {
|
|
|
|
|
$placeholders = [];
|
|
|
|
|
$replacements = [];
|
|
|
|
|
foreach ($parameters as $placeholder => $parameter) {
|
|
|
|
|
$placeholders[] = '{' . $placeholder . '}';
|
|
|
|
|
foreach (['name','type'] as $requiredField) {
|
|
|
|
|
if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) {
|
|
|
|
|
throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing");
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-11 11:26:24 +01:00
|
|
|
|
$replacements[] = match($parameter['type']) {
|
|
|
|
|
'user' => '@' . $parameter['name'],
|
|
|
|
|
'file' => $parameter['path'] ?? $parameter['name'],
|
|
|
|
|
default => $parameter['name'],
|
|
|
|
|
};
|
2024-01-04 16:27:18 +01:00
|
|
|
|
}
|
|
|
|
|
return str_replace($placeholders, $replacements, $message);
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-24 10:42:26 +02:00
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int {
|
|
|
|
|
$results = $this->setupCheckManager->runAll();
|
|
|
|
|
switch ($input->getOption('output')) {
|
|
|
|
|
case self::OUTPUT_FORMAT_JSON:
|
|
|
|
|
case self::OUTPUT_FORMAT_JSON_PRETTY:
|
|
|
|
|
$this->writeArrayInOutputFormat($input, $output, $results);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
foreach ($results as $category => $checks) {
|
|
|
|
|
$output->writeln("\t{$category}:");
|
2023-11-06 16:21:15 +01:00
|
|
|
|
foreach ($checks as $check) {
|
2023-10-24 10:42:26 +02:00
|
|
|
|
$styleTag = match ($check->getSeverity()) {
|
|
|
|
|
'success' => 'info',
|
|
|
|
|
'error' => 'error',
|
2023-11-06 15:28:51 +01:00
|
|
|
|
'warning' => 'comment',
|
|
|
|
|
default => null,
|
2023-10-24 10:42:26 +02:00
|
|
|
|
};
|
|
|
|
|
$emoji = match ($check->getSeverity()) {
|
|
|
|
|
'success' => '✓',
|
2023-10-26 16:10:49 +02:00
|
|
|
|
'error' => '✗',
|
2023-10-26 12:01:52 +02:00
|
|
|
|
'warning' => '⚠',
|
2023-10-24 10:42:26 +02:00
|
|
|
|
default => 'ℹ',
|
|
|
|
|
};
|
2023-10-24 16:55:45 +02:00
|
|
|
|
$verbosity = ($check->getSeverity() === 'error' ? OutputInterface::VERBOSITY_QUIET : OutputInterface::VERBOSITY_NORMAL);
|
|
|
|
|
$description = $check->getDescription();
|
2024-01-04 16:27:18 +01:00
|
|
|
|
$descriptionParameters = $check->getDescriptionParameters();
|
2024-01-09 16:27:23 +01:00
|
|
|
|
if ($description !== null && $descriptionParameters !== null) {
|
2024-01-04 16:27:18 +01:00
|
|
|
|
$description = $this->richToParsed($description, $descriptionParameters);
|
|
|
|
|
}
|
2023-10-24 10:42:26 +02:00
|
|
|
|
$output->writeln(
|
2023-11-06 15:28:51 +01:00
|
|
|
|
"\t\t".
|
|
|
|
|
($styleTag !== null ? "<{$styleTag}>" : '').
|
2023-10-24 10:42:26 +02:00
|
|
|
|
"{$emoji} ".
|
2023-11-06 16:21:15 +01:00
|
|
|
|
($check->getName() ?? $check::class).
|
2023-10-24 16:55:45 +02:00
|
|
|
|
($description !== null ? ': '.$description : '').
|
2023-11-06 15:28:51 +01:00
|
|
|
|
($styleTag !== null ? "</{$styleTag}>" : ''),
|
2023-10-24 16:55:45 +02:00
|
|
|
|
$verbosity
|
2023-10-24 10:42:26 +02:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
foreach ($results as $category => $checks) {
|
2023-11-06 16:21:15 +01:00
|
|
|
|
foreach ($checks as $check) {
|
2023-10-24 10:42:26 +02:00
|
|
|
|
if ($check->getSeverity() !== 'success') {
|
2023-10-24 16:55:45 +02:00
|
|
|
|
return self::FAILURE;
|
2023-10-24 10:42:26 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-24 16:55:45 +02:00
|
|
|
|
return self::SUCCESS;
|
2023-10-24 10:42:26 +02:00
|
|
|
|
}
|
|
|
|
|
}
|