Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to change the shell binary #33

Merged
merged 6 commits into from Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions features/shell.feature
Expand Up @@ -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
15 changes: 14 additions & 1 deletion src/WP_CLI/REPL.php
Expand Up @@ -2,6 +2,8 @@

namespace WP_CLI;

use WP_CLI;

class REPL {

private $prompt;
Expand Down Expand Up @@ -91,6 +93,17 @@ 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' ) ) {
$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; "
Expand All @@ -101,7 +114,7 @@ private static function create_prompt_cmd( $prompt, $history_path ) {
. "history -w $history_path; "
. "echo \$LINE; ";

return '/bin/bash -c ' . escapeshellarg( $cmd );
return "{$shell_binary} -c " . escapeshellarg( $cmd );
}

private function set_history_file() {
Expand Down