-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathCLI_Alias_Command.php
More file actions
560 lines (492 loc) · 15.3 KB
/
CLI_Alias_Command.php
File metadata and controls
560 lines (492 loc) · 15.3 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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
* Retrieves, sets and updates aliases for WordPress Installations.
*/
use Mustangostang\Spyc;
use WP_CLI\ExitException;
use WP_CLI\Utils;
/**
* Retrieves, sets and updates aliases for WordPress Installations.
*
* Aliases are shorthand references to WordPress installs. For instance,
* `@dev` could refer to a development install and `@prod` could refer to a production install.
* This command gives you and option to add, update and delete, the registered aliases you have available.
*
* Learn more about [running commands remotely](https://make.wordpress.org/cli/handbook/guides/running-commands-remotely/).
*
* ## EXAMPLES
*
* # List alias information.
* $ wp cli alias list
* list
* ---
* @all: Run command against every registered alias.
* @local:
* user: wpcli
* path: /Users/wpcli/sites/testsite
*
* # Get alias information.
* $ wp cli alias get @dev
* ssh: dev@somedeve.env:12345/home/dev/
*
* # Add alias.
* $ wp cli alias add @prod --set-ssh=login@host --set-path=/path/to/wordpress/install/ --set-user=wpcli
* Success: Added '@prod' alias.
*
* # Update alias.
* $ wp cli alias update @prod --set-user=newuser --set-path=/new/path/to/wordpress/install/
* Success: Updated 'prod' alias.
*
* # Delete alias.
* $ wp cli alias delete @prod
* Success: Deleted '@prod' alias.
*
* @package wp-cli
* @when before_wp_load
*/
class CLI_Alias_Command extends WP_CLI_Command {
/**
* Lists available WP-CLI aliases.
*
* ## OPTIONS
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: yaml
* options:
* - yaml
* - json
* - var_export
* ---
*
* ## EXAMPLES
*
* # List all available aliases.
* $ wp cli alias list
* ---
* @all: Run command against every registered alias.
* @prod:
* ssh: runcommand@runcommand.io~/webapps/production
* @dev:
* ssh: vagrant@192.168.50.10/srv/www/runcommand.dev
* @both:
* - @prod
* - @dev
*
* @subcommand list
*
* @param array $args Positional arguments. Unused.
* @param array{format: string} $assoc_args Associative arguments.
*/
public function list_( $args, $assoc_args ) {
WP_CLI::print_value( WP_CLI::get_runner()->aliases, $assoc_args );
}
/**
* Gets the value for an alias.
*
* ## OPTIONS
*
* <key>
* : Key for the alias.
*
* ## EXAMPLES
*
* # Get alias.
* $ wp cli alias get @prod
* ssh: dev@somedeve.env:12345/home/dev/
*
* @param array{string} $args Positional arguments.
*/
public function get( $args ) {
list( $alias ) = $args;
$aliases = WP_CLI::get_runner()->aliases;
if ( empty( $aliases[ $alias ] ) ) {
WP_CLI::error( "No alias found with key '{$alias}'." );
}
foreach ( $aliases[ $alias ] as $key => $value ) {
WP_CLI::log( "{$key}: {$value}" );
}
}
/**
* Creates an alias.
*
* ## OPTIONS
*
* <key>
* : Key for the alias.
*
* [--set-user=<user>]
* : Set user for alias.
*
* [--set-url=<url>]
* : Set url for alias.
*
* [--set-path=<path>]
* : Set path for alias.
*
* [--set-ssh=<ssh>]
* : Set ssh for alias.
*
* [--set-http=<http>]
* : Set http for alias.
*
* [--grouping=<grouping>]
* : For grouping multiple aliases.
*
* [--config=<config>]
* : Config file to be considered for operations.
* ---
* default: global
* options:
* - global
* - project
* ---
*
* ## EXAMPLES
*
* # Add alias to global config.
* $ wp cli alias add @prod --set-ssh=login@host --set-path=/path/to/wordpress/install/ --set-user=wpcli
* Success: Added '@prod' alias.
*
* # Add alias to project config.
* $ wp cli alias add @prod --set-ssh=login@host --set-path=/path/to/wordpress/install/ --set-user=wpcli --config=project
* Success: Added '@prod' alias.
*
* # Add group of aliases.
* $ wp cli alias add @multiservers --grouping=servera,serverb
* Success: Added '@multiservers' alias.
*
* @param array{string} $args Positional arguments.
* @param array{'set-user'?: string, 'set-url'?: string, 'set-path'?: string, 'set-ssh'?: string, 'set-http'?: string, grouping?: string, config?: string} $assoc_args Associative arguments.
*/
public function add( $args, $assoc_args ) {
$config = ( ! empty( $assoc_args['config'] ) ? $assoc_args['config'] : 'global' );
list( $config_path, $aliases ) = $this->get_aliases_data( $config, '', true );
$this->validate_config_file( $config_path );
$alias = $args[0];
/**
* @var string|null $grouping
*/
$grouping = Utils\get_flag_value( $assoc_args, 'grouping' );
$this->validate_input( $assoc_args, $grouping );
if ( isset( $aliases[ $alias ] ) ) {
WP_CLI::error( "Key '{$alias}' exists already." );
}
if ( null === $grouping ) {
$aliases = $this->build_aliases( $aliases, $alias, $assoc_args, false );
} else {
$aliases = $this->build_aliases( $aliases, $alias, $assoc_args, true, $grouping );
}
$this->process_aliases( $aliases, $alias, $config_path, 'Added' );
}
/**
* Deletes an alias.
*
* ## OPTIONS
*
* <key>
* : Key for the alias.
*
* [--config=<config>]
* : Config file to be considered for operations.
* ---
* options:
* - global
* - project
* ---
*
* ## EXAMPLES
*
* # Delete alias.
* $ wp cli alias delete @prod
* Success: Deleted '@prod' alias.
*
* # Delete project alias.
* $ wp cli alias delete @prod --config=project
* Success: Deleted '@prod' alias.
*
* @param array{string} $args Positional arguments.
* @param array{config?: string} $assoc_args Associative arguments
*/
public function delete( $args, $assoc_args ) {
list( $alias ) = $args;
$config = ( ! empty( $assoc_args['config'] ) ? $assoc_args['config'] : '' );
list( $config_path, $aliases ) = $this->get_aliases_data( $config, $alias );
$this->validate_config_file( $config_path );
if ( empty( $aliases[ $alias ] ) ) {
WP_CLI::error( "No alias found with key '{$alias}'." );
}
unset( $aliases[ $alias ] );
$this->process_aliases( $aliases, $alias, $config_path, 'Deleted' );
}
/**
* Updates an alias.
*
* ## OPTIONS
*
* <key>
* : Key for the alias.
*
* [--set-user=<user>]
* : Set user for alias.
*
* [--set-url=<url>]
* : Set url for alias.
*
* [--set-path=<path>]
* : Set path for alias.
*
* [--set-ssh=<ssh>]
* : Set ssh for alias.
*
* [--set-http=<http>]
* : Set http for alias.
*
* [--grouping=<grouping>]
* : For grouping multiple aliases.
*
* [--config=<config>]
* : Config file to be considered for operations.
* ---
* options:
* - global
* - project
* ---
*
* ## EXAMPLES
*
* # Update alias.
* $ wp cli alias update @prod --set-user=newuser --set-path=/new/path/to/wordpress/install/
* Success: Updated 'prod' alias.
*
* # Update project alias.
* $ wp cli alias update @prod --set-user=newuser --set-path=/new/path/to/wordpress/install/ --config=project
* Success: Updated 'prod' alias.
*
* @param array{string} $args Positional arguments.
* @param array{'set-user'?: string, 'set-url'?: string, 'set-path'?: string, 'set-ssh'?: string, 'set-http'?: string, grouping?: string, config?: string} $assoc_args Associative arguments.
*/
public function update( $args, $assoc_args ) {
$config = ( ! empty( $assoc_args['config'] ) ? $assoc_args['config'] : '' );
$alias = $args[0];
/**
* @var string|null $grouping
*/
$grouping = Utils\get_flag_value( $assoc_args, 'grouping' );
list( $config_path, $aliases ) = $this->get_aliases_data( $config, $alias, true );
$this->validate_config_file( $config_path );
$this->validate_input( $assoc_args, $grouping );
if ( empty( $aliases[ $alias ] ) ) {
WP_CLI::error( "No alias found with key '{$alias}'." );
}
if ( null === $grouping ) {
$aliases = $this->build_aliases( $aliases, $alias, $assoc_args, false, '', true );
} else {
$aliases = $this->build_aliases( $aliases, $alias, $assoc_args, true, $grouping, true );
}
$this->process_aliases( $aliases, $alias, $config_path, 'Updated' );
}
/**
* Check whether an alias is a group.
*
* ## OPTIONS
*
* <key>
* : Key for the alias.
*
* ## EXAMPLES
*
* # Checks whether the alias is a group; exit status 0 if it is, otherwise 1.
* $ wp cli alias is-group @prod
* $ echo $?
* 1
*
* @subcommand is-group
*/
public function is_group( $args, $assoc_args = array() ) {
$alias = $args[0];
$aliases = WP_CLI::get_runner()->aliases;
if ( empty( $aliases[ $alias ] ) ) {
WP_CLI::error( "No alias found with key '{$alias}'." );
}
// how do we know the alias is a group?
// + array keys are numeric
// + array values begin with '@'
$first_item = $aliases[ $alias ];
$first_item_key = key( $first_item );
$first_item_value = $first_item[ $first_item_key ];
if ( is_numeric( $first_item_key ) && substr( $first_item_value, 0, 1 ) === '@' ) {
WP_CLI::halt( 0 );
}
WP_CLI::halt( 1 );
}
/**
* Get config path and aliases data based on config type.
*
* @param string $config Type of config to get data from.
* @param string $alias Alias to be used for Add/Update/Delete.
* @param bool $create_config_file Optional. If a config file doesn't exist,
* should it be created? Defaults to false.
*
* @return array Config Path and Aliases in it.
* @throws ExitException
*/
private function get_aliases_data( $config, $alias, $create_config_file = false ) {
$global_config_path = WP_CLI::get_runner()->get_global_config_path( $create_config_file );
$global_aliases = Spyc::YAMLLoad( $global_config_path );
$project_config_path = WP_CLI::get_runner()->get_project_config_path();
$project_aliases = Spyc::YAMLLoad( $project_config_path );
if ( 'global' === $config ) {
$config_path = $global_config_path;
$aliases = $global_aliases;
} elseif ( 'project' === $config ) {
$config_path = $project_config_path;
$aliases = $project_aliases;
} else {
$is_global_alias = array_key_exists( $alias, $global_aliases );
$is_project_alias = array_key_exists( $alias, $project_aliases );
if ( $is_global_alias && $is_project_alias ) {
WP_CLI::error( "Key '{$alias}' found in more than one path. Please pass --config param." );
} elseif ( $is_global_alias ) {
$config_path = $global_config_path;
$aliases = $global_aliases;
} else {
$config_path = $project_config_path;
$aliases = $project_aliases;
}
}
return [ $config_path, $aliases ];
}
/**
* Check if the config file exists and is writable.
*
* @param string $config_path Path to config file.
*/
private function validate_config_file( $config_path ): void {
if ( ! file_exists( $config_path ) || ! is_writable( $config_path ) ) {
WP_CLI::error( "Config file does not exist: {$config_path}" );
}
}
/**
* Return aliases array.
*
* @param array $aliases Current aliases data.
* @param string $alias Name of alias.
* @param array $assoc_args Associative arguments.
* @param bool $is_grouping Check if its a grouping operation.
* @param string $grouping Grouping value.
* @param bool $is_update Is this an update operation?
*
* @return array<string, array<string, mixed>>
*/
private function build_aliases( $aliases, $alias, $assoc_args, $is_grouping, $grouping = '', $is_update = false ) {
$alias = $this->normalize_alias( $alias );
if ( $is_grouping ) {
$valid_assoc_args = [ 'config', 'grouping' ];
$invalid_args = array_diff( array_keys( $assoc_args ), $valid_assoc_args );
// Check for invalid args.
if ( ! empty( $invalid_args ) ) {
$args_info = implode( ',', $invalid_args );
WP_CLI::error( "--grouping argument works alone. Found invalid arg(s) '$args_info'." );
}
}
if ( $is_update ) {
$this->validate_alias_type( $aliases, $alias, $assoc_args, $grouping );
}
if ( ! $is_grouping ) {
foreach ( $assoc_args as $key => $value ) {
if ( strpos( $key, 'set-' ) !== false ) {
$alias_key_info = explode( '-', $key );
$alias_key = empty( $alias_key_info[1] ) ? '' : $alias_key_info[1];
if ( ! empty( $alias_key ) && ! empty( $value ) ) {
$aliases[ $alias ][ $alias_key ] = $value;
}
}
}
} elseif ( ! empty( $grouping ) ) {
$group_alias_list = explode( ',', $grouping );
$group_alias = array_map(
function ( $current_alias ) {
return '@' . ltrim( $current_alias, '@' );
},
$group_alias_list
);
$aliases[ $alias ] = $group_alias;
}
return $aliases;
}
/**
* Validate input of passed arguments.
*
* @param array $assoc_args Arguments array.
* @param string|null $grouping Grouping argument value.
*
* @throws ExitException
*/
private function validate_input( $assoc_args, $grouping ) {
// Check if valid arguments were passed.
$arg_match = (array) preg_grep( '/^set-(\w+)/i', array_keys( $assoc_args ) );
// Verify passed-arguments.
if ( empty( $grouping ) && empty( $arg_match ) ) {
WP_CLI::error( 'No valid arguments passed.' );
}
// Check whether passed arguments contain value or not.
$assoc_arg_values = array_filter( array_intersect_key( $assoc_args, array_flip( $arg_match ) ) );
if ( empty( $grouping ) && empty( $assoc_arg_values ) ) {
WP_CLI::error( 'No value passed to arguments.' );
}
}
/**
* Validate alias type before update.
*
* @param array $aliases Existing aliases data.
* @param string $alias Alias Name.
* @param array $assoc_args Arguments array.
* @param string $grouping Grouping argument value.
*
* @throws ExitException
*/
private function validate_alias_type( $aliases, $alias, $assoc_args, $grouping ) {
$alias_data = $aliases[ $alias ];
$group_aliases_match = preg_grep( '/^@(\w+)/i', $alias_data );
$arg_match = preg_grep( '/^set-(\w+)/i', array_keys( $assoc_args ) );
if ( ! empty( $group_aliases_match ) && ! empty( $arg_match ) ) {
WP_CLI::error( 'Trying to update group alias with invalid arguments.' );
} elseif ( empty( $group_aliases_match ) && ! empty( $grouping ) ) {
WP_CLI::error( 'Trying to update simple alias with invalid --grouping argument.' );
}
}
/**
* Save aliases data to config file.
*
* @param array $aliases Current aliases data.
* @param string $alias Name of alias.
* @param string $config_path Path to config file.
* @param string $operation Current operation string fro message.
*/
private function process_aliases( $aliases, $alias, $config_path, $operation = '' ) {
$alias = $this->normalize_alias( $alias );
// Convert data to YAML string.
$yaml_data = Spyc::YAMLDump( $aliases );
// Add data in config file.
if ( file_put_contents( $config_path, $yaml_data ) ) {
WP_CLI::success( "$operation '{$alias}' alias." );
}
}
/**
* Normalize the alias to an expected format.
*
* - Add @ if not present.
*
* @param string $alias Name of alias.
*/
private function normalize_alias( $alias ) {
// Check if the alias starts with the @.
// See: https://github.com/wp-cli/wp-cli/issues/5391
if ( strpos( $alias, '@' ) !== 0 ) {
$alias = '@' . ltrim( $alias, '@' );
}
return $alias;
}
}