SetupChecks.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. protected function execute(InputInterface $input, OutputInterface $output): int {
  41. $results = $this->setupCheckManager->runAll();
  42. switch ($input->getOption('output')) {
  43. case self::OUTPUT_FORMAT_JSON:
  44. case self::OUTPUT_FORMAT_JSON_PRETTY:
  45. $this->writeArrayInOutputFormat($input, $output, $results);
  46. break;
  47. default:
  48. foreach ($results as $category => $checks) {
  49. $output->writeln("\t{$category}:");
  50. foreach ($checks as $title => $check) {
  51. $styleTag = match ($check->getSeverity()) {
  52. 'success' => 'info',
  53. 'error' => 'error',
  54. 'warning' => 'comment',
  55. default => null,
  56. };
  57. $emoji = match ($check->getSeverity()) {
  58. 'success' => '✓',
  59. 'error' => '✗',
  60. 'warning' => '⚠',
  61. default => 'ℹ',
  62. };
  63. $verbosity = ($check->getSeverity() === 'error' ? OutputInterface::VERBOSITY_QUIET : OutputInterface::VERBOSITY_NORMAL);
  64. $description = $check->getDescription();
  65. $output->writeln(
  66. "\t\t".
  67. ($styleTag !== null ? "<{$styleTag}>" : '').
  68. "{$emoji} ".
  69. $title.
  70. ($description !== null ? ': '.$description : '').
  71. ($styleTag !== null ? "</{$styleTag}>" : ''),
  72. $verbosity
  73. );
  74. }
  75. }
  76. }
  77. foreach ($results as $category => $checks) {
  78. foreach ($checks as $title => $check) {
  79. if ($check->getSeverity() !== 'success') {
  80. return self::FAILURE;
  81. }
  82. }
  83. }
  84. return self::SUCCESS;
  85. }
  86. }