Base.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Core\Command;
  27. use OC\Core\Command\User\ListCommand;
  28. use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
  29. use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
  30. use Symfony\Component\Console\Command\Command;
  31. use Symfony\Component\Console\Helper\Table;
  32. use Symfony\Component\Console\Input\InputInterface;
  33. use Symfony\Component\Console\Input\InputOption;
  34. use Symfony\Component\Console\Output\OutputInterface;
  35. class Base extends Command implements CompletionAwareInterface {
  36. public const OUTPUT_FORMAT_PLAIN = 'plain';
  37. public const OUTPUT_FORMAT_JSON = 'json';
  38. public const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty';
  39. protected string $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN;
  40. private bool $php_pcntl_signal = false;
  41. private bool $interrupted = false;
  42. protected function configure() {
  43. $this
  44. ->addOption(
  45. 'output',
  46. null,
  47. InputOption::VALUE_OPTIONAL,
  48. 'Output format (plain, json or json_pretty, default is plain)',
  49. $this->defaultOutputFormat
  50. )
  51. ;
  52. }
  53. protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, array $items, string $prefix = ' - '): void {
  54. switch ($input->getOption('output')) {
  55. case self::OUTPUT_FORMAT_JSON:
  56. $output->writeln(json_encode($items));
  57. break;
  58. case self::OUTPUT_FORMAT_JSON_PRETTY:
  59. $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
  60. break;
  61. default:
  62. foreach ($items as $key => $item) {
  63. if (is_array($item)) {
  64. $output->writeln($prefix . $key . ':');
  65. $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix);
  66. continue;
  67. }
  68. if (!is_int($key) || ListCommand::class === get_class($this)) {
  69. $value = $this->valueToString($item);
  70. if (!is_null($value)) {
  71. $output->writeln($prefix . $key . ': ' . $value);
  72. } else {
  73. $output->writeln($prefix . $key);
  74. }
  75. } else {
  76. $output->writeln($prefix . $this->valueToString($item));
  77. }
  78. }
  79. break;
  80. }
  81. }
  82. protected function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void {
  83. switch ($input->getOption('output')) {
  84. case self::OUTPUT_FORMAT_JSON:
  85. $output->writeln(json_encode($items));
  86. break;
  87. case self::OUTPUT_FORMAT_JSON_PRETTY:
  88. $output->writeln(json_encode($items, JSON_PRETTY_PRINT));
  89. break;
  90. default:
  91. $table = new Table($output);
  92. $table->setRows($items);
  93. if (!empty($items) && is_string(array_key_first(reset($items)))) {
  94. $table->setHeaders(array_keys(reset($items)));
  95. }
  96. $table->render();
  97. break;
  98. }
  99. }
  100. /**
  101. * @param mixed $item
  102. */
  103. protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) {
  104. if (is_array($item)) {
  105. $this->writeArrayInOutputFormat($input, $output, $item, '');
  106. return;
  107. }
  108. switch ($input->getOption('output')) {
  109. case self::OUTPUT_FORMAT_JSON:
  110. $output->writeln(json_encode($item));
  111. break;
  112. case self::OUTPUT_FORMAT_JSON_PRETTY:
  113. $output->writeln(json_encode($item, JSON_PRETTY_PRINT));
  114. break;
  115. default:
  116. $output->writeln($this->valueToString($item, false));
  117. break;
  118. }
  119. }
  120. protected function valueToString($value, bool $returnNull = true): ?string {
  121. if ($value === false) {
  122. return 'false';
  123. } elseif ($value === true) {
  124. return 'true';
  125. } elseif ($value === null) {
  126. return $returnNull ? null : 'null';
  127. } else {
  128. return $value;
  129. }
  130. }
  131. /**
  132. * Throw InterruptedException when interrupted by user
  133. *
  134. * @throws InterruptedException
  135. */
  136. protected function abortIfInterrupted() {
  137. if ($this->php_pcntl_signal === false) {
  138. return;
  139. }
  140. pcntl_signal_dispatch();
  141. if ($this->interrupted === true) {
  142. throw new InterruptedException('Command interrupted by user');
  143. }
  144. }
  145. /**
  146. * Changes the status of the command to "interrupted" if ctrl-c has been pressed
  147. *
  148. * Gives a chance to the command to properly terminate what it's doing
  149. */
  150. protected function cancelOperation() {
  151. $this->interrupted = true;
  152. }
  153. public function run(InputInterface $input, OutputInterface $output) {
  154. // check if the php pcntl_signal functions are accessible
  155. $this->php_pcntl_signal = function_exists('pcntl_signal');
  156. if ($this->php_pcntl_signal) {
  157. // Collect interrupts and notify the running command
  158. pcntl_signal(SIGTERM, [$this, 'cancelOperation']);
  159. pcntl_signal(SIGINT, [$this, 'cancelOperation']);
  160. }
  161. return parent::run($input, $output);
  162. }
  163. /**
  164. * @param string $optionName
  165. * @param CompletionContext $context
  166. * @return string[]
  167. */
  168. public function completeOptionValues($optionName, CompletionContext $context) {
  169. if ($optionName === 'output') {
  170. return ['plain', 'json', 'json_pretty'];
  171. }
  172. return [];
  173. }
  174. /**
  175. * @param string $argumentName
  176. * @param CompletionContext $context
  177. * @return string[]
  178. */
  179. public function completeArgumentValues($argumentName, CompletionContext $context) {
  180. return [];
  181. }
  182. }