1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @package: chapi |
4
|
|
|
* |
5
|
|
|
* @author: msiebeneicher |
6
|
|
|
* @since: 2015-07-28 |
7
|
|
|
* |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Chapi\Commands; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Symfony\Component\Console\Question\Question; |
16
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
17
|
|
|
use Symfony\Component\Yaml\Dumper; |
18
|
|
|
use Symfony\Component\Yaml\Parser; |
19
|
|
|
|
20
|
|
|
class ConfigureCommand extends AbstractCommand |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Configures the current command. |
24
|
|
|
*/ |
25
|
4 |
|
protected function configure() |
26
|
|
|
{ |
27
|
4 |
|
$this->setName('configure') |
28
|
4 |
|
->setDescription('Configure application and add necessary configs') |
29
|
4 |
|
->addOption('cache_dir', 'd', InputOption::VALUE_OPTIONAL, 'Path to cache directory') |
30
|
|
|
|
31
|
4 |
|
->addOption('chronos_url', 'u', InputOption::VALUE_OPTIONAL, 'The chronos url (inclusive port)', '') |
32
|
4 |
|
->addOption('chronos_http_username', 'un', InputOption::VALUE_OPTIONAL, 'The chronos username (HTTP credentials)', '') |
33
|
4 |
|
->addOption('chronos_http_password', 'p', InputOption::VALUE_OPTIONAL, 'The chronos password (HTTP credentials)', '') |
34
|
4 |
|
->addOption('repository_dir', 'r', InputOption::VALUE_OPTIONAL, 'Root path to your job files', '') |
35
|
|
|
|
36
|
4 |
|
->addOption('marathon_url', 'mu', InputOption::VALUE_OPTIONAL, 'The marathon url (inclusive port)', '') |
37
|
4 |
|
->addOption('marathon_http_username', 'mun', InputOption::VALUE_OPTIONAL, 'The marathon username (HTTP credentials)', '') |
38
|
4 |
|
->addOption('marathon_http_password', 'mp', InputOption::VALUE_OPTIONAL, 'The marathon password (HTTP credentials)', '') |
39
|
4 |
|
->addOption('repository_dir_marathon', 'mr', InputOption::VALUE_OPTIONAL, 'Root path to the app files', '') |
40
|
|
|
; |
41
|
4 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param InputInterface $oInput |
45
|
|
|
* @param OutputInterface $oOutput |
46
|
|
|
* @return int |
47
|
|
|
*/ |
48
|
4 |
|
protected function execute(InputInterface $oInput, OutputInterface $oOutput) |
49
|
|
|
{ |
50
|
4 |
|
$this->oInput = $oInput; |
|
|
|
|
51
|
4 |
|
$this->oOutput = $oOutput; |
52
|
|
|
|
53
|
4 |
|
return $this->process(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return int |
58
|
|
|
*/ |
59
|
4 |
|
protected function process() |
60
|
|
|
{ |
61
|
4 |
|
$_aParams = $this->getInputValues(); |
62
|
|
|
|
63
|
4 |
|
if ($this->hasValidateUserInput($_aParams)) |
64
|
|
|
{ |
65
|
3 |
|
$this->saveParameters($_aParams); |
66
|
3 |
|
return 0; |
67
|
|
|
} |
68
|
|
|
|
69
|
1 |
|
return 1; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return array<string,array<string,string|boolean>> |
74
|
|
|
*/ |
75
|
4 |
|
private function getInputValues() |
76
|
|
|
{ |
77
|
4 |
|
$_aResult = []; |
78
|
|
|
|
79
|
4 |
|
$_aResult['cache_dir'] = [ |
80
|
4 |
|
'value' => $this->getInputValue('cache_dir', '[GLOBAL] Please enter a cache directory'), |
81
|
|
|
'required' => true |
82
|
|
|
]; |
83
|
|
|
|
84
|
4 |
|
$_aResult['chronos_url'] = [ |
85
|
4 |
|
'value' => $this->getInputValue('chronos_url', '[CHRONOS] Please enter the chronos url (inclusive port)'), |
86
|
|
|
'required' => false |
87
|
|
|
]; |
88
|
|
|
|
89
|
4 |
|
$_aResult['chronos_http_username'] = [ |
90
|
4 |
|
'value' => $this->getInputValue('chronos_http_username', '[CHRONOS] Please enter the username to access your chronos instance'), |
91
|
|
|
'required' => false |
92
|
|
|
]; |
93
|
|
|
|
94
|
4 |
|
$_aResult['chronos_http_password'] = [ |
95
|
4 |
|
'value' => $this->getInputValue('chronos_http_password', '[CHRONOS] Please enter the password to access your chronos instance', true), |
96
|
|
|
'required' => false |
97
|
|
|
]; |
98
|
|
|
|
99
|
4 |
|
$_aResult['repository_dir'] = [ |
100
|
4 |
|
'value' => $this->getInputValue('repository_dir', '[CHRONOS] Please enter absolute path to your local chronos jobs configurations'), |
101
|
|
|
'required' => false |
102
|
|
|
]; |
103
|
|
|
|
104
|
4 |
|
$_aResult['marathon_url'] = [ |
105
|
4 |
|
'value' => $this->getInputValue('marathon_url', '[MARATHON] Please enter the marathon url (inclusive port)'), |
106
|
|
|
'required' => false |
107
|
|
|
]; |
108
|
|
|
|
109
|
4 |
|
$_aResult['marathon_http_username'] = [ |
110
|
4 |
|
'value' => $this->getInputValue('marathon_http_username', '[MARATHON] Please enter the username to access marathon instance'), |
111
|
|
|
'required' => false |
112
|
|
|
]; |
113
|
|
|
|
114
|
4 |
|
$_aResult['marathon_http_password'] = [ |
115
|
4 |
|
'value' => $this->getInputValue('marathon_http_password', '[MARATHON] Please enter the password to access marathon instance', true), |
116
|
|
|
'required' => false |
117
|
|
|
]; |
118
|
|
|
|
119
|
4 |
|
$_aResult['repository_dir_marathon'] = [ |
120
|
4 |
|
'value' => $this->getInputValue('repository_dir_marathon', '[MARATHON] Please enter absolute path to your local marathon tasks configurations'), |
121
|
|
|
'required' => false |
122
|
|
|
]; |
123
|
|
|
|
124
|
4 |
|
return $_aResult; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* @param string $sValueKey |
129
|
|
|
* @param string $sQuestion |
130
|
|
|
* @param boolean $bHiddenAnswer |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
4 |
|
private function getInputValue($sValueKey, $sQuestion, $bHiddenAnswer = false) |
134
|
|
|
{ |
135
|
4 |
|
$_sValue = $this->oInput->getOption($sValueKey); |
136
|
4 |
|
if (empty($_sValue)) |
137
|
|
|
{ |
138
|
3 |
|
$_sValue = $this->printQuestion( |
139
|
|
|
$sQuestion, |
140
|
3 |
|
$this->getParameterValue($sValueKey), |
141
|
|
|
$bHiddenAnswer |
142
|
|
|
); |
143
|
|
|
} |
144
|
|
|
|
145
|
4 |
|
return $_sValue; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array $aUserInput |
150
|
|
|
*/ |
151
|
3 |
|
private function saveParameters(array $aUserInput) |
152
|
|
|
{ |
153
|
|
|
// We implemented an additional level of information |
154
|
|
|
// into the user input array: Is this field required or not? |
155
|
|
|
// To be backwards compatible we only store the value of |
156
|
|
|
// the question in the dump file. |
157
|
|
|
// With this loop we get rid of the "required" information |
158
|
|
|
// from getInputValues(). |
159
|
3 |
|
$aToStore = []; |
160
|
3 |
|
foreach ($aUserInput as $key => $value) |
161
|
|
|
{ |
162
|
3 |
|
$aToStore[$key] = $value['value']; |
163
|
|
|
} |
164
|
|
|
|
165
|
3 |
|
$_oDumper = new Dumper(); |
166
|
3 |
|
$_sYaml = $_oDumper->dump(array('parameters' => $aToStore), 2); |
|
|
|
|
167
|
|
|
|
168
|
3 |
|
$_oFileSystem = new Filesystem(); |
169
|
3 |
|
$_oFileSystem->dumpFile( |
170
|
3 |
|
$this->getHomeDir() . DIRECTORY_SEPARATOR . $this->getParameterFileName(), |
171
|
|
|
$_sYaml |
172
|
|
|
); |
173
|
3 |
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* @param array $aUserInput |
177
|
|
|
* @return bool |
178
|
|
|
*/ |
179
|
4 |
|
private function hasValidateUserInput(array $aUserInput) |
180
|
|
|
{ |
181
|
4 |
|
foreach ($aUserInput as $_sKey => $_sValue) |
182
|
|
|
{ |
183
|
4 |
|
if ($_sValue['required'] == true && empty($_sValue['value'])) |
184
|
|
|
{ |
185
|
1 |
|
$this->oOutput->writeln(sprintf('<error>Please add a valid value for parameter "%s"</error>', $_sKey)); |
186
|
4 |
|
return false; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
3 |
|
return true; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @param string $sKey |
195
|
|
|
* @param mixed $mDefaultValue |
196
|
|
|
* @return mixed |
197
|
|
|
*/ |
198
|
3 |
|
private function getParameterValue($sKey, $mDefaultValue = null) |
199
|
|
|
{ |
200
|
3 |
|
$_oParser = new Parser(); |
|
|
|
|
201
|
3 |
|
$_sParameterFile = $this->getHomeDir() . DIRECTORY_SEPARATOR . $this->getParameterFileName(); |
202
|
|
|
|
203
|
3 |
|
if (file_exists($_sParameterFile)) |
204
|
|
|
{ |
205
|
|
|
$_aParameters = $_oParser->parse( |
206
|
|
|
file_get_contents($_sParameterFile) |
207
|
|
|
); |
208
|
|
|
|
209
|
|
|
if (isset($_aParameters['parameters']) && isset($_aParameters['parameters'][$sKey])) |
210
|
|
|
{ |
211
|
|
|
return $_aParameters['parameters'][$sKey]; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|
215
|
3 |
|
return $mDefaultValue; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param string $sQuestion |
220
|
|
|
* @param null|mixed $mDefaultValue |
221
|
|
|
* @param boolean $bHiddenAnswer |
222
|
|
|
* @return mixed |
223
|
|
|
*/ |
224
|
3 |
|
private function printQuestion($sQuestion, $mDefaultValue = null, $bHiddenAnswer = false) |
225
|
|
|
{ |
226
|
3 |
|
$_oHelper = $this->getHelper('question'); |
227
|
|
|
|
228
|
|
|
// If we have a hidden answer and the default value is not empty |
229
|
|
|
// the we will set it as empty, because we don`t want to show |
230
|
|
|
// the default value on the terminal. |
231
|
|
|
// We know that the user has to enter the password again |
232
|
|
|
// if he / she want to reconfigure something. But this |
233
|
|
|
// is an acceptable tradeoff. |
234
|
3 |
|
if ($bHiddenAnswer === true && !empty($mDefaultValue)) |
235
|
|
|
{ |
236
|
|
|
$mDefaultValue = null; |
|
|
|
|
237
|
|
|
} |
238
|
|
|
|
239
|
3 |
|
$_sFormat = (!empty($mDefaultValue)) ? '<comment>%s (default: %s):</comment>' : '<comment>%s:</comment>'; |
|
|
|
|
240
|
3 |
|
$_oQuestion = new Question(sprintf($_sFormat, $sQuestion, $mDefaultValue), $mDefaultValue); |
241
|
|
|
|
242
|
|
|
// Sensitive information (like passwords) should not be |
243
|
|
|
// visible during the configuration wizard |
244
|
3 |
|
if ($bHiddenAnswer === true) |
245
|
|
|
{ |
246
|
3 |
|
$_oQuestion->setHidden(true); |
247
|
3 |
|
$_oQuestion->setHiddenFallback(false); |
248
|
|
|
} |
249
|
|
|
|
250
|
3 |
|
return $_oHelper->ask($this->oInput, $this->oOutput, $_oQuestion); |
251
|
|
|
} |
252
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.