Completed
Push — master ( b1a829...012708 )
by Christoph
08:19
created

ListConfigs::getSystemConfigs()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 11
nc 5
nop 1
dl 0
loc 18
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
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
23
namespace OC\Core\Command\Config;
24
25
use OC\Core\Command\Base;
26
use OC\SystemConfig;
27
use OCP\IAppConfig;
28
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
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
34
class ListConfigs extends Base {
35
	protected $defaultOutputFormat = self::OUTPUT_FORMAT_JSON_PRETTY;
36
37
	/** * @var SystemConfig */
38
	protected $systemConfig;
39
40
	/** @var IAppConfig */
41
	protected $appConfig;
42
43
	/**
44
	 * @param SystemConfig $systemConfig
45
	 * @param IAppConfig $appConfig
46
	 */
47
	public function __construct(SystemConfig $systemConfig, IAppConfig $appConfig) {
48
		parent::__construct();
49
		$this->systemConfig = $systemConfig;
50
		$this->appConfig = $appConfig;
51
	}
52
53
	protected function configure() {
54
		parent::configure();
55
56
		$this
57
			->setName('config:list')
58
			->setDescription('List all configs')
59
			->addArgument(
60
				'app',
61
				InputArgument::OPTIONAL,
62
				'Name of the app ("system" to get the config.php values, "all" for all apps and system)',
63
				'all'
64
			)
65
			->addOption(
66
				'private',
67
				null,
68
				InputOption::VALUE_NONE,
69
				'Use this option when you want to include sensitive configs like passwords, salts, ...'
70
			)
71
		;
72
	}
73
74
	protected function execute(InputInterface $input, OutputInterface $output) {
75
		$app = $input->getArgument('app');
76
		$noSensitiveValues = !$input->getOption('private');
77
78
		switch ($app) {
79
			case 'system':
80
				$configs = [
81
					'system' => $this->getSystemConfigs($noSensitiveValues),
82
				];
83
			break;
84
85
			case 'all':
86
				$apps = $this->appConfig->getApps();
87
				$configs = [
88
					'system' => $this->getSystemConfigs($noSensitiveValues),
89
					'apps' => [],
90
				];
91
				foreach ($apps as $appName) {
92
					$configs['apps'][$appName] = $this->getAppConfigs($appName, $noSensitiveValues);
93
				}
94
			break;
95
96
			default:
97
				$configs = [
98
					'apps' => [
99
						$app => $this->getAppConfigs($app, $noSensitiveValues),
100
					],
101
				];
102
		}
103
104
		$this->writeArrayInOutputFormat($input, $output, $configs);
105
	}
106
107
	/**
108
	 * Get the system configs
109
	 *
110
	 * @param bool $noSensitiveValues
111
	 * @return array
112
	 */
113
	protected function getSystemConfigs($noSensitiveValues) {
114
		$keys = $this->systemConfig->getKeys();
115
116
		$configs = [];
117
		foreach ($keys as $key) {
118
			if ($noSensitiveValues) {
119
				$value = $this->systemConfig->getFilteredValue($key, serialize(null));
120
			} else {
121
				$value = $this->systemConfig->getValue($key, serialize(null));
122
			}
123
124
			if ($value !== 'N;') {
125
				$configs[$key] = $value;
126
			}
127
		}
128
129
		return $configs;
130
	}
131
132
	/**
133
	 * Get the app configs
134
	 *
135
	 * @param string $app
136
	 * @param bool $noSensitiveValues
137
	 * @return array
138
	 */
139
	protected function getAppConfigs($app, $noSensitiveValues) {
140
		if ($noSensitiveValues) {
141
			return $this->appConfig->getFilteredValues($app, false);
142
		} else {
143
			return $this->appConfig->getValues($app, false);
144
		}
145
	}
146
147
	/**
148
	 * @param string $argumentName
149
	 * @param CompletionContext $context
150
	 * @return string[]
151
	 */
152
	public function completeArgumentValues($argumentName, CompletionContext $context) {
153
		if ($argumentName === 'app') {
154
			return array_merge(['all', 'system'], \OC_App::getAllApps());
155
		}
156
		return [];
157
	}
158
}
159