@@ -17,225 +17,225 @@ |
||
| 17 | 17 | use Symfony\Component\Console\Output\OutputInterface; |
| 18 | 18 | |
| 19 | 19 | class Base extends Command implements CompletionAwareInterface { |
| 20 | - public const OUTPUT_FORMAT_PLAIN = 'plain'; |
|
| 21 | - public const OUTPUT_FORMAT_JSON = 'json'; |
|
| 22 | - public const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty'; |
|
| 23 | - |
|
| 24 | - protected string $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN; |
|
| 25 | - private bool $php_pcntl_signal = false; |
|
| 26 | - private bool $interrupted = false; |
|
| 27 | - |
|
| 28 | - protected function configure() { |
|
| 29 | - // Some of our commands do not extend this class; and some of those that do do not call parent::configure() |
|
| 30 | - $defaultHelp = 'More extensive and thorough documentation may be found at ' . \OCP\Server::get(\OCP\Defaults::class)->getDocBaseUrl() . PHP_EOL; |
|
| 31 | - $this |
|
| 32 | - ->setHelp($defaultHelp) |
|
| 33 | - ->addOption( |
|
| 34 | - 'output', |
|
| 35 | - null, |
|
| 36 | - InputOption::VALUE_OPTIONAL, |
|
| 37 | - 'Output format (plain, json or json_pretty, default is plain)', |
|
| 38 | - $this->defaultOutputFormat |
|
| 39 | - ) |
|
| 40 | - ; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, iterable $items, string $prefix = ' - '): void { |
|
| 44 | - switch ($input->getOption('output')) { |
|
| 45 | - case self::OUTPUT_FORMAT_JSON: |
|
| 46 | - $items = (is_array($items) ? $items : iterator_to_array($items)); |
|
| 47 | - $output->writeln(json_encode($items)); |
|
| 48 | - break; |
|
| 49 | - case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
| 50 | - $items = (is_array($items) ? $items : iterator_to_array($items)); |
|
| 51 | - $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); |
|
| 52 | - break; |
|
| 53 | - default: |
|
| 54 | - foreach ($items as $key => $item) { |
|
| 55 | - if (is_iterable($item)) { |
|
| 56 | - $output->writeln($prefix . $key . ':'); |
|
| 57 | - $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix); |
|
| 58 | - continue; |
|
| 59 | - } |
|
| 60 | - if (!is_int($key) || get_class($this) === ListCommand::class) { |
|
| 61 | - $value = $this->valueToString($item); |
|
| 62 | - if (!is_null($value)) { |
|
| 63 | - $output->writeln($prefix . $key . ': ' . $value); |
|
| 64 | - } else { |
|
| 65 | - $output->writeln($prefix . $key); |
|
| 66 | - } |
|
| 67 | - } else { |
|
| 68 | - $output->writeln($prefix . $this->valueToString($item)); |
|
| 69 | - } |
|
| 70 | - } |
|
| 71 | - break; |
|
| 72 | - } |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - protected function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void { |
|
| 76 | - switch ($input->getOption('output')) { |
|
| 77 | - case self::OUTPUT_FORMAT_JSON: |
|
| 78 | - $output->writeln(json_encode($items)); |
|
| 79 | - break; |
|
| 80 | - case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
| 81 | - $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); |
|
| 82 | - break; |
|
| 83 | - default: |
|
| 84 | - $table = new Table($output); |
|
| 85 | - $table->setRows($items); |
|
| 86 | - if (!empty($items) && is_string(array_key_first(reset($items)))) { |
|
| 87 | - $table->setHeaders(array_keys(reset($items))); |
|
| 88 | - } |
|
| 89 | - $table->render(); |
|
| 90 | - break; |
|
| 91 | - } |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - protected function writeStreamingTableInOutputFormat(InputInterface $input, OutputInterface $output, \Iterator $items, int $tableGroupSize): void { |
|
| 95 | - switch ($input->getOption('output')) { |
|
| 96 | - case self::OUTPUT_FORMAT_JSON: |
|
| 97 | - case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
| 98 | - $this->writeStreamingJsonArray($input, $output, $items); |
|
| 99 | - break; |
|
| 100 | - default: |
|
| 101 | - foreach ($this->chunkIterator($items, $tableGroupSize) as $chunk) { |
|
| 102 | - $this->writeTableInOutputFormat($input, $output, $chunk); |
|
| 103 | - } |
|
| 104 | - break; |
|
| 105 | - } |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - protected function writeStreamingJsonArray(InputInterface $input, OutputInterface $output, \Iterator $items): void { |
|
| 109 | - $first = true; |
|
| 110 | - $outputType = $input->getOption('output'); |
|
| 111 | - |
|
| 112 | - $output->writeln('['); |
|
| 113 | - foreach ($items as $item) { |
|
| 114 | - if (!$first) { |
|
| 115 | - $output->writeln(','); |
|
| 116 | - } |
|
| 117 | - if ($outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { |
|
| 118 | - $output->write(json_encode($item, JSON_PRETTY_PRINT)); |
|
| 119 | - } else { |
|
| 120 | - $output->write(json_encode($item)); |
|
| 121 | - } |
|
| 122 | - $first = false; |
|
| 123 | - } |
|
| 124 | - $output->writeln("\n]"); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - public function chunkIterator(\Iterator $iterator, int $count): \Iterator { |
|
| 128 | - $chunk = []; |
|
| 129 | - |
|
| 130 | - for ($i = 0; $iterator->valid(); $i++) { |
|
| 131 | - $chunk[] = $iterator->current(); |
|
| 132 | - $iterator->next(); |
|
| 133 | - if (count($chunk) == $count) { |
|
| 134 | - // Got a full chunk, yield and start a new one |
|
| 135 | - yield $chunk; |
|
| 136 | - $chunk = []; |
|
| 137 | - } |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - if (count($chunk)) { |
|
| 141 | - // Yield the last chunk even if incomplete |
|
| 142 | - yield $chunk; |
|
| 143 | - } |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - |
|
| 147 | - /** |
|
| 148 | - * @param mixed $item |
|
| 149 | - */ |
|
| 150 | - protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) { |
|
| 151 | - if (is_array($item)) { |
|
| 152 | - $this->writeArrayInOutputFormat($input, $output, $item, ''); |
|
| 153 | - return; |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - switch ($input->getOption('output')) { |
|
| 157 | - case self::OUTPUT_FORMAT_JSON: |
|
| 158 | - $output->writeln(json_encode($item)); |
|
| 159 | - break; |
|
| 160 | - case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
| 161 | - $output->writeln(json_encode($item, JSON_PRETTY_PRINT)); |
|
| 162 | - break; |
|
| 163 | - default: |
|
| 164 | - $output->writeln($this->valueToString($item, false)); |
|
| 165 | - break; |
|
| 166 | - } |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - protected function valueToString($value, bool $returnNull = true): ?string { |
|
| 170 | - if ($value === false) { |
|
| 171 | - return 'false'; |
|
| 172 | - } elseif ($value === true) { |
|
| 173 | - return 'true'; |
|
| 174 | - } elseif ($value === null) { |
|
| 175 | - return $returnNull ? null : 'null'; |
|
| 176 | - } if ($value instanceof \UnitEnum) { |
|
| 177 | - return $value->value; |
|
| 178 | - } else { |
|
| 179 | - return $value; |
|
| 180 | - } |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - /** |
|
| 184 | - * Throw InterruptedException when interrupted by user |
|
| 185 | - * |
|
| 186 | - * @throws InterruptedException |
|
| 187 | - */ |
|
| 188 | - protected function abortIfInterrupted() { |
|
| 189 | - if ($this->php_pcntl_signal === false) { |
|
| 190 | - return; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - pcntl_signal_dispatch(); |
|
| 194 | - |
|
| 195 | - if ($this->interrupted === true) { |
|
| 196 | - throw new InterruptedException('Command interrupted by user'); |
|
| 197 | - } |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Changes the status of the command to "interrupted" if ctrl-c has been pressed |
|
| 202 | - * |
|
| 203 | - * Gives a chance to the command to properly terminate what it's doing |
|
| 204 | - */ |
|
| 205 | - public function cancelOperation(): void { |
|
| 206 | - $this->interrupted = true; |
|
| 207 | - } |
|
| 208 | - |
|
| 209 | - public function run(InputInterface $input, OutputInterface $output): int { |
|
| 210 | - // check if the php pcntl_signal functions are accessible |
|
| 211 | - $this->php_pcntl_signal = function_exists('pcntl_signal'); |
|
| 212 | - if ($this->php_pcntl_signal) { |
|
| 213 | - // Collect interrupts and notify the running command |
|
| 214 | - pcntl_signal(SIGTERM, [$this, 'cancelOperation']); |
|
| 215 | - pcntl_signal(SIGINT, [$this, 'cancelOperation']); |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - return parent::run($input, $output); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * @param string $optionName |
|
| 223 | - * @param CompletionContext $context |
|
| 224 | - * @return string[] |
|
| 225 | - */ |
|
| 226 | - public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 227 | - if ($optionName === 'output') { |
|
| 228 | - return ['plain', 'json', 'json_pretty']; |
|
| 229 | - } |
|
| 230 | - return []; |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * @param string $argumentName |
|
| 235 | - * @param CompletionContext $context |
|
| 236 | - * @return string[] |
|
| 237 | - */ |
|
| 238 | - public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 239 | - return []; |
|
| 240 | - } |
|
| 20 | + public const OUTPUT_FORMAT_PLAIN = 'plain'; |
|
| 21 | + public const OUTPUT_FORMAT_JSON = 'json'; |
|
| 22 | + public const OUTPUT_FORMAT_JSON_PRETTY = 'json_pretty'; |
|
| 23 | + |
|
| 24 | + protected string $defaultOutputFormat = self::OUTPUT_FORMAT_PLAIN; |
|
| 25 | + private bool $php_pcntl_signal = false; |
|
| 26 | + private bool $interrupted = false; |
|
| 27 | + |
|
| 28 | + protected function configure() { |
|
| 29 | + // Some of our commands do not extend this class; and some of those that do do not call parent::configure() |
|
| 30 | + $defaultHelp = 'More extensive and thorough documentation may be found at ' . \OCP\Server::get(\OCP\Defaults::class)->getDocBaseUrl() . PHP_EOL; |
|
| 31 | + $this |
|
| 32 | + ->setHelp($defaultHelp) |
|
| 33 | + ->addOption( |
|
| 34 | + 'output', |
|
| 35 | + null, |
|
| 36 | + InputOption::VALUE_OPTIONAL, |
|
| 37 | + 'Output format (plain, json or json_pretty, default is plain)', |
|
| 38 | + $this->defaultOutputFormat |
|
| 39 | + ) |
|
| 40 | + ; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, iterable $items, string $prefix = ' - '): void { |
|
| 44 | + switch ($input->getOption('output')) { |
|
| 45 | + case self::OUTPUT_FORMAT_JSON: |
|
| 46 | + $items = (is_array($items) ? $items : iterator_to_array($items)); |
|
| 47 | + $output->writeln(json_encode($items)); |
|
| 48 | + break; |
|
| 49 | + case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
| 50 | + $items = (is_array($items) ? $items : iterator_to_array($items)); |
|
| 51 | + $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); |
|
| 52 | + break; |
|
| 53 | + default: |
|
| 54 | + foreach ($items as $key => $item) { |
|
| 55 | + if (is_iterable($item)) { |
|
| 56 | + $output->writeln($prefix . $key . ':'); |
|
| 57 | + $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix); |
|
| 58 | + continue; |
|
| 59 | + } |
|
| 60 | + if (!is_int($key) || get_class($this) === ListCommand::class) { |
|
| 61 | + $value = $this->valueToString($item); |
|
| 62 | + if (!is_null($value)) { |
|
| 63 | + $output->writeln($prefix . $key . ': ' . $value); |
|
| 64 | + } else { |
|
| 65 | + $output->writeln($prefix . $key); |
|
| 66 | + } |
|
| 67 | + } else { |
|
| 68 | + $output->writeln($prefix . $this->valueToString($item)); |
|
| 69 | + } |
|
| 70 | + } |
|
| 71 | + break; |
|
| 72 | + } |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + protected function writeTableInOutputFormat(InputInterface $input, OutputInterface $output, array $items): void { |
|
| 76 | + switch ($input->getOption('output')) { |
|
| 77 | + case self::OUTPUT_FORMAT_JSON: |
|
| 78 | + $output->writeln(json_encode($items)); |
|
| 79 | + break; |
|
| 80 | + case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
| 81 | + $output->writeln(json_encode($items, JSON_PRETTY_PRINT)); |
|
| 82 | + break; |
|
| 83 | + default: |
|
| 84 | + $table = new Table($output); |
|
| 85 | + $table->setRows($items); |
|
| 86 | + if (!empty($items) && is_string(array_key_first(reset($items)))) { |
|
| 87 | + $table->setHeaders(array_keys(reset($items))); |
|
| 88 | + } |
|
| 89 | + $table->render(); |
|
| 90 | + break; |
|
| 91 | + } |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + protected function writeStreamingTableInOutputFormat(InputInterface $input, OutputInterface $output, \Iterator $items, int $tableGroupSize): void { |
|
| 95 | + switch ($input->getOption('output')) { |
|
| 96 | + case self::OUTPUT_FORMAT_JSON: |
|
| 97 | + case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
| 98 | + $this->writeStreamingJsonArray($input, $output, $items); |
|
| 99 | + break; |
|
| 100 | + default: |
|
| 101 | + foreach ($this->chunkIterator($items, $tableGroupSize) as $chunk) { |
|
| 102 | + $this->writeTableInOutputFormat($input, $output, $chunk); |
|
| 103 | + } |
|
| 104 | + break; |
|
| 105 | + } |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + protected function writeStreamingJsonArray(InputInterface $input, OutputInterface $output, \Iterator $items): void { |
|
| 109 | + $first = true; |
|
| 110 | + $outputType = $input->getOption('output'); |
|
| 111 | + |
|
| 112 | + $output->writeln('['); |
|
| 113 | + foreach ($items as $item) { |
|
| 114 | + if (!$first) { |
|
| 115 | + $output->writeln(','); |
|
| 116 | + } |
|
| 117 | + if ($outputType === self::OUTPUT_FORMAT_JSON_PRETTY) { |
|
| 118 | + $output->write(json_encode($item, JSON_PRETTY_PRINT)); |
|
| 119 | + } else { |
|
| 120 | + $output->write(json_encode($item)); |
|
| 121 | + } |
|
| 122 | + $first = false; |
|
| 123 | + } |
|
| 124 | + $output->writeln("\n]"); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + public function chunkIterator(\Iterator $iterator, int $count): \Iterator { |
|
| 128 | + $chunk = []; |
|
| 129 | + |
|
| 130 | + for ($i = 0; $iterator->valid(); $i++) { |
|
| 131 | + $chunk[] = $iterator->current(); |
|
| 132 | + $iterator->next(); |
|
| 133 | + if (count($chunk) == $count) { |
|
| 134 | + // Got a full chunk, yield and start a new one |
|
| 135 | + yield $chunk; |
|
| 136 | + $chunk = []; |
|
| 137 | + } |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + if (count($chunk)) { |
|
| 141 | + // Yield the last chunk even if incomplete |
|
| 142 | + yield $chunk; |
|
| 143 | + } |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + |
|
| 147 | + /** |
|
| 148 | + * @param mixed $item |
|
| 149 | + */ |
|
| 150 | + protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) { |
|
| 151 | + if (is_array($item)) { |
|
| 152 | + $this->writeArrayInOutputFormat($input, $output, $item, ''); |
|
| 153 | + return; |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + switch ($input->getOption('output')) { |
|
| 157 | + case self::OUTPUT_FORMAT_JSON: |
|
| 158 | + $output->writeln(json_encode($item)); |
|
| 159 | + break; |
|
| 160 | + case self::OUTPUT_FORMAT_JSON_PRETTY: |
|
| 161 | + $output->writeln(json_encode($item, JSON_PRETTY_PRINT)); |
|
| 162 | + break; |
|
| 163 | + default: |
|
| 164 | + $output->writeln($this->valueToString($item, false)); |
|
| 165 | + break; |
|
| 166 | + } |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + protected function valueToString($value, bool $returnNull = true): ?string { |
|
| 170 | + if ($value === false) { |
|
| 171 | + return 'false'; |
|
| 172 | + } elseif ($value === true) { |
|
| 173 | + return 'true'; |
|
| 174 | + } elseif ($value === null) { |
|
| 175 | + return $returnNull ? null : 'null'; |
|
| 176 | + } if ($value instanceof \UnitEnum) { |
|
| 177 | + return $value->value; |
|
| 178 | + } else { |
|
| 179 | + return $value; |
|
| 180 | + } |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + /** |
|
| 184 | + * Throw InterruptedException when interrupted by user |
|
| 185 | + * |
|
| 186 | + * @throws InterruptedException |
|
| 187 | + */ |
|
| 188 | + protected function abortIfInterrupted() { |
|
| 189 | + if ($this->php_pcntl_signal === false) { |
|
| 190 | + return; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + pcntl_signal_dispatch(); |
|
| 194 | + |
|
| 195 | + if ($this->interrupted === true) { |
|
| 196 | + throw new InterruptedException('Command interrupted by user'); |
|
| 197 | + } |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Changes the status of the command to "interrupted" if ctrl-c has been pressed |
|
| 202 | + * |
|
| 203 | + * Gives a chance to the command to properly terminate what it's doing |
|
| 204 | + */ |
|
| 205 | + public function cancelOperation(): void { |
|
| 206 | + $this->interrupted = true; |
|
| 207 | + } |
|
| 208 | + |
|
| 209 | + public function run(InputInterface $input, OutputInterface $output): int { |
|
| 210 | + // check if the php pcntl_signal functions are accessible |
|
| 211 | + $this->php_pcntl_signal = function_exists('pcntl_signal'); |
|
| 212 | + if ($this->php_pcntl_signal) { |
|
| 213 | + // Collect interrupts and notify the running command |
|
| 214 | + pcntl_signal(SIGTERM, [$this, 'cancelOperation']); |
|
| 215 | + pcntl_signal(SIGINT, [$this, 'cancelOperation']); |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + return parent::run($input, $output); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * @param string $optionName |
|
| 223 | + * @param CompletionContext $context |
|
| 224 | + * @return string[] |
|
| 225 | + */ |
|
| 226 | + public function completeOptionValues($optionName, CompletionContext $context) { |
|
| 227 | + if ($optionName === 'output') { |
|
| 228 | + return ['plain', 'json', 'json_pretty']; |
|
| 229 | + } |
|
| 230 | + return []; |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * @param string $argumentName |
|
| 235 | + * @param CompletionContext $context |
|
| 236 | + * @return string[] |
|
| 237 | + */ |
|
| 238 | + public function completeArgumentValues($argumentName, CompletionContext $context) { |
|
| 239 | + return []; |
|
| 240 | + } |
|
| 241 | 241 | } |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | |
| 28 | 28 | protected function configure() { |
| 29 | 29 | // Some of our commands do not extend this class; and some of those that do do not call parent::configure() |
| 30 | - $defaultHelp = 'More extensive and thorough documentation may be found at ' . \OCP\Server::get(\OCP\Defaults::class)->getDocBaseUrl() . PHP_EOL; |
|
| 30 | + $defaultHelp = 'More extensive and thorough documentation may be found at '.\OCP\Server::get(\OCP\Defaults::class)->getDocBaseUrl().PHP_EOL; |
|
| 31 | 31 | $this |
| 32 | 32 | ->setHelp($defaultHelp) |
| 33 | 33 | ->addOption( |
@@ -53,19 +53,19 @@ discard block |
||
| 53 | 53 | default: |
| 54 | 54 | foreach ($items as $key => $item) { |
| 55 | 55 | if (is_iterable($item)) { |
| 56 | - $output->writeln($prefix . $key . ':'); |
|
| 57 | - $this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix); |
|
| 56 | + $output->writeln($prefix.$key.':'); |
|
| 57 | + $this->writeArrayInOutputFormat($input, $output, $item, ' '.$prefix); |
|
| 58 | 58 | continue; |
| 59 | 59 | } |
| 60 | 60 | if (!is_int($key) || get_class($this) === ListCommand::class) { |
| 61 | 61 | $value = $this->valueToString($item); |
| 62 | 62 | if (!is_null($value)) { |
| 63 | - $output->writeln($prefix . $key . ': ' . $value); |
|
| 63 | + $output->writeln($prefix.$key.': '.$value); |
|
| 64 | 64 | } else { |
| 65 | - $output->writeln($prefix . $key); |
|
| 65 | + $output->writeln($prefix.$key); |
|
| 66 | 66 | } |
| 67 | 67 | } else { |
| 68 | - $output->writeln($prefix . $this->valueToString($item)); |
|
| 68 | + $output->writeln($prefix.$this->valueToString($item)); |
|
| 69 | 69 | } |
| 70 | 70 | } |
| 71 | 71 | break; |