Skip to content

Commit 8b2f89d

Browse files
authored
Merge pull request #33 from villuorav/custom-bin-bash
Add possibility to change the shell binary
2 parents 35aaf57 + c2281cd commit 8b2f89d

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

features/shell.feature

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,24 @@ Feature: WordPress REPL
4040
bool(true)
4141
"""
4242
43+
Scenario: Use custom shell path
44+
Given a WP install
45+
46+
And a session file:
47+
"""
48+
return true;
49+
"""
50+
51+
When I try `WP_CLI_CUSTOM_SHELL=/nonsense/path wp shell --basic < session`
52+
Then STDOUT should be empty
53+
And STDERR should contain:
54+
"""
55+
Error: The shell binary '/nonsense/path' is not valid.
56+
"""
57+
58+
When I try `WP_CLI_CUSTOM_SHELL=/bin/bash wp shell --basic < session`
59+
Then STDOUT should contain:
60+
"""
61+
bool(true)
62+
"""
63+
And STDERR should be empty

src/WP_CLI/REPL.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace WP_CLI;
44

5+
use WP_CLI;
6+
57
class REPL {
68

79
private $prompt;
@@ -95,6 +97,17 @@ private function prompt() {
9597
private static function create_prompt_cmd( $prompt, $history_path ) {
9698
$prompt = escapeshellarg( $prompt );
9799
$history_path = escapeshellarg( $history_path );
100+
if ( getenv( 'WP_CLI_CUSTOM_SHELL' ) ) {
101+
$shell_binary = getenv( 'WP_CLI_CUSTOM_SHELL' );
102+
} else {
103+
$shell_binary = '/bin/bash';
104+
}
105+
106+
if ( ! is_file( $shell_binary ) || ! is_readable( $shell_binary ) ) {
107+
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." );
108+
}
109+
110+
$shell_binary = escapeshellarg( $shell_binary );
98111

99112
$cmd = "set -f; "
100113
. "history -r $history_path; "
@@ -105,7 +118,7 @@ private static function create_prompt_cmd( $prompt, $history_path ) {
105118
. "history -w $history_path; "
106119
. "echo \$LINE; ";
107120

108-
return '/bin/bash -c ' . escapeshellarg( $cmd );
121+
return "{$shell_binary} -c " . escapeshellarg( $cmd );
109122
}
110123

111124
private function set_history_file() {

0 commit comments

Comments
 (0)