Skip to content

Commit

Permalink
Merge pull request #16368 from owncloud/occ-config-commands
Browse files Browse the repository at this point in the history
Add "occ config:*" to manage config values
  • Loading branch information
Vincent Petry committed Jul 7, 2015
2 parents 87e10a7 + 85f0125 commit d3e26a3
Show file tree
Hide file tree
Showing 20 changed files with 2,203 additions and 19 deletions.
42 changes: 36 additions & 6 deletions core/command/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ protected function configure() {
* @param InputInterface $input
* @param OutputInterface $output
* @param array $items
* @param string $prefix
*/
protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items) {
protected function writeArrayInOutputFormat(InputInterface $input, OutputInterface $output, $items, $prefix = ' - ') {
switch ($input->getOption('output')) {
case 'json':
$output->writeln(json_encode($items));
Expand All @@ -54,28 +55,57 @@ protected function writeArrayInOutputFormat(InputInterface $input, OutputInterfa
break;
default:
foreach ($items as $key => $item) {
if (is_array($item)) {
$output->writeln($prefix . $key . ':');
$this->writeArrayInOutputFormat($input, $output, $item, ' ' . $prefix);
continue;
}
if (!is_int($key)) {
$value = $this->valueToString($item);
if (!is_null($value)) {
$output->writeln(' - ' . $key . ': ' . $value);
$output->writeln($prefix . $key . ': ' . $value);
} else {
$output->writeln(' - ' . $key);
$output->writeln($prefix . $key);
}
} else {
$output->writeln(' - ' . $this->valueToString($item));
$output->writeln($prefix . $this->valueToString($item));
}
}
break;
}
}

protected function valueToString($value) {
/**
* @param InputInterface $input
* @param OutputInterface $output
* @param mixed $item
*/
protected function writeMixedInOutputFormat(InputInterface $input, OutputInterface $output, $item) {
if (is_array($item)) {
$this->writeArrayInOutputFormat($input, $output, $item, '');
return;
}

switch ($input->getOption('output')) {
case 'json':
$output->writeln(json_encode($item));
break;
case 'json_pretty':
$output->writeln(json_encode($item, JSON_PRETTY_PRINT));
break;
default:
$output->writeln($this->valueToString($item, false));
break;
}
}

protected function valueToString($value, $returnNull = true) {
if ($value === false) {
return 'false';
} else if ($value === true) {
return 'true';
} else if ($value === null) {
null;
return ($returnNull) ? null : 'null';
} else {
return $value;
}
Expand Down
81 changes: 81 additions & 0 deletions core/command/config/app/deleteconfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php
/**
* @author Joas Schilling <[email protected]>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Core\Command\Config\App;

use OC\Core\Command\Base;
use OCP\IConfig;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class DeleteConfig extends Base {
/** * @var IConfig */
protected $config;

/**
* @param IConfig $config
*/
public function __construct(IConfig $config) {
parent::__construct();
$this->config = $config;
}

protected function configure() {
parent::configure();

$this
->setName('config:app:delete')
->setDescription('Delete an app config value')
->addArgument(
'app',
InputArgument::REQUIRED,
'Name of the app'
)
->addArgument(
'name',
InputArgument::REQUIRED,
'Name of the config to delete'
)
->addOption(
'error-if-not-exists',
null,
InputOption::VALUE_NONE,
'Checks whether the config exists before deleting it'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output) {
$appName = $input->getArgument('app');
$configName = $input->getArgument('name');

if ($input->hasParameterOption('--error-if-not-exists') && !in_array($configName, $this->config->getAppKeys($appName))) {
$output->writeln('<error>Config ' . $configName . ' could not be deleted because it did not exist</error>');
return 1;
}

$this->config->deleteAppValue($appName, $configName);
$output->writeln('<info>System config value ' . $configName . ' deleted</info>');
return 0;
}
}
93 changes: 93 additions & 0 deletions core/command/config/app/getconfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* @author Joas Schilling <[email protected]>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Core\Command\Config\App;

use OC\Core\Command\Base;
use OCP\IConfig;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class GetConfig extends Base {
/** * @var IConfig */
protected $config;

/**
* @param IConfig $config
*/
public function __construct(IConfig $config) {
parent::__construct();
$this->config = $config;
}

protected function configure() {
parent::configure();

$this
->setName('config:app:get')
->setDescription('Get an app config value')
->addArgument(
'app',
InputArgument::REQUIRED,
'Name of the app'
)
->addArgument(
'name',
InputArgument::REQUIRED,
'Name of the config to get'
)
->addOption(
'default-value',
null,
InputOption::VALUE_OPTIONAL,
'If no default value is set and the config does not exist, the command will exit with 1'
)
;
}

/**
* Executes the current command.
*
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
* @return null|int null or 0 if everything went fine, or an error code
*/
protected function execute(InputInterface $input, OutputInterface $output) {
$appName = $input->getArgument('app');
$configName = $input->getArgument('name');
$defaultValue = $input->getOption('default-value');

if (!in_array($configName, $this->config->getAppKeys($appName)) && !$input->hasParameterOption('--default-value')) {
return 1;
}

if (!in_array($configName, $this->config->getAppKeys($appName))) {
$configValue = $defaultValue;
} else {
$configValue = $this->config->getAppValue($appName, $configName);
}

$this->writeMixedInOutputFormat($input, $output, $configValue);
return 0;
}
}
89 changes: 89 additions & 0 deletions core/command/config/app/setconfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php
/**
* @author Joas Schilling <[email protected]>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

namespace OC\Core\Command\Config\App;

use OC\Core\Command\Base;
use OCP\IConfig;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class SetConfig extends Base {
/** * @var IConfig */
protected $config;

/**
* @param IConfig $config
*/
public function __construct(IConfig $config) {
parent::__construct();
$this->config = $config;
}

protected function configure() {
parent::configure();

$this
->setName('config:app:set')
->setDescription('Set an app config value')
->addArgument(
'app',
InputArgument::REQUIRED,
'Name of the app'
)
->addArgument(
'name',
InputArgument::REQUIRED,
'Name of the config to set'
)
->addOption(
'value',
null,
InputOption::VALUE_REQUIRED,
'The new value of the config'
)
->addOption(
'update-only',
null,
InputOption::VALUE_NONE,
'Only updates the value, if it is not set before, it is not being added'
)
;
}

protected function execute(InputInterface $input, OutputInterface $output) {
$appName = $input->getArgument('app');
$configName = $input->getArgument('name');

if (!in_array($configName, $this->config->getAppKeys($appName)) && $input->hasParameterOption('--update-only')) {
$output->writeln('<comment>Value not updated, as it has not been set before.</comment>');
return 1;
}

$configValue = $input->getOption('value');
$this->config->setAppValue($appName, $configName, $configValue);

$output->writeln('<info>Config value ' . $configName . ' for app ' . $appName . ' set to ' . $configValue . '</info>');
return 0;
}
}
Loading

0 comments on commit d3e26a3

Please sign in to comment.