Skip to content

Commit 88cbe48

Browse files
authored
Merge pull request #6057 from drzraf/4972-docker-stdin
docker scheme: working-directory and stdin passing (reroll of #5974)
2 parents f139c1e + 31b2dc3 commit 88cbe48

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

features/flags.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ Feature: Global flags
355355
"""
356356

357357
Scenario: SSH flag should support Docker
358-
When I try `wp --debug --ssh=docker:user@wordpress --version`
358+
When I try `WP_CLI_DOCKER_NO_INTERACTIVE=1 wp --debug --ssh=docker:user@wordpress --version`
359359
Then STDERR should contain:
360360
"""
361361
Running SSH command: docker exec --user 'user' 'wordpress' sh -c

php/WP_CLI/Runner.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -503,9 +503,6 @@ private function run_ssh_command( $connection_string ) {
503503

504504
$pre_cmd = rtrim( $pre_cmd, ';' ) . '; ';
505505
}
506-
if ( ! empty( $bits['path'] ) ) {
507-
$pre_cmd .= 'cd ' . escapeshellarg( $bits['path'] ) . '; ';
508-
}
509506

510507
$env_vars = '';
511508
if ( getenv( 'WP_CLI_STRICT_ARGS_MODE' ) ) {
@@ -575,50 +572,67 @@ private function generate_ssh_command( $bits, $wp_command ) {
575572
WP_CLI::debug( 'SSH ' . $bit . ': ' . $bits[ $bit ], 'bootstrap' );
576573
}
577574

578-
$is_tty = function_exists( 'posix_isatty' ) && posix_isatty( STDOUT );
575+
/*
576+
* posix_isatty(STDIN) is generally true unless something was passed on stdin
577+
* If autodetection leads to false (fd on stdin), then `-i` is passed to `docker` cmd
578+
* (unless WP_CLI_DOCKER_NO_INTERACTIVE is set)
579+
*/
580+
$is_stdout_tty = function_exists( 'posix_isatty' ) && posix_isatty( STDOUT );
581+
$is_stdin_tty = function_exists( 'posix_isatty' ) ? posix_isatty( STDIN ) : true;
582+
579583
$docker_compose_v2_version_cmd = Utils\esc_cmd( Utils\force_env_on_nix_systems( 'docker' ) . ' compose %s', 'version' );
580584
$docker_compose_cmd = ! empty( Process::create( $docker_compose_v2_version_cmd )->run()->stdout )
581585
? 'docker compose'
582586
: 'docker-compose';
583587

584588
if ( 'docker' === $bits['scheme'] ) {
585-
$command = 'docker exec %s%s%s sh -c %s';
589+
$command = 'docker exec %s%s%s%s%s sh -c %s';
586590

587591
$escaped_command = sprintf(
588592
$command,
589593
$bits['user'] ? '--user ' . escapeshellarg( $bits['user'] ) . ' ' : '',
590-
$is_tty ? '-t ' : '',
594+
$bits['path'] ? '--workdir ' . escapeshellarg( $bits['path'] ) . ' ' : '',
595+
$is_stdout_tty && ! getenv( 'WP_CLI_DOCKER_NO_TTY' ) ? '-t ' : '',
596+
$is_stdin_tty || getenv( 'WP_CLI_DOCKER_NO_INTERACTIVE' ) ? '' : '-i ',
591597
escapeshellarg( $bits['host'] ),
592598
escapeshellarg( $wp_command )
593599
);
594600
}
595601

596602
if ( 'docker-compose' === $bits['scheme'] ) {
597-
$command = '%s exec %s%s%s sh -c %s';
603+
$command = '%s exec %s%s%s%s sh -c %s';
598604

599605
$escaped_command = sprintf(
600606
$command,
601607
$docker_compose_cmd,
602608
$bits['user'] ? '--user ' . escapeshellarg( $bits['user'] ) . ' ' : '',
603-
$is_tty ? '' : '-T ',
609+
$bits['path'] ? '--workdir ' . escapeshellarg( $bits['path'] ) . ' ' : '',
610+
$is_stdout_tty || getenv( 'WP_CLI_DOCKER_NO_TTY' ) ? '' : '-T ',
604611
escapeshellarg( $bits['host'] ),
605612
escapeshellarg( $wp_command )
606613
);
607614
}
608615

609616
if ( 'docker-compose-run' === $bits['scheme'] ) {
610-
$command = '%s run %s%s%s %s';
617+
$command = '%s run %s%s%s%s%s %s';
611618

612619
$escaped_command = sprintf(
613620
$command,
614621
$docker_compose_cmd,
615622
$bits['user'] ? '--user ' . escapeshellarg( $bits['user'] ) . ' ' : '',
616-
$is_tty ? '' : '-T ',
623+
$bits['path'] ? '--workdir ' . escapeshellarg( $bits['path'] ) . ' ' : '',
624+
$is_stdout_tty || getenv( 'WP_CLI_DOCKER_NO_TTY' ) ? '' : '-T ',
625+
$is_stdin_tty || getenv( 'WP_CLI_DOCKER_NO_INTERACTIVE' ) ? '' : '-i ',
617626
escapeshellarg( $bits['host'] ),
618627
$wp_command
619628
);
620629
}
621630

631+
// For "vagrant" & "ssh" schemes which don't provide a working-directory option, use `cd`
632+
if ( $bits['path'] ) {
633+
$wp_command = 'cd ' . escapeshellarg( $bits['path'] ) . '; ' . $wp_command;
634+
}
635+
622636
// Vagrant ssh-config.
623637
if ( 'vagrant' === $bits['scheme'] ) {
624638
$cache = WP_CLI::get_cache();
@@ -675,7 +689,7 @@ private function generate_ssh_command( $bits, $wp_command ) {
675689
$bits['proxyjump'] ? sprintf( '-J %s', escapeshellarg( $bits['proxyjump'] ) ) : '',
676690
$bits['port'] ? sprintf( '-p %d', (int) $bits['port'] ) : '',
677691
$bits['key'] ? sprintf( '-i %s', escapeshellarg( $bits['key'] ) ) : '',
678-
$is_tty ? '-t' : '-T',
692+
$is_stdout_tty ? '-t' : '-T',
679693
WP_CLI::get_config( 'debug' ) ? '-vvv' : '-q',
680694
];
681695

0 commit comments

Comments
 (0)