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
186 changes: 186 additions & 0 deletions features/config.feature
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,192 @@ Feature: Have a config file
Then STDOUT should not be empty
And the return code should be 0

Scenario: Environment variables configured in wp-cli.yml - WP_CLI_CACHE_DIR
Given an empty directory
And a wp-cli.yml file:
"""
env:
WP_CLI_CACHE_DIR: /tmp/custom-cache
"""

When I run `wp cli info --format=json`
Then STDOUT should contain:
"""
\/tmp\/custom-cache
"""

Scenario: Environment variables configured in wp-cli.yml - WP_CLI_PACKAGES_DIR
Given an empty directory
And an empty custom-packages directory
And a wp-cli.yml file:
"""
env:
WP_CLI_PACKAGES_DIR: ./custom-packages
"""

When I run `wp cli info --format=json`
Then STDOUT should contain:
"""
\/custom-packages
"""

Scenario: Actual environment variables take precedence over config
Given an empty directory
And a wp-cli.yml file:
"""
env:
WP_CLI_CACHE_DIR: /tmp/from-config
"""

When I run `WP_CLI_CACHE_DIR=/tmp/from-env wp cli info --format=json`
Then STDOUT should contain:
"""
\/tmp\/from-env
"""
And STDOUT should not contain:
"""
from-config
"""

Scenario: Environment variables configured in wp-cli.yml - WP_CLI_CACHE_EXPIRY
Given an empty directory
And a test-cache-config.php file:
"""
<?php
WP_CLI::add_command( 'test-cache-config', function() {
$expiry = WP_CLI\Utils\get_env_or_config('WP_CLI_CACHE_EXPIRY');
WP_CLI::log( 'Cache expiry: ' . $expiry );
}, array( 'when' => 'before_wp_load' ) );
"""
And a wp-cli.yml file:
"""
env:
WP_CLI_CACHE_EXPIRY: 7200
"""

When I run `wp --require=test-cache-config.php test-cache-config`
Then STDOUT should contain:
"""
Cache expiry: 7200
"""

Scenario: Environment variables configured in wp-cli.yml - WP_CLI_CACHE_MAX_SIZE
Given an empty directory
And a test-cache-config.php file:
"""
<?php
WP_CLI::add_command( 'test-cache-config', function() {
$max_size = WP_CLI\Utils\get_env_or_config('WP_CLI_CACHE_MAX_SIZE');
WP_CLI::log( 'Cache max size: ' . $max_size );
}, array( 'when' => 'before_wp_load' ) );
"""
And a wp-cli.yml file:
"""
env:
WP_CLI_CACHE_MAX_SIZE: 209715200
"""

When I run `wp --require=test-cache-config.php test-cache-config`
Then STDOUT should contain:
"""
Cache max size: 209715200
"""

Scenario: WP_CLI_CACHE_EXPIRY and WP_CLI_CACHE_MAX_SIZE with environment variable precedence
Given an empty directory
And a test-cache-config.php file:
"""
<?php
WP_CLI::add_command( 'test-cache-config', function() {
$expiry = WP_CLI\Utils\get_env_or_config('WP_CLI_CACHE_EXPIRY');
$max_size = WP_CLI\Utils\get_env_or_config('WP_CLI_CACHE_MAX_SIZE');
WP_CLI::log( 'Cache expiry: ' . $expiry );
WP_CLI::log( 'Cache max size: ' . $max_size );
}, array( 'when' => 'before_wp_load' ) );
"""
And a wp-cli.yml file:
"""
env:
WP_CLI_CACHE_EXPIRY: 3600
WP_CLI_CACHE_MAX_SIZE: 104857600
"""

When I run `WP_CLI_CACHE_EXPIRY=7200 wp --require=test-cache-config.php test-cache-config`
Then STDOUT should contain:
"""
Cache expiry: 7200
"""
And STDOUT should contain:
"""
Cache max size: 104857600
"""

Scenario: Environment variables configured in wp-cli.yml - WP_CLI_SKIP_PROMPT
Given an empty directory
And a test-skip-prompt.php file:
"""
<?php
WP_CLI::add_command( 'test-skip-prompt', function() {
$skip_prompt = WP_CLI\Utils\get_env_or_config('WP_CLI_SKIP_PROMPT');
WP_CLI::log( 'WP_CLI_SKIP_PROMPT: ' . $skip_prompt );
}, array( 'when' => 'before_wp_load' ) );
"""
And a wp-cli.yml file:
"""
env:
WP_CLI_SKIP_PROMPT: "yes"
"""

When I run `wp --require=test-skip-prompt.php test-skip-prompt`
Then STDOUT should contain:
"""
WP_CLI_SKIP_PROMPT: yes
"""

Scenario: Environment variables configured in wp-cli.yml - WP_CLI_AUTO_UPDATE_PROMPT
Given an empty directory
And a test-auto-update.php file:
"""
<?php
WP_CLI::add_command( 'test-auto-update', function() {
$auto_update = WP_CLI\Utils\get_env_or_config('WP_CLI_AUTO_UPDATE_PROMPT');
WP_CLI::log( 'WP_CLI_AUTO_UPDATE_PROMPT: ' . $auto_update );
}, array( 'when' => 'before_wp_load' ) );
"""
And a wp-cli.yml file:
"""
env:
WP_CLI_AUTO_UPDATE_PROMPT: "no"
"""

When I run `wp --require=test-auto-update.php test-auto-update`
Then STDOUT should contain:
"""
WP_CLI_AUTO_UPDATE_PROMPT: no
"""

Scenario: Environment variables configured in wp-cli.yml - WP_CLI_AUTOCORRECT
Given an empty directory
And a test-autocorrect.php file:
"""
<?php
WP_CLI::add_command( 'test-autocorrect', function() {
$autocorrect = WP_CLI\Utils\get_env_or_config('WP_CLI_AUTOCORRECT');
WP_CLI::log( 'WP_CLI_AUTOCORRECT: ' . $autocorrect );
}, array( 'when' => 'before_wp_load' ) );
"""
And a wp-cli.yml file:
"""
env:
WP_CLI_AUTOCORRECT: "1"
"""

When I run `wp --require=test-autocorrect.php test-autocorrect`
Then STDOUT should contain:
"""
WP_CLI_AUTOCORRECT: 1
"""

Scenario: Custom system config path via WP_CLI_SYSTEM_SETTINGS_PATH
Given an empty directory
And a system-config.yml file:
Expand Down
2 changes: 1 addition & 1 deletion php/WP_CLI/Dispatcher/CompositeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ protected function get_global_params( $root_command = false ) {
}

// Check if global parameters synopsis should be displayed or not.
if ( 'true' !== getenv( 'WP_CLI_SUPPRESS_GLOBAL_PARAMS' ) ) {
if ( 'true' !== Utils\get_env_or_config( 'WP_CLI_SUPPRESS_GLOBAL_PARAMS' ) ) {
$binding['parameters'][] = [
'synopsis' => $synopsis,
'desc' => $details['desc'],
Expand Down
26 changes: 13 additions & 13 deletions php/WP_CLI/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ static function ( $dir ) {
* @return string
*/
public function get_packages_dir_path() {
$packages_dir = (string) getenv( 'WP_CLI_PACKAGES_DIR' );
$packages_dir = (string) Utils\get_env_or_config( 'WP_CLI_PACKAGES_DIR' );
if ( $packages_dir ) {
$packages_dir = Utils\trailingslashit( $packages_dir );
} else {
Expand Down Expand Up @@ -606,7 +606,7 @@ public function run_command( $args, $assoc_args = [], $options = [] ) {
if ( ! empty( $options['back_compat_conversions'] ) ) {
list( $args, $assoc_args ) = self::back_compat_conversions( $args, $assoc_args );
}
$r = $this->find_command_to_run( $args, getenv( 'WP_CLI_AUTOCORRECT' ) ? 'auto' : 'confirm' );
$r = $this->find_command_to_run( $args, Utils\get_env_or_config( 'WP_CLI_AUTOCORRECT' ) ? 'auto' : 'confirm' );
if ( is_string( $r ) ) {
WP_CLI::error( $r );
}
Expand Down Expand Up @@ -679,7 +679,7 @@ private function run_ssh_command( string $connection_string ): void {

$bits = Utils\parse_ssh_url( $connection_string );

$pre_cmd = getenv( 'WP_CLI_SSH_PRE_CMD' );
$pre_cmd = Utils\get_env_or_config( 'WP_CLI_SSH_PRE_CMD' );
if ( $pre_cmd ) {
WP_CLI::warning( "WP_CLI_SSH_PRE_CMD found, executing the following command(s) on the remote machine:\n $pre_cmd" );

Expand All @@ -691,7 +691,7 @@ private function run_ssh_command( string $connection_string ): void {
$env_vars .= 'WP_CLI_STRICT_ARGS_MODE=1 ';
}

$wp_binary = getenv( 'WP_CLI_SSH_BINARY' ) ?: 'wp';
$wp_binary = Utils\get_env_or_config( 'WP_CLI_SSH_BINARY' ) ?: 'wp';
$wp_args = array_slice( (array) $GLOBALS['argv'], 1 );

if ( $this->alias && ! empty( $wp_args[0] ) && ( '@' . $this->alias === $wp_args[0] || "--alias={$this->alias}" === $wp_args[0] ) ) {
Expand Down Expand Up @@ -811,8 +811,8 @@ private function generate_ssh_command( $bits, $wp_command ) {
$ssh_args ? $ssh_args . ' ' : '',
$bits['user'] ? '--user ' . escapeshellarg( $bits['user'] ) . ' ' : '',
$bits['path'] ? '--workdir ' . escapeshellarg( $bits['path'] ) . ' ' : '',
$is_stdout_tty && ! getenv( 'WP_CLI_DOCKER_NO_TTY' ) ? '-t ' : '',
$is_stdin_tty || getenv( 'WP_CLI_DOCKER_NO_INTERACTIVE' ) ? '' : '-i ',
$is_stdout_tty && ! Utils\get_env_or_config( 'WP_CLI_DOCKER_NO_TTY' ) ? '-t ' : '',
$is_stdin_tty || Utils\get_env_or_config( 'WP_CLI_DOCKER_NO_INTERACTIVE' ) ? '' : '-i ',
escapeshellarg( $bits['host'] ),
escapeshellarg( $wp_command )
);
Expand All @@ -827,7 +827,7 @@ private function generate_ssh_command( $bits, $wp_command ) {
$ssh_args ? $ssh_args . ' ' : '',
$bits['user'] ? '--user ' . escapeshellarg( $bits['user'] ) . ' ' : '',
$bits['path'] ? '--workdir ' . escapeshellarg( $bits['path'] ) . ' ' : '',
$is_stdout_tty || getenv( 'WP_CLI_DOCKER_NO_TTY' ) ? '' : '-T ',
$is_stdout_tty || Utils\get_env_or_config( 'WP_CLI_DOCKER_NO_TTY' ) ? '' : '-T ',
escapeshellarg( $bits['host'] ),
escapeshellarg( $wp_command )
);
Expand All @@ -842,8 +842,8 @@ private function generate_ssh_command( $bits, $wp_command ) {
$ssh_args ? $ssh_args . ' ' : '',
$bits['user'] ? '--user ' . escapeshellarg( $bits['user'] ) . ' ' : '',
$bits['path'] ? '--workdir ' . escapeshellarg( $bits['path'] ) . ' ' : '',
$is_stdout_tty || getenv( 'WP_CLI_DOCKER_NO_TTY' ) ? '' : '-T ',
$is_stdin_tty || getenv( 'WP_CLI_DOCKER_NO_INTERACTIVE' ) ? '' : '-i ',
$is_stdout_tty || Utils\get_env_or_config( 'WP_CLI_DOCKER_NO_TTY' ) ? '' : '-T ',
$is_stdin_tty || Utils\get_env_or_config( 'WP_CLI_DOCKER_NO_INTERACTIVE' ) ? '' : '-i ',
escapeshellarg( $bits['host'] ),
$wp_command
);
Expand Down Expand Up @@ -1388,7 +1388,7 @@ function ( $value ) use ( $alias_regex ) {
$runtime_config = Utils\assoc_args_to_str( $filtered_runtime_config );

// Check if parallel execution is enabled via environment variable.
$parallel = (bool) getenv( 'WP_CLI_ALIAS_GROUPS_PARALLEL' );
$parallel = (bool) Utils\get_env_or_config( 'WP_CLI_ALIAS_GROUPS_PARALLEL' );

if ( $parallel ) {
// Run aliases in parallel.
Expand Down Expand Up @@ -2317,12 +2317,12 @@ private function auto_check_update(): void {
}

// Allow hosts and other providers to disable automatic check update.
if ( getenv( 'WP_CLI_DISABLE_AUTO_CHECK_UPDATE' ) ) {
if ( Utils\get_env_or_config( 'WP_CLI_DISABLE_AUTO_CHECK_UPDATE' ) ) {
return;
}

// Permit configuration of number of days between checks.
$days_between_checks = getenv( 'WP_CLI_AUTO_CHECK_UPDATE_DAYS' );
$days_between_checks = Utils\get_env_or_config( 'WP_CLI_AUTO_CHECK_UPDATE_DAYS' );
if ( false === $days_between_checks ) {
$days_between_checks = 1;
}
Expand Down Expand Up @@ -2360,7 +2360,7 @@ private function auto_check_update(): void {
// Looks like an update is available, so let's prompt to update.
$update_args = [];
// Allow skipping the confirmation prompt via environment variable.
if ( getenv( 'WP_CLI_AUTO_UPDATE_PROMPT' ) === 'no' ) {
if ( Utils\get_env_or_config( 'WP_CLI_AUTO_UPDATE_PROMPT' ) === 'no' ) {
$update_args['yes'] = true;
}

Expand Down
5 changes: 3 additions & 2 deletions php/WP_CLI/ShutdownHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace WP_CLI;

use WP_CLI;
use WP_CLI\Utils;

/**
* Handles fatal errors to detect plugin/theme issues and suggest workarounds.
Expand Down Expand Up @@ -221,7 +222,7 @@ private static function extract_theme_slug( $file, $theme_dir ) {
private static function should_prompt_rerun() {
// Check environment variable WP_CLI_SKIP_PROMPT
// If set to 'yes', automatically rerun; if 'no', don't prompt
$skip_prompt = getenv( 'WP_CLI_SKIP_PROMPT' );
$skip_prompt = Utils\get_env_or_config( 'WP_CLI_SKIP_PROMPT' );

if ( false !== $skip_prompt ) {
return 'yes' !== $skip_prompt && 'no' !== $skip_prompt;
Expand All @@ -238,7 +239,7 @@ private static function should_prompt_rerun() {
*/
private static function prompt_and_rerun( $skip ) {
// Get environment variable to check default behavior
$skip_prompt = getenv( 'WP_CLI_SKIP_PROMPT' );
$skip_prompt = Utils\get_env_or_config( 'WP_CLI_SKIP_PROMPT' );

// If set to 'yes', automatically rerun without prompting
if ( 'yes' === $skip_prompt ) {
Expand Down
4 changes: 2 additions & 2 deletions php/class-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ public static function get_cache() {

if ( ! $cache ) {
$dir = Utils\get_cache_dir();
$ttl = (int) getenv( 'WP_CLI_CACHE_EXPIRY' ) ? : 15552000;
$max_size = (int) getenv( 'WP_CLI_CACHE_MAX_SIZE' ) ? : 314572800;
$ttl = (int) Utils\get_env_or_config( 'WP_CLI_CACHE_EXPIRY' ) ? : 15552000;
$max_size = (int) Utils\get_env_or_config( 'WP_CLI_CACHE_MAX_SIZE' ) ? : 314572800;
// 6 months, 300mb
$cache = new FileCache( $dir, $ttl, $max_size );

Expand Down
4 changes: 2 additions & 2 deletions php/commands/src/Help_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Help_Command extends WP_CLI_Command {
* @param string[] $args
*/
public function __invoke( $args ) {
$r = WP_CLI::get_runner()->find_command_to_run( $args, getenv( 'WP_CLI_AUTOCORRECT' ) ? 'auto' : 'confirm' );
$r = WP_CLI::get_runner()->find_command_to_run( $args, Utils\get_env_or_config( 'WP_CLI_AUTOCORRECT' ) ? 'auto' : 'confirm' );

if ( is_array( $r ) ) {
list( $command ) = $r;
Expand Down Expand Up @@ -208,7 +208,7 @@ private static function pass_through_pager( $out ) {
// For Windows 7 need to set code page to something other than Unicode (65001) to get around "Not enough memory." error with `more.com` on PHP 7.1+.
if ( 'more' === $pager && defined( 'PHP_WINDOWS_VERSION_MAJOR' ) && PHP_WINDOWS_VERSION_MAJOR < 10 ) {
// Note will also apply to Windows 8 (see https://msdn.microsoft.com/en-us/library/windows/desktop/ms724832.aspx) but probably harmless anyway.
$cp = (int) getenv( 'WP_CLI_WINDOWS_CODE_PAGE' ) ?: 1252; // Code page 1252 is the most used so probably the most compat.
$cp = (int) Utils\get_env_or_config( 'WP_CLI_WINDOWS_CODE_PAGE' ) ?: 1252; // Code page 1252 is the most used so probably the most compat.
sapi_windows_cp_set( $cp );
}

Expand Down
30 changes: 28 additions & 2 deletions php/utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -2287,14 +2287,40 @@ function get_sql_modes() {
return $sql_modes;
}

/**
* Get an environment variable value, with config file fallback.
*
* Checks the actual environment variable first, then falls back to
* values defined in the 'env' configuration key in wp-cli.yml.
*
* @param string $name Environment variable name.
* @return string|false The value of the environment variable, or false if not set.
*/
function get_env_or_config( $name ) {
$env_value = getenv( $name );
if ( false !== $env_value ) {
return $env_value;
}

// Try to get from config file
$runner = WP_CLI::get_runner();
if ( $runner && isset( $runner->extra_config['env'] ) && is_array( $runner->extra_config['env'] ) && isset( $runner->extra_config['env'][ $name ] ) ) {
// @phpstan-ignore cast.string
return (string) $runner->extra_config['env'][ $name ];
}

return false;
}

/**
* Get the WP-CLI cache directory.
*
* @return string
*/
function get_cache_dir() {
$home = get_home_dir();
return getenv( 'WP_CLI_CACHE_DIR' ) ? : "$home/.wp-cli/cache";
$home = get_home_dir();
$cache_dir = get_env_or_config( 'WP_CLI_CACHE_DIR' );
return $cache_dir ? : "$home/.wp-cli/cache";
}

/**
Expand Down
Loading
Loading