You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

120 lines
3.6 KiB

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