diff options
author | Joas Schilling <nickvergessen@owncloud.com> | 2015-06-09 15:10:04 +0200 |
---|---|---|
committer | Joas Schilling <nickvergessen@owncloud.com> | 2015-07-07 11:18:25 +0200 |
commit | 130b5d8587b9330fd004eb015f60c03cd4e9ec6c (patch) | |
tree | 4bb36ab1d757bd1cb7fe2b4953e6f2c6d9ca9f8d /core | |
parent | 86952f88cbcd1a24930ad69c1c3d52f7f982b035 (diff) | |
download | nextcloud-server-130b5d8587b9330fd004eb015f60c03cd4e9ec6c.tar.gz nextcloud-server-130b5d8587b9330fd004eb015f60c03cd4e9ec6c.zip |
Allow reading from stdin
Diffstat (limited to 'core')
-rw-r--r-- | core/command/config/import.php | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/core/command/config/import.php b/core/command/config/import.php index 08c37570174..c53fd7255f9 100644 --- a/core/command/config/import.php +++ b/core/command/config/import.php @@ -47,7 +47,7 @@ class Import extends Command { ->setDescription('Import a list of configs') ->addArgument( 'file', - InputArgument::REQUIRED, + InputArgument::OPTIONAL, 'File with the json array to import' ) ; @@ -55,10 +55,17 @@ class Import extends Command { protected function execute(InputInterface $input, OutputInterface $output) { $importFile = $input->getArgument('file'); + if ($importFile !== null) { + $content = $this->getArrayFromFile($importFile); + } else { + $content = $this->getArrayFromStdin(); + } + try { - $configs = $this->getArrayFromFile($importFile); + $configs = $this->validateFileContent($content); } catch (\UnexpectedValueException $e) { $output->writeln('<error>' . $e->getMessage(). '</error>'); + return; } if (!empty($configs['system'])) { @@ -81,13 +88,37 @@ class Import extends Command { } /** + * Get the content from stdin ("config:import < file.json") + * + * @return string + */ + protected function getArrayFromStdin() { + // Read from stdin. stream_set_blocking is used to prevent blocking + // when nothing is passed via stdin. + stream_set_blocking(STDIN, 0); + $content = file_get_contents('php://stdin'); + stream_set_blocking(STDIN, 1); + return $content; + } + + /** + * Get the content of the specified file ("config:import file.json") + * * @param string $importFile + * @return string + */ + protected function getArrayFromFile($importFile) { + $content = file_get_contents($importFile); + return $content; + } + + /** + * @param string $content * @return array * @throws \UnexpectedValueException when the array is invalid */ - protected function getArrayFromFile($importFile) { - $helo = file_get_contents($importFile); - $decodedContent = json_decode($helo, true); + protected function validateFileContent($content) { + $decodedContent = json_decode($content, true); if (!is_array($decodedContent) || empty($decodedContent)) { throw new \UnexpectedValueException('The file must contain a valid json array'); } |