Base.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Core\Command;
  8. use OC\Core\Command\User\ListCommand;
  9. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  10. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Helper\Table;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Input\InputOption;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class Base extends Command implements CompletionAwareInterface {
  17. public const OUTPUT_FORMAT_PLAIN = 'plain';
  18. public const OUTPUT_FORMAT_JSON = 'json';
  19. public const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty';
  20. protected string $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN;
  21. private bool $php_pcntl_signal = false;
  22. private bool $interrupted = false;
  23. protected function configure() {
  24. $this
  25. ->addOption(
  26. 'output',
  27. null,
  28. InputOption::VALUE_OPTIONAL,
  29. 'Output format (plain, json or json_pretty, default is plain)',
  30. $this->defaultOutputFormat
  31. )
  32. ;
  33. }
  34. protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, iterable $items, string $prefix = ' - '): void {
  35. switch ($input->getOption('output')) {
  36. case self::OUTPUT_FORMAT_JSON:
  37. $items = (is_array($items) ? $items : iterator_to_array($items));
  38. $output->writeln(json_encode($items));
  39. break;
  40. case self::OUTPUT_FORMAT_JSON_PRETTY:
  41. $items = (is_array($items) ? $items : iterator_to_array($items));
  42. $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
  43. break;
  44. default:
  45. foreach ($items as $key => $item) {
  46. if (is_iterable($item)) {
  47. $output->writeln($prefix . $key . ':');
  48. $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix);
  49. continue;
  50. }
  51. if (!is_int($key) || get_class($this) === ListCommand::class) {
  52. $value = $this->valueToString($item);
  53. if (!is_null($value)) {
  54. $output->writeln($prefix . $key . ': ' . $value);
  55. } else {
  56. $output->writeln($prefix . $key);
  57. }
  58. } else {
  59. $output->writeln($prefix . $this->valueToString($item));
  60. }
  61. }
  62. break;
  63. }
  64. }
  65. protected function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void {
  66. switch ($input->getOption('output')) {
  67. case self::OUTPUT_FORMAT_JSON:
  68. $output->writeln(json_encode($items));
  69. break;
  70. case self::OUTPUT_FORMAT_JSON_PRETTY:
  71. $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
  72. break;
  73. default:
  74. $table = new Table($output);
  75. $table->setRows($items);
  76. if (!empty($items) && is_string(array_key_first(reset($items)))) {
  77. $table->setHeaders(array_keys(reset($items)));
  78. }
  79. $table->render();
  80. break;
  81. }
  82. }
  83. /**
  84. * @param mixed $item
  85. */
  86. protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) {
  87. if (is_array($item)) {
  88. $this->writeArrayInOutputFormat($input, $output, $item, '');
  89. return;
  90. }
  91. switch ($input->getOption('output')) {
  92. case self::OUTPUT_FORMAT_JSON:
  93. $output->writeln(json_encode($item));
  94. break;
  95. case self::OUTPUT_FORMAT_JSON_PRETTY:
  96. $output->writeln(json_encode($item, JSON_PRETTY_PRINT));
  97. break;
  98. default:
  99. $output->writeln($this->valueToString($item, false));
  100. break;
  101. }
  102. }
  103. protected function valueToString($value, bool $returnNull = true): ?string {
  104. if ($value === false) {
  105. return 'false';
  106. } elseif ($value === true) {
  107. return 'true';
  108. } elseif ($value === null) {
  109. return $returnNull ? null : 'null';
  110. } else {
  111. return $value;
  112. }
  113. }
  114. /**
  115. * Throw InterruptedException when interrupted by user
  116. *
  117. * @throws InterruptedException
  118. */
  119. protected function abortIfInterrupted() {
  120. if ($this->php_pcntl_signal === false) {
  121. return;
  122. }
  123. pcntl_signal_dispatch();
  124. if ($this->interrupted === true) {
  125. throw new InterruptedException('Command interrupted by user');
  126. }
  127. }
  128. /**
  129. * Changes the status of the command to "interrupted" if ctrl-c has been pressed
  130. *
  131. * Gives a chance to the command to properly terminate what it's doing
  132. */
  133. public function cancelOperation(): void {
  134. $this->interrupted = true;
  135. }
  136. public function run(InputInterface $input, OutputInterface $output) {
  137. // check if the php pcntl_signal functions are accessible
  138. $this->php_pcntl_signal = function_exists('pcntl_signal');
  139. if ($this->php_pcntl_signal) {
  140. // Collect interrupts and notify the running command
  141. pcntl_signal(SIGTERM, [$this, 'cancelOperation']);
  142. pcntl_signal(SIGINT, [$this, 'cancelOperation']);
  143. }
  144. return parent::run($input, $output);
  145. }
  146. /**
  147. * @param string $optionName
  148. * @param CompletionContext $context
  149. * @return string[]
  150. */
  151. public function completeOptionValues($optionName, CompletionContext $context) {
  152. if ($optionName === 'output') {
  153. return ['plain', 'json', 'json_pretty'];
  154. }
  155. return [];
  156. }
  157. /**
  158. * @param string $argumentName
  159. * @param CompletionContext $context
  160. * @return string[]
  161. */
  162. public function completeArgumentValues($argumentName, CompletionContext $context) {
  163. return [];
  164. }
  165. }