-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_command.php
More file actions
313 lines (270 loc) · 8 KB
/
Copy pathcli_command.php
File metadata and controls
313 lines (270 loc) · 8 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<?php
if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!');
final class PwgCommand
{
public const SUCCESS = 0; // the command ran fine
public const ERROR = 1; // the command ran and failed, or was aborted
public const INVALID = 2; // the command never ran, the user typed something wrong
private const GREEN = 32;
private const RED = 31;
private const YELLOW = 33;
private static ?bool $decorated = null;
private static bool $assume_yes = false;
private static bool $verbose = false;
private static bool $dry_run = false;
private function __construct() {}
/**
* Make every confirm() answer yes without asking. Called by the engine when
* --yes is passed, a command never needs to call it.
*/
public static function assume_yes()
{
self::$assume_yes = true;
}
/**
* Turn dry-run mode on. Called by the engine when --dry-run is passed, a
* command never needs to call it.
*/
public static function set_dry_run()
{
self::$dry_run = true;
}
/**
* True when --dry-run was passed. A command that writes anything MUST honor
* it: report what would happen, change nothing, return SUCCESS.
*/
public static function is_dry_run(): bool
{
return self::$dry_run;
}
/**
* Turn verbose mode on. Called by the engine when --verbose is passed, a
* command never needs to call it.
*/
public static function set_verbose()
{
self::$verbose = true;
}
/**
* True when --verbose was passed: print your debug details (full errors,
* timings...), keep the default output clean otherwise.
*/
public static function is_verbose(): bool
{
return self::$verbose;
}
/**
* Ask a free-form question and return the trimmed answer. On a closed STDIN
* (cron, CI) it returns the empty string instead of blocking, so give every
* question a sensible empty-answer behavior.
*/
public static function prompt(string $message): string
{
self::output(STDOUT, $message.' ', PHP_EOL, '');
$input = fgets(STDIN);
// EOF (cron, closed stdin) answers the empty string, never blocks
return $input === false
? ''
: trim($input);
}
/**
* Ask before doing something destructive. Defaults to no: --yes is the only
* way to say yes non-interactively, so a cron without it aborts safely.
*/
public static function confirm(string $question): bool
{
if (self::$assume_yes)
{
return true;
}
$answer = strtolower(self::prompt($question.' [y/N]'));
return in_array($answer, ['y', 'yes']);
}
/**
* Color a piece of text green, for building custom output like a report
* line. Colors only show on a terminal, a piped or redirected output stays
* plain, safe to use anywhere.
*/
public static function green(string $text): string
{
return self::paint($text, self::GREEN);
}
/**
* Color a piece of text yellow. Same terminal-only rule as green().
*/
public static function yellow(string $text): string
{
return self::paint($text, self::YELLOW);
}
/**
* Color a piece of text red. Same terminal-only rule as green().
*/
public static function red(string $text): string
{
return self::paint($text, self::RED);
}
private static function paint(string $text, int $code): string
{
global $conf;
if (self::$decorated === null)
{
self::$decorated = stream_isatty(STDOUT);
}
// if we don't out the result in file and allow color show color
return self::$decorated && $conf['cli_allow_color']
? "\033[".$code."m".$text."\033[0m"
: $text;
}
/**
* @param resource $stream
* @param string|array $message
*/
private static function output($stream, $message, string $line_start = PHP_EOL, string $line_end = PHP_EOL)
{
fwrite($stream, self::implode_recursive($line_start, (array) $message) . $line_end);
}
private static function implode_recursive(string $separator, array $array): string
{
$flat = [];
foreach ($array as $a)
{
$flat[] = is_array($a) ? self::implode_recursive($separator, $a) : $a;
}
return implode($separator, $flat);
}
/**
* Print on STDOUT, ending with a newline. An array prints one line per
* entry, so a whole report can be built then written in one call.
*
* @param string|array $message
*/
public static function writeln($message)
{
self::output(STDOUT, $message);
}
/**
* Print on STDOUT without a newline, to build a line piece by piece.
* An array prints its entries separated by a comma.
*
* @param string|array $message
*/
public static function write($message)
{
self::output(STDOUT, $message, ', ', '');
}
/**
* Print anything as pretty JSON on STDOUT. Prefer it when you want to print
* readable data (like 1 user): writeln() flattens arrays into lines and
* loses the keys, writeJson() keeps the structure and stays parseable.
*
* @param mixed $message
*/
public static function writeJson($message)
{
fwrite(STDOUT, json_encode($message, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . PHP_EOL);
}
/**
* Print on STDERR, ending with a newline. Use it for anything that is not
* the command's result, so "pwg x > file" keeps the file clean.
*
* @param string|array $message
*/
public static function errln($message)
{
self::output(STDERR, $message);
}
/**
* Print a mysql-style table on STDOUT. The keys of the first row are the
* headers.
*
* The optional $headers replaces the displayed names (in column order), and
* makes an empty $rows still show the header grid instead of nothing.
*/
public static function table(array $rows, ?array $headers = null)
{
$columns = empty($rows) ? [] : array_keys((array) reset($rows));
if (null === $headers)
{
if (empty($columns))
{
return; // no rows, no headers: nothing to show
}
$headers = $columns;
}
elseif (empty($columns))
{
$columns = array_keys($headers); // header-only grid for an empty result
}
$headers = array_values($headers);
$widths = [];
foreach ($headers as $i => $header)
{
$widths[$i] = self::visible_width((string) $header);
}
foreach ($rows as $row)
{
$row = (array) $row;
foreach ($columns as $i => $key)
{
$widths[$i] = max($widths[$i] ?? 0, self::visible_width((string) ($row[$key] ?? '')));
}
}
$separator = '+';
foreach ($widths as $width)
{
$separator .= str_repeat('-', $width + 2).'+';
}
$lines = [$separator, self::table_row($headers, $widths), $separator];
foreach ($rows as $row)
{
$row = (array) $row;
$cells = [];
foreach ($columns as $key)
{
$cells[] = $row[$key] ?? '';
}
$lines[] = self::table_row($cells, $widths);
}
$lines[] = $separator;
self::writeln($lines);
}
private static function table_row(array $cells, array $widths): string
{
$line = '|';
foreach ($widths as $i => $width)
{
$cell = (string) ($cells[$i] ?? '');
$line .= ' '.$cell.str_repeat(' ', $width - self::visible_width($cell)).' |';
}
return $line;
}
// str_pad counts bytes, an accent would shift every column after it
private static function visible_width(string $text): int
{
return function_exists('mb_strwidth') ? mb_strwidth($text, 'UTF-8') : strlen($text);
}
/**
* Print a green "[OK]" message on STDOUT, for the final good news of a
* command.
*/
public static function success(string $message)
{
self::writeln(self::paint('[OK] ', self::GREEN). $message);
}
/**
* Print a yellow "[WARNING]" message on STDERR: something odd but not
* blocking, the command goes on.
*/
public static function warning(string $message)
{
self::errln(self::paint('[WARNING] ', self::YELLOW). $message);
}
/**
* Print a red "[ERROR]" message on STDERR. It only prints, the command
* still has to return PwgCommand::ERROR itself.
*/
public static function error(string $message)
{
self::errln(self::paint('[ERROR] ', self::RED). $message);
}
}