1
0

Base.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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, array $items, string $prefix = ' - '): void {
  35. switch ($input->getOption('output')) {
  36. case self::OUTPUT_FORMAT_JSON:
  37. $output->writeln(json_encode($items));
  38. break;
  39. case self::OUTPUT_FORMAT_JSON_PRETTY:
  40. $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
  41. break;
  42. default:
  43. foreach ($items as $key => $item) {
  44. if (is_array($item)) {
  45. $output->writeln($prefix . $key . ':');
  46. $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix);
  47. continue;
  48. }
  49. if (!is_int($key) || get_class($this) === ListCommand::class) {
  50. $value = $this->valueToString($item);
  51. if (!is_null($value)) {
  52. $output->writeln($prefix . $key . ': ' . $value);
  53. } else {
  54. $output->writeln($prefix . $key);
  55. }
  56. } else {
  57. $output->writeln($prefix . $this->valueToString($item));
  58. }
  59. }
  60. break;
  61. }
  62. }
  63. protected function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void {
  64. switch ($input->getOption('output')) {
  65. case self::OUTPUT_FORMAT_JSON:
  66. $output->writeln(json_encode($items));
  67. break;
  68. case self::OUTPUT_FORMAT_JSON_PRETTY:
  69. $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
  70. break;
  71. default:
  72. $table = new Table($output);
  73. $table->setRows($items);
  74. if (!empty($items) && is_string(array_key_first(reset($items)))) {
  75. $table->setHeaders(array_keys(reset($items)));
  76. }
  77. $table->render();
  78. break;
  79. }
  80. }
  81. /**
  82. * @param mixed $item
  83. */
  84. protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) {
  85. if (is_array($item)) {
  86. $this->writeArrayInOutputFormat($input, $output, $item, '');
  87. return;
  88. }
  89. switch ($input->getOption('output')) {
  90. case self::OUTPUT_FORMAT_JSON:
  91. $output->writeln(json_encode($item));
  92. break;
  93. case self::OUTPUT_FORMAT_JSON_PRETTY:
  94. $output->writeln(json_encode($item, JSON_PRETTY_PRINT));
  95. break;
  96. default:
  97. $output->writeln($this->valueToString($item, false));
  98. break;
  99. }
  100. }
  101. protected function valueToString($value, bool $returnNull = true): ?string {
  102. if ($value === false) {
  103. return 'false';
  104. } elseif ($value === true) {
  105. return 'true';
  106. } elseif ($value === null) {
  107. return $returnNull ? null : 'null';
  108. } else {
  109. return $value;
  110. }
  111. }
  112. /**
  113. * Throw InterruptedException when interrupted by user
  114. *
  115. * @throws InterruptedException
  116. */
  117. protected function abortIfInterrupted() {
  118. if ($this->php_pcntl_signal === false) {
  119. return;
  120. }
  121. pcntl_signal_dispatch();
  122. if ($this->interrupted === true) {
  123. throw new InterruptedException('Command interrupted by user');
  124. }
  125. }
  126. /**
  127. * Changes the status of the command to "interrupted" if ctrl-c has been pressed
  128. *
  129. * Gives a chance to the command to properly terminate what it's doing
  130. */
  131. public function cancelOperation(): void {
  132. $this->interrupted = true;
  133. }
  134. public function run(InputInterface $input, OutputInterface $output) {
  135. // check if the php pcntl_signal functions are accessible
  136. $this->php_pcntl_signal = function_exists('pcntl_signal');
  137. if ($this->php_pcntl_signal) {
  138. // Collect interrupts and notify the running command
  139. pcntl_signal(SIGTERM, [$this, 'cancelOperation']);
  140. pcntl_signal(SIGINT, [$this, 'cancelOperation']);
  141. }
  142. return parent::run($input, $output);
  143. }
  144. /**
  145. * @param string $optionName
  146. * @param CompletionContext $context
  147. * @return string[]
  148. */
  149. public function completeOptionValues($optionName, CompletionContext $context) {
  150. if ($optionName === 'output') {
  151. return ['plain', 'json', 'json_pretty'];
  152. }
  153. return [];
  154. }
  155. /**
  156. * @param string $argumentName
  157. * @param CompletionContext $context
  158. * @return string[]
  159. */
  160. public function completeArgumentValues($argumentName, CompletionContext $context) {
  161. return [];
  162. }
  163. }