SetConfig.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. *
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Core\Command\Config\App;
  23. use OCP\IConfig;
  24. use Symfony\Component\Console\Input\InputArgument;
  25. use Symfony\Component\Console\Input\InputInterface;
  26. use Symfony\Component\Console\Input\InputOption;
  27. use Symfony\Component\Console\Output\OutputInterface;
  28. class SetConfig extends Base {
  29. protected IConfig $config;
  30. public function __construct(IConfig $config) {
  31. parent::__construct();
  32. $this->config = $config;
  33. }
  34. protected function configure() {
  35. parent::configure();
  36. $this
  37. ->setName('config:app:set')
  38. ->setDescription('Set an app config value')
  39. ->addArgument(
  40. 'app',
  41. InputArgument::REQUIRED,
  42. 'Name of the app'
  43. )
  44. ->addArgument(
  45. 'name',
  46. InputArgument::REQUIRED,
  47. 'Name of the config to set'
  48. )
  49. ->addOption(
  50. 'value',
  51. null,
  52. InputOption::VALUE_REQUIRED,
  53. 'The new value of the config'
  54. )
  55. ->addOption(
  56. 'update-only',
  57. null,
  58. InputOption::VALUE_NONE,
  59. 'Only updates the value, if it is not set before, it is not being added'
  60. )
  61. ;
  62. }
  63. protected function execute(InputInterface $input, OutputInterface $output): int {
  64. $appName = $input->getArgument('app');
  65. $configName = $input->getArgument('name');
  66. if (!in_array($configName, $this->config->getAppKeys($appName)) && $input->hasParameterOption('--update-only')) {
  67. $output->writeln('<comment>Config value ' . $configName . ' for app ' . $appName . ' not updated, as it has not been set before.</comment>');
  68. return 1;
  69. }
  70. $configValue = $input->getOption('value');
  71. $this->config->setAppValue($appName, $configName, $configValue);
  72. $output->writeln('<info>Config value ' . $configName . ' for app ' . $appName . ' set to ' . $configValue . '</info>');
  73. return 0;
  74. }
  75. }