SetConfig.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. public function __construct(
  30. protected IConfig $config,
  31. ) {
  32. parent::__construct();
  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. }