Config.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. class Config extends Base {
  34. /**
  35. * @var GlobalStoragesService
  36. */
  37. protected $globalService;
  38. public function __construct(GlobalStoragesService $globalService) {
  39. parent::__construct();
  40. $this->globalService = $globalService;
  41. }
  42. protected function configure() {
  43. $this
  44. ->setName('files_external:config')
  45. ->setDescription('Manage backend configuration for a mount')
  46. ->addArgument(
  47. 'mount_id',
  48. InputArgument::REQUIRED,
  49. 'The id of the mount to edit'
  50. )->addArgument(
  51. 'key',
  52. InputArgument::REQUIRED,
  53. 'key of the config option to set/get'
  54. )->addArgument(
  55. 'value',
  56. InputArgument::OPTIONAL,
  57. 'value to set the config option to, when no value is provided the existing value will be printed'
  58. );
  59. parent::configure();
  60. }
  61. protected function execute(InputInterface $input, OutputInterface $output): int {
  62. $mountId = $input->getArgument('mount_id');
  63. $key = $input->getArgument('key');
  64. try {
  65. $mount = $this->globalService->getStorage($mountId);
  66. } catch (NotFoundException $e) {
  67. $output->writeln('<error>Mount with id "' . $mountId . ' not found, check "occ files_external:list" to get available mounts"</error>');
  68. return 404;
  69. }
  70. $value = $input->getArgument('value');
  71. if ($value !== null) {
  72. $this->setOption($mount, $key, $value, $output);
  73. } else {
  74. $this->getOption($mount, $key, $output);
  75. }
  76. return 0;
  77. }
  78. /**
  79. * @param StorageConfig $mount
  80. * @param string $key
  81. * @param OutputInterface $output
  82. */
  83. protected function getOption(StorageConfig $mount, $key, OutputInterface $output) {
  84. if ($key === 'mountpoint' || $key === 'mount_point') {
  85. $value = $mount->getMountPoint();
  86. } else {
  87. $value = $mount->getBackendOption($key);
  88. }
  89. if (!is_string($value) && json_decode(json_encode($value)) === $value) { // show bools and objects correctly
  90. $value = json_encode($value);
  91. }
  92. $output->writeln($value);
  93. }
  94. /**
  95. * @param StorageConfig $mount
  96. * @param string $key
  97. * @param string $value
  98. * @param OutputInterface $output
  99. */
  100. protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) {
  101. $decoded = json_decode($value, true);
  102. if (!is_null($decoded) && json_encode($decoded) === $value) {
  103. $value = $decoded;
  104. }
  105. if ($key === 'mountpoint' || $key === 'mount_point') {
  106. $mount->setMountPoint($value);
  107. } else {
  108. $mount->setBackendOption($key, $value);
  109. }
  110. $this->globalService->updateStorage($mount);
  111. }
  112. }