Config.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Files_External\Command;
  24. use OC\Core\Command\Base;
  25. use OCA\Files_External\Lib\StorageConfig;
  26. use OCA\Files_External\NotFoundException;
  27. use OCA\Files_External\Service\GlobalStoragesService;
  28. use Symfony\Component\Console\Input\InputArgument;
  29. use Symfony\Component\Console\Input\InputInterface;
  30. use Symfony\Component\Console\Output\OutputInterface;
  31. class Config extends Base {
  32. /**
  33. * @var GlobalStoragesService
  34. */
  35. protected $globalService;
  36. function __construct(GlobalStoragesService $globalService) {
  37. parent::__construct();
  38. $this->globalService = $globalService;
  39. }
  40. protected function configure() {
  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) {
  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 404;
  67. }
  68. $value = $input->getArgument('value');
  69. if ($value) {
  70. $this->setOption($mount, $key, $value, $output);
  71. } else {
  72. $this->getOption($mount, $key, $output);
  73. }
  74. }
  75. /**
  76. * @param StorageConfig $mount
  77. * @param string $key
  78. * @param OutputInterface $output
  79. */
  80. protected function getOption(StorageConfig $mount, $key, OutputInterface $output) {
  81. if ($key === 'mountpoint' || $key === 'mount_point') {
  82. $value = $mount->getMountPoint();
  83. } else {
  84. $value = $mount->getBackendOption($key);
  85. }
  86. if (!is_string($value)) { // show bools and objects correctly
  87. $value = json_encode($value);
  88. }
  89. $output->writeln($value);
  90. }
  91. /**
  92. * @param StorageConfig $mount
  93. * @param string $key
  94. * @param string $value
  95. * @param OutputInterface $output
  96. */
  97. protected function setOption(StorageConfig $mount, $key, $value, OutputInterface $output) {
  98. $decoded = json_decode($value, true);
  99. if (!is_null($decoded)) {
  100. $value = $decoded;
  101. }
  102. if ($key === 'mountpoint' || $key === 'mount_point') {
  103. $mount->setMountPoint($value);
  104. } else {
  105. $mount->setBackendOption($key, $value);
  106. }
  107. $this->globalService->updateStorage($mount);
  108. }
  109. }