From 70acd51cfc1bb88c4c01144f830ba03c3031d4de Mon Sep 17 00:00:00 2001 From: Villu Orav Date: Fri, 2 Nov 2018 16:24:14 +0200 Subject: [PATCH 1/6] Add possibility to change the shell binary --- src/WP_CLI/REPL.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/WP_CLI/REPL.php b/src/WP_CLI/REPL.php index bd95c4fed..2bb83873f 100644 --- a/src/WP_CLI/REPL.php +++ b/src/WP_CLI/REPL.php @@ -91,6 +91,11 @@ private function prompt() { private static function create_prompt_cmd( $prompt, $history_path ) { $prompt = escapeshellarg( $prompt ); $history_path = escapeshellarg( $history_path ); + if ( getenv( 'WP_CLI_CUSTOM_SHELL' ) ) { + $bash_path = escapeshellarg(getenv( 'WP_CLI_CUSTOM_SHELL' )); + } else { + $bash_path = '/bin/bash'; + } $cmd = "set -f; " . "history -r $history_path; " @@ -101,7 +106,7 @@ private static function create_prompt_cmd( $prompt, $history_path ) { . "history -w $history_path; " . "echo \$LINE; "; - return '/bin/bash -c ' . escapeshellarg( $cmd ); + return $bash_path . ' -c ' . escapeshellarg( $cmd ); } private function set_history_file() { From 16056ea553fbfc17b12ce9a3be5b742d3eb16c28 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 25 Feb 2019 19:38:58 +0200 Subject: [PATCH 2/6] Update src/WP_CLI/REPL.php Co-Authored-By: villuorav --- src/WP_CLI/REPL.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/WP_CLI/REPL.php b/src/WP_CLI/REPL.php index 2bb83873f..cc81be964 100644 --- a/src/WP_CLI/REPL.php +++ b/src/WP_CLI/REPL.php @@ -92,7 +92,7 @@ private static function create_prompt_cmd( $prompt, $history_path ) { $prompt = escapeshellarg( $prompt ); $history_path = escapeshellarg( $history_path ); if ( getenv( 'WP_CLI_CUSTOM_SHELL' ) ) { - $bash_path = escapeshellarg(getenv( 'WP_CLI_CUSTOM_SHELL' )); + $shell_binary = escapeshellarg(getenv( 'WP_CLI_CUSTOM_SHELL' )); } else { $bash_path = '/bin/bash'; } From e647921aa2d38b3f04878d8487c749cf68bc0e10 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 25 Feb 2019 19:42:14 +0200 Subject: [PATCH 3/6] Apply suggestions from code review Co-Authored-By: villuorav --- src/WP_CLI/REPL.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/WP_CLI/REPL.php b/src/WP_CLI/REPL.php index cc81be964..5515299bc 100644 --- a/src/WP_CLI/REPL.php +++ b/src/WP_CLI/REPL.php @@ -94,7 +94,7 @@ private static function create_prompt_cmd( $prompt, $history_path ) { if ( getenv( 'WP_CLI_CUSTOM_SHELL' ) ) { $shell_binary = escapeshellarg(getenv( 'WP_CLI_CUSTOM_SHELL' )); } else { - $bash_path = '/bin/bash'; + $shell_binary = '/bin/bash'; } $cmd = "set -f; " @@ -106,7 +106,7 @@ private static function create_prompt_cmd( $prompt, $history_path ) { . "history -w $history_path; " . "echo \$LINE; "; - return $bash_path . ' -c ' . escapeshellarg( $cmd ); + return "{$shell_binary} -c " . escapeshellarg( $cmd ); } private function set_history_file() { From 2da4095d6dedb3076e5b1759fd9d6d11ec0113b3 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 1 Apr 2019 15:25:21 +0200 Subject: [PATCH 4/6] Add test --- features/shell.feature | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/features/shell.feature b/features/shell.feature index 03dd6cb3e..881454fe6 100644 --- a/features/shell.feature +++ b/features/shell.feature @@ -40,3 +40,24 @@ Feature: WordPress REPL bool(true) """ + Scenario: Use custom shell path + Given a WP install + + And a session file: + """ + return true; + """ + + When I try `WP_CLI_CUSTOM_SHELL=/nonsense/path wp shell --basic < session` + Then STDOUT should be empty + And STDERR should contain: + """ + Error: The shell binary '/nonsense/path' is not valid. + """ + + When I try `WP_CLI_CUSTOM_SHELL=/bin/sh wp shell --basic < session` + Then STDOUT should contain: + """ + bool(true) + """ + And STDERR should be empty From b8e1f4387206c627bca251325a673031b7f1b32e Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 1 Apr 2019 15:25:32 +0200 Subject: [PATCH 5/6] Add error checking --- src/WP_CLI/REPL.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/WP_CLI/REPL.php b/src/WP_CLI/REPL.php index 5515299bc..671d129e1 100644 --- a/src/WP_CLI/REPL.php +++ b/src/WP_CLI/REPL.php @@ -2,6 +2,8 @@ namespace WP_CLI; +use WP_CLI; + class REPL { private $prompt; @@ -92,11 +94,17 @@ private static function create_prompt_cmd( $prompt, $history_path ) { $prompt = escapeshellarg( $prompt ); $history_path = escapeshellarg( $history_path ); if ( getenv( 'WP_CLI_CUSTOM_SHELL' ) ) { - $shell_binary = escapeshellarg(getenv( 'WP_CLI_CUSTOM_SHELL' )); + $shell_binary = getenv( 'WP_CLI_CUSTOM_SHELL' ); } else { $shell_binary = '/bin/bash'; } + if ( ! is_file( $shell_binary ) || ! is_readable( $shell_binary ) ) { + WP_CLI::error( "The shell binary '{$shell_binary}' is not valid. You can override the shell to be used through the WP_CLI_CUSTOM_SHELL environment variable." ); + } + + $shell_binary = escapeshellarg( $shell_binary ); + $cmd = "set -f; " . "history -r $history_path; " . "LINE=\"\"; " From c2281cd10ad45ab90e13e60afb2677317a8c4908 Mon Sep 17 00:00:00 2001 From: Alain Schlesser Date: Mon, 1 Apr 2019 15:35:21 +0200 Subject: [PATCH 6/6] Use /bin/bash, as Travis does not provide anything else --- features/shell.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/features/shell.feature b/features/shell.feature index 881454fe6..53fa0d5cd 100644 --- a/features/shell.feature +++ b/features/shell.feature @@ -55,7 +55,7 @@ Feature: WordPress REPL Error: The shell binary '/nonsense/path' is not valid. """ - When I try `WP_CLI_CUSTOM_SHELL=/bin/sh wp shell --basic < session` + When I try `WP_CLI_CUSTOM_SHELL=/bin/bash wp shell --basic < session` Then STDOUT should contain: """ bool(true)