Config.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 OCA\Files_External\Command;
  8. use OC\Core\Command\Base;
  9. use OCA\Files_External\Lib\StorageConfig;
  10. use OCA\Files_External\NotFoundException;
  11. use OCA\Files_External\Service\GlobalStoragesService;
  12. use OCP\AppFramework\Http;
  13. use Symfony\Component\Console\Input\InputArgument;
  14. use Symfony\Component\Console\Input\InputInterface;
  15. use Symfony\Component\Console\Output\OutputInterface;
  16. class Config extends Base {
  17. public function __construct(
  18. protected GlobalStoragesService $globalService,
  19. ) {
  20. parent::__construct();
  21. }
  22. protected function configure(): void {
  23. $this
  24. ->setName('files_external:config')
  25. ->setDescription('Manage backend configuration for a mount')
  26. ->addArgument(
  27. 'mount_id',
  28. InputArgument::REQUIRED,
  29. 'The id of the mount to edit'
  30. )->addArgument(
  31. 'key',
  32. InputArgument::REQUIRED,
  33. 'key of the config option to set/get'
  34. )->addArgument(
  35. 'value',
  36. InputArgument::OPTIONAL,
  37. 'value to set the config option to, when no value is provided the existing value will be printed'
  38. );
  39. parent::configure();
  40. }
  41. protected function execute(InputInterface $input, OutputInterface $output): int {
  42. $mountId = $input->getArgument('mount_id');
  43. $key = $input->getArgument('key');
  44. try {
  45. $mount = $this->globalService->getStorage($mountId);
  46. } catch (NotFoundException $e) {
  47. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
  48. return Http::STATUS_NOT_FOUND;
  49. }
  50. $value = $input->getArgument('value');
  51. if ($value !== null) {
  52. $this->setOption($mount, $key, $value, $output);
  53. } else {
  54. $this->getOption($mount, $key, $output);
  55. }
  56. return self::SUCCESS;
  57. }
  58. /**
  59. * @param string $key
  60. */
  61. protected function getOption(StorageConfig $mount, $key, OutputInterface $output): void {
  62. if ($key === 'mountpoint' || $key === 'mount_point') {
  63. $value = $mount->getMountPoint();
  64. } else {
  65. $value = $mount->getBackendOption($key);
  66. }
  67. if (!is_string($value) && json_decode(json_encode($value)) === $value) { // show bools and objects correctly
  68. $value = json_encode($value);
  69. }
  70. $output->writeln((string)$value);
  71. }
  72. /**
  73. * @param string $key
  74. * @param string $value
  75. */
  76. protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output): void {
  77. $decoded = json_decode($value, true);
  78. if (!is_null($decoded) && json_encode($decoded) === $value) {
  79. $value = $decoded;
  80. }
  81. if ($key === 'mountpoint' || $key === 'mount_point') {
  82. $mount->setMountPoint($value);
  83. } else {
  84. $mount->setBackendOption($key, $value);
  85. }
  86. $this->globalService->updateStorage($mount);
  87. }
  88. }