Config.php 3.6 KB

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