1
0

SetupChecks.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2023 Côme Chilliet <come.chilliet@nextcloud.com>
  5. *
  6. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Core\Command;
  24. use OCP\SetupCheck\ISetupCheckManager;
  25. use Symfony\Component\Console\Input\InputInterface;
  26. use Symfony\Component\Console\Output\OutputInterface;
  27. class SetupChecks extends Base {
  28. public function __construct(
  29. private ISetupCheckManager $setupCheckManager,
  30. ) {
  31. parent::__construct();
  32. }
  33. protected function configure(): void {
  34. parent::configure();
  35. $this
  36. ->setName('setupchecks')
  37. ->setDescription('Run setup checks and output the results')
  38. ;
  39. }
  40. /**
  41. * @TODO move this method to a common service used by notifications, activity and this command
  42. * @throws \InvalidArgumentException if a parameter has no name or no type
  43. */
  44. private function richToParsed(string $message, array $parameters): string {
  45. $placeholders = [];
  46. $replacements = [];
  47. foreach ($parameters as $placeholder => $parameter) {
  48. $placeholders[] = '{' . $placeholder . '}';
  49. foreach (['name','type'] as $requiredField) {
  50. if (!isset($parameter[$requiredField]) || !is_string($parameter[$requiredField])) {
  51. throw new \InvalidArgumentException("Invalid rich object, {$requiredField} field is missing");
  52. }
  53. }
  54. $replacements[] = match($parameter['type']) {
  55. 'user' => '@' . $parameter['name'],
  56. 'file' => $parameter['path'] ?? $parameter['name'],
  57. default => $parameter['name'],
  58. };
  59. }
  60. return str_replace($placeholders, $replacements, $message);
  61. }
  62. protected function execute(InputInterface $input, OutputInterface $output): int {
  63. $results = $this->setupCheckManager->runAll();
  64. switch ($input->getOption('output')) {
  65. case self::OUTPUT_FORMAT_JSON:
  66. case self::OUTPUT_FORMAT_JSON_PRETTY:
  67. $this->writeArrayInOutputFormat($input, $output, $results);
  68. break;
  69. default:
  70. foreach ($results as $category => $checks) {
  71. $output->writeln("\t{$category}:");
  72. foreach ($checks as $check) {
  73. $styleTag = match ($check->getSeverity()) {
  74. 'success' => 'info',
  75. 'error' => 'error',
  76. 'warning' => 'comment',
  77. default => null,
  78. };
  79. $emoji = match ($check->getSeverity()) {
  80. 'success' => '✓',
  81. 'error' => '✗',
  82. 'warning' => '⚠',
  83. default => 'ℹ',
  84. };
  85. $verbosity = ($check->getSeverity() === 'error' ? OutputInterface::VERBOSITY_QUIET : OutputInterface::VERBOSITY_NORMAL);
  86. $description = $check->getDescription();
  87. $descriptionParameters = $check->getDescriptionParameters();
  88. if ($description !== null && $descriptionParameters !== null) {
  89. $description = $this->richToParsed($description, $descriptionParameters);
  90. }
  91. $output->writeln(
  92. "\t\t".
  93. ($styleTag !== null ? "<{$styleTag}>" : '').
  94. "{$emoji} ".
  95. ($check->getName() ?? $check::class).
  96. ($description !== null ? ': '.$description : '').
  97. ($styleTag !== null ? "</{$styleTag}>" : ''),
  98. $verbosity
  99. );
  100. }
  101. }
  102. }
  103. foreach ($results as $category => $checks) {
  104. foreach ($checks as $check) {
  105. if ($check->getSeverity() !== 'success') {
  106. return self::FAILURE;
  107. }
  108. }
  109. }
  110. return self::SUCCESS;
  111. }
  112. }