Config.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Ardinis <Ardinis@users.noreply.github.com>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_External\Command;
  26. use OC\Core\Command\Base;
  27. use OCA\Files_External\Lib\StorageConfig;
  28. use OCA\Files_External\NotFoundException;
  29. use OCA\Files_External\Service\GlobalStoragesService;
  30. use Symfony\Component\Console\Input\InputArgument;
  31. use Symfony\Component\Console\Input\InputInterface;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. use Symfony\Component\HttpFoundation\Response;
  34. class Config extends Base {
  35. public function __construct(
  36. protected GlobalStoragesService $globalService,
  37. ) {
  38. parent::__construct();
  39. }
  40. protected function configure(): void {
  41. $this
  42. ->setName('files_external:config')
  43. ->setDescription('Manage backend configuration for a mount')
  44. ->addArgument(
  45. 'mount_id',
  46. InputArgument::REQUIRED,
  47. 'The id of the mount to edit'
  48. )->addArgument(
  49. 'key',
  50. InputArgument::REQUIRED,
  51. 'key of the config option to set/get'
  52. )->addArgument(
  53. 'value',
  54. InputArgument::OPTIONAL,
  55. 'value to set the config option to, when no value is provided the existing value will be printed'
  56. );
  57. parent::configure();
  58. }
  59. protected function execute(InputInterface $input, OutputInterface $output): int {
  60. $mountId = $input->getArgument('mount_id');
  61. $key = $input->getArgument('key');
  62. try {
  63. $mount = $this->globalService->getStorage($mountId);
  64. } catch (NotFoundException $e) {
  65. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
  66. return Response::HTTP_NOT_FOUND;
  67. }
  68. $value = $input->getArgument('value');
  69. if ($value !== null) {
  70. $this->setOption($mount, $key, $value, $output);
  71. } else {
  72. $this->getOption($mount, $key, $output);
  73. }
  74. return self::SUCCESS;
  75. }
  76. /**
  77. * @param string $key
  78. */
  79. protected function getOption(StorageConfig $mount, $key, OutputInterface $output): void {
  80. if ($key === 'mountpoint' || $key === 'mount_point') {
  81. $value = $mount->getMountPoint();
  82. } else {
  83. $value = $mount->getBackendOption($key);
  84. }
  85. if (!is_string($value) && json_decode(json_encode($value)) === $value) { // show bools and objects correctly
  86. $value = json_encode($value);
  87. }
  88. $output->writeln($value);
  89. }
  90. /**
  91. * @param string $key
  92. * @param string $value
  93. */
  94. protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output): void {
  95. $decoded = json_decode($value, true);
  96. if (!is_null($decoded) && json_encode($decoded) === $value) {
  97. $value = $decoded;
  98. }
  99. if ($key === 'mountpoint' || $key === 'mount_point') {
  100. $mount->setMountPoint($value);
  101. } else {
  102. $mount->setBackendOption($key, $value);
  103. }
  104. $this->globalService->updateStorage($mount);
  105. }
  106. }