From a5392f8c9e1b32fcedcedb3dfe0383f3a18a1aa7 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 15 Jul 2016 15:11:41 -0700 Subject: [PATCH] Permit defining aliases at runtime This lets us pass the current alias through over SSH. I wish there was a better way to do this, but I think this is the best we have. I suppose it will let users with lots of WordPress installs to define aliases dynamically, instead of needing to write a file. --- php/WP_CLI/Configurator.php | 36 ++++++++++++++++++++++++++++-------- php/WP_CLI/Runner.php | 11 +++++++++++ 2 files changed, 39 insertions(+), 8 deletions(-) diff --git a/php/WP_CLI/Configurator.php b/php/WP_CLI/Configurator.php index 6c0e852f04..f259528d14 100644 --- a/php/WP_CLI/Configurator.php +++ b/php/WP_CLI/Configurator.php @@ -34,6 +34,17 @@ class Configurator { */ const ALIAS_REGEX = '^@[A-Za-z0-9-_]+$'; + /** + * @var array ALIAS_SPEC Arguments that can be used in an alias + */ + private static $alias_spec = array( + 'user', + 'url', + 'path', + 'ssh', + 'http', + ); + /** * @param string $path Path to config spec file. */ @@ -79,7 +90,22 @@ function get_spec() { * @return array */ function get_aliases() { - return $this->aliases; + if ( $runtime_alias = getenv( 'WP_CLI_RUNTIME_ALIAS' ) ) { + $returned_aliases = array(); + foreach( json_decode( $runtime_alias, true ) as $key => $value ) { + if ( preg_match( '#' . self::ALIAS_REGEX . '#', $key ) ) { + $returned_aliases[ $key ] = array(); + foreach( self::$alias_spec as $i ) { + if ( isset( $value[ $i ] ) ) { + $returned_aliases[ $key ][ $i ] = $value[ $i ]; + } + } + } + } + return $returned_aliases; + } else { + return $this->aliases; + } } /** @@ -196,13 +222,7 @@ public function merge_yml( $path ) { if ( preg_match( '#' . self::ALIAS_REGEX . '#', $key ) ) { $this->aliases[ $key ] = array(); $is_alias = false; - foreach( array( - 'user', - 'url', - 'path', - 'ssh', - 'http', - ) as $i ) { + foreach( self::$alias_spec as $i ) { if ( isset( $value[ $i ] ) ) { $this->aliases[ $key ][ $i ] = $value[ $i ]; $is_alias = true; diff --git a/php/WP_CLI/Runner.php b/php/WP_CLI/Runner.php index d526256f3a..3e0024d5e7 100644 --- a/php/WP_CLI/Runner.php +++ b/php/WP_CLI/Runner.php @@ -348,6 +348,17 @@ private function run_ssh_command( $ssh ) { if ( $this->alias && ! empty( $wp_args[0] ) && $this->alias === $wp_args[0] ) { array_shift( $wp_args ); + $runtime_alias = array(); + foreach( $this->aliases[ $this->alias ] as $key => $value ) { + if ( 'ssh' === $key ) { + continue; + } + $runtime_alias[ $key ] = $value; + } + if ( ! empty( $runtime_alias ) ) { + $encoded_alias = json_encode( array( $this->alias => $runtime_alias ) ); + $wp_binary = "WP_CLI_RUNTIME_ALIAS='{$encoded_alias}' {$wp_binary} {$this->alias}"; + } } foreach( $wp_args as $k => $v ) {