forked from webdevops/clitools
/
AbstractCommand.php
259 lines (220 loc) · 7.16 KB
/
AbstractCommand.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
namespace CliTools\Console\Command;
/*
* CliTools Command
* Copyright (C) 2015 Markus Blaschke <markus@familie-blaschke.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
use CliTools\Utility\ConsoleUtility;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use CliTools\Shell\CommandBuilder\FullSelfCommandBuilder;
use CliTools\Shell\CommandBuilder\CommandBuilder;
abstract class AbstractCommand extends Command {
/**
* Message list (will be shown at the end)
*
* @var array
*/
protected $finishMessageList = array();
/**
* Input
*
* @var InputInterface
*/
protected $input;
/**
* Input
*
* @var OutputInterface
*/
protected $output;
/**
* Initializes the command just after the input has been validated.
*
* This is mainly useful when a lot of commands extends one main command
* where some things need to be initialized based on the input arguments and options.
*
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*/
protected function initialize(InputInterface $input, OutputInterface $output) {
$this->input = $input;
$this->output = $output;
ConsoleUtility::initialize($input, $output);
// Set default terminal title
$this->setTerminalTitle(explode(':', $this->getName()));
}
/**
* Runs the command.
*
* The code to execute is either defined directly with the
* setCode() method or by overriding the execute() method
* in a sub-class.
*
* @param InputInterface $input An InputInterface instance
* @param OutputInterface $output An OutputInterface instance
*
* @return int The command exit code
*
* @throws \Exception
*
* @see setCode()
* @see execute()
*
* @api
*/
public function run(InputInterface $input, OutputInterface $output) {
try {
$ret = parent::run($input, $output);
$this->showFinishMessages();
} catch (\Exception $e) {
$this->showFinishMessages();
throw $e;
}
return $ret;
}
/**
* Get full parameter list
*
* @param integer $offset Parameter offset
*
* @return mixed
*/
protected function getFullParameterList($offset = null) {
$ret = $_SERVER['argv'];
// remove requested offset
if ($offset !== null) {
$ret = array_splice($ret, $offset);
}
return $ret;
}
/**
* Elevate process (exec sudo with same parameters)
*
* @param InputInterface $input Input instance
* @param OutputInterface $output Output instance
*
* @return int|null|void
*/
protected function elevateProcess(InputInterface $input, OutputInterface $output) {
if (!$this->getApplication()->isRunningAsRoot()) {
// Process is not running as root, trying to elevate to root
$output->writeln('<comment>Elevating process using sudo...</comment>');
try {
$commandMyself = new FullSelfCommandBuilder();
$commandSudo = new CommandBuilder('sudo');
$commandSudo->append($commandMyself, false);
$commandSudo->executeInteractive();
} catch (\Exception $e) {
// do not display exception here because it's a child process
}
throw new \CliTools\Exception\StopException(0);
} else {
// running as root
}
}
/**
* Show log, passthru multitail
*
* @param array $logList List of log files
* @param InputInterface $input Input instance
* @param OutputInterface $output Output instance
* @param string $grep Grep value
* @param array $optionList Additional option list for multitail
*
* @return int|null|void
* @throws \Exception
*/
protected function showLog($logList, $input, $output, $grep = null, $optionList = null) {
$this->elevateProcess($input, $output);
// check if logfiles are accessable
foreach ($logList as $log) {
if (!is_readable($log)) {
$output->writeln('<p-error>Can\'t read ' . $log . '</p-error>');
return 1;
}
}
$output->writeln('<p>Reading logfile with multitail</p>');
$command = new CommandBuilder('multitail', '--follow-all');
// Add grep
if ($grep !== null) {
$command->addArgumentTemplate('-E %s', $grep);
}
// Add log
$command->addArgumentList($logList);
$command->executeInteractive();
return 0;
}
/**
* Add message to finish list
*
* @param string $message Message
*/
protected function addFinishMessage($message) {
$this->output->writeln($message);
$this->finishMessageList[] = $message;
}
/**
* Show all finish messages
*/
protected function showFinishMessages() {
if (!empty($this->finishMessageList)) {
$this->output->writeln('');
$this->output->writeln('Replay finish message log:');
foreach ($this->finishMessageList as $message) {
$this->output->writeln(' - ' . $message);
}
}
$this->finishMessageList = array();
}
/**
* Gets the application instance for this command.
*
* @return \CliTools\Console\Application An Application instance
*
* @api
*/
public function getApplication() {
return parent::getApplication();
}
/**
* Sets the terminal title of the command.
*
* This feature should be used only when creating a long process command,
* like a daemon.
*
* PHP 5.5+ or the proctitle PECL library is required
*
* @param string $title The terminal title
*
* @return Command The current instance
*/
public function setTerminalTitle($title) {
$args = func_get_args();
$titleList = array();
foreach($args as $value) {
if (is_array($value)) {
$value = implode(' ', $value);
}
$titleList[] = trim($value);
}
$title = implode(' ', $titleList);
$title = trim($title);
$this->getApplication()->setTerminalTitle($title);
return $this;
}
}