Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ private function run_command_and_exit( $help_exit_warning = '' ) {

/**
* Perform a command against a remote server over SSH (or a container using
* scheme of "docker" or "docker-compose").
* scheme of "docker", "docker-compose", or "docker-compose-run").
*
* @param string $connection_string Passed connection string.
* @return void
Expand Down Expand Up @@ -512,7 +512,12 @@ private function run_ssh_command( $connection_string ) {
}
}

$wp_command = $pre_cmd . $env_vars . $wp_binary . ' ' . implode( ' ', array_map( 'escapeshellarg', $wp_args ) );
$wp_command = $pre_cmd . $env_vars . $wp_binary . ' ' . implode( ' ', array_map( 'escapeshellarg', $wp_args ) );

if ( isset( $bits['scheme'] ) && 'docker-compose-run' === $bits['scheme'] ) {
$wp_command = implode( ' ', $wp_args );
}

$escaped_command = $this->generate_ssh_command( $bits, $wp_command );

passthru( $escaped_command, $exit_code );
Expand Down Expand Up @@ -568,6 +573,18 @@ private function generate_ssh_command( $bits, $wp_command ) {
);
}

if ( 'docker-compose-run' === $bits['scheme'] ) {
$command = 'docker-compose run %s%s%s %s';

$escaped_command = sprintf(
$command,
$bits['user'] ? '--user ' . escapeshellarg( $bits['user'] ) . ' ' : '',
$is_tty ? '' : '-T ',
escapeshellarg( $bits['host'] ),
$wp_command
);
}

// Vagrant ssh-config.
if ( 'vagrant' === $bits['scheme'] ) {
$cache = WP_CLI::get_cache();
Expand Down
2 changes: 1 addition & 1 deletion php/config-spec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
'ssh' => [
'runtime' => '=[<scheme>:][<user>@]<host|container>[:<port>][<path>]',
'file' => '[<scheme>:][<user>@]<host|container>[:<port>][<path>]',
'desc' => 'Perform operation against a remote server over SSH (or a container using scheme of "docker", "docker-compose", "vagrant").',
'desc' => 'Perform operation against a remote server over SSH (or a container using scheme of "docker", "docker-compose", "docker-compose-run", "vagrant").',
],

'http' => [
Expand Down
2 changes: 1 addition & 1 deletion php/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,7 @@ function get_temp_dir() {
* @return mixed
*/
function parse_ssh_url( $url, $component = -1 ) {
preg_match( '#^((docker|docker\-compose|ssh|vagrant):)?(([^@:]+)@)?([^:/~]+)(:([\d]*))?((/|~)(.+))?$#', $url, $matches );
preg_match( '#^((docker|docker\-compose|docker\-compose\-run|ssh|vagrant):)?(([^@:]+)@)?([^:/~]+)(:([\d]*))?((/|~)(.+))?$#', $url, $matches );
$bits = [];
foreach ( [
2 => 'scheme',
Expand Down
15 changes: 15 additions & 0 deletions tests/test-utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,21 @@ public function testParseSSHUrl() {
$this->assertEquals( null, Utils\parse_ssh_url( $testcase, PHP_URL_PORT ) );
$this->assertEquals( '~/path/to/dir', Utils\parse_ssh_url( $testcase, PHP_URL_PATH ) );

// container scheme with user, host, and path
$testcase = 'docker-compose-run:bar@wordpress:~/path/to/dir';
$expected = [
'scheme' => 'docker-compose-run',
'user' => 'bar',
'host' => 'wordpress',
'path' => '~/path/to/dir',
];
$this->assertEquals( $expected, Utils\parse_ssh_url( $testcase ) );
$this->assertEquals( 'docker-compose-run', Utils\parse_ssh_url( $testcase, PHP_URL_SCHEME ) );
$this->assertEquals( 'bar', Utils\parse_ssh_url( $testcase, PHP_URL_USER ) );
$this->assertEquals( 'wordpress', Utils\parse_ssh_url( $testcase, PHP_URL_HOST ) );
$this->assertEquals( null, Utils\parse_ssh_url( $testcase, PHP_URL_PORT ) );
$this->assertEquals( '~/path/to/dir', Utils\parse_ssh_url( $testcase, PHP_URL_PATH ) );

// vagrant scheme
$testcase = 'vagrant:default';
$expected = [
Expand Down