3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
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.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Console;
24 use Asika\SimpleConsole\CommandArgsException;
26 use Friendica\Core\Config\Capability\IManageConfigValues;
30 * tool to access the system config from the CLI
32 * With this script you can access the system configuration of your node from
33 * the CLI. You can do both, reading current values stored in the database and
34 * set new values to config variables.
37 * If you specify no parameters at the CLI, the script will list all config
40 * If you specify one parameter, the script will list all config variables
41 * defined in this section of the configuration (e.g. "system").
43 * If you specify two parameters, the script will show you the current value
44 * of the named configuration setting. (e.g. "system loglevel")
46 * If you specify three parameters, the named configuration setting will be
47 * set to the value of the last parameter. (e.g. "system loglevel 0" will
50 class Config extends \Asika\SimpleConsole\Console
52 protected $helpOptions = ['h', 'help', '?'];
59 * @var IManageConfigValues
63 protected function getHelp()
66 console config - Manage site configuration
68 bin/console config [-h|--help|-?] [-v]
69 bin/console config <category> [-h|--help|-?] [-v]
70 bin/console config <category> <key> [-h|--help|-?] [-v]
71 bin/console config <category> <key> <value> [-h|--help|-?] [-v]
75 Lists all config values
77 bin/console config <category>
78 Lists all config values in the provided category
80 bin/console config <category> <key>
81 Shows the value of the provided key in the category
83 bin/console config <category> <key> <value>
84 Sets the value of the provided key in the category
87 Setting config entries which are manually set in config/local.config.php may result in
88 conflict between database settings and the manual startup settings.
91 -h|--help|-? Show help information
92 -v Show more debug information.
97 public function __construct(App\Mode $appMode, IManageConfigValues $config, array $argv = null)
99 parent::__construct($argv);
101 $this->appMode = $appMode;
102 $this->config = $config;
105 protected function doExecute(): int
107 if ($this->getOption('v')) {
108 $this->out('Executable: ' . $this->executable);
109 $this->out('Class: ' . __CLASS__);
110 $this->out('Arguments: ' . var_export($this->args, true));
111 $this->out('Options: ' . var_export($this->options, true));
114 if (count($this->args) > 3) {
115 throw new CommandArgsException('Too many arguments');
118 if (!$this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
119 $this->out('Database isn\'t ready or populated yet, showing file config only');
122 if (count($this->args) == 3) {
123 $cat = $this->getArgument(0);
124 $key = $this->getArgument(1);
125 $value = $this->getArgument(2);
127 if (is_array($this->config->get($cat, $key))) {
128 throw new RuntimeException("$cat.$key is an array and can't be set using this command.");
131 if ($this->config->get($cat, $key) === $value) {
132 throw new RuntimeException("$cat.$key already set to $value.");
135 $result = $this->config->set($cat, $key, $value);
137 $this->out("{$cat}.{$key} <= " .
138 $this->config->get($cat, $key));
140 $this->out("Unable to set {$cat}.{$key}");
144 if (count($this->args) == 2) {
145 $cat = $this->getArgument(0);
146 $key = $this->getArgument(1);
147 $value = $this->config->get($this->getArgument(0), $this->getArgument(1));
149 if (is_array($value)) {
150 foreach ($value as $k => $v) {
151 $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
154 $this->out("{$cat}.{$key} => " . ($value ?? 'NULL'));
158 if (count($this->args) == 1) {
159 $cat = $this->getArgument(0);
160 $this->config->load($cat);
161 $configCache = $this->config->getCache();
163 if ($configCache->get($cat) !== null) {
164 $this->out("[{$cat}]");
165 $catVal = $configCache->get($cat);
166 foreach ($catVal as $key => $value) {
167 if (is_array($value)) {
168 foreach ($value as $k => $v) {
169 $this->out("{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
172 $this->out("{$key} => " . $value);
176 $this->out('Config section ' . $this->getArgument(0) . ' returned nothing');
180 if (count($this->args) == 0) {
181 $this->config->load();
183 if ($this->config->get('system', 'config_adapter') == 'jit' && $this->appMode->has(App\Mode::DBCONFIGAVAILABLE)) {
184 $this->out('Warning: The JIT (Just In Time) Config adapter doesn\'t support loading the entire configuration, showing file config only');
187 $config = $this->config->getCache()->getAll();
188 foreach ($config as $cat => $section) {
189 if (is_array($section)) {
190 foreach ($section as $key => $value) {
191 if (is_array($value)) {
192 foreach ($value as $k => $v) {
193 $this->out("{$cat}.{$key}[{$k}] => " . (is_array($v) ? implode(', ', $v) : $v));
196 $this->out("{$cat}.{$key} => " . $value);
200 $this->out("config.{$cat} => " . $section);