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

Restructure config value retrieval #42

Merged
merged 12 commits into from
Jan 25, 2018
71 changes: 62 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,52 @@ the database constants are correct.

### wp config get

Gets variables, constants, and file includes defined in wp-config.php file.
Gets the value of a specific variable or constant defined in wp-config.php

~~~
wp config get [--fields=<fields>] [--constant=<constant>] [--global=<global>] [--format=<format>]
wp config get <key> [--type=<type>]
~~~

file.

**OPTIONS**

[--fields=<fields>]
Limit the output to specific fields. Defaults to all fields.
<key>
Key for the wp-config.php variable or constant.

[--type=<type>]
Type of config value to retrieve. Defaults to 'all'.
---
default: all
options:
- constant
- variable
- all
---

**EXAMPLES**

# Get the table_prefix as defined in wp-config.php file.
$ wp config get table_prefix
wp_


[--constant=<constant>]
Returns the value of a specific constant defined in the wp-config.php file.

[--global=<global>]
Returns the value of a specific global defined in the wp-config.php file.
### wp config list

Lists variables, constants, and file includes defined in wp-config.php file.

~~~
wp config list [<filter>...] [--fields=<fields>] [--format=<format>] [--strict]
~~~

**OPTIONS**

[<filter>...]
Key or partial key to filter the list by.

[--fields=<fields>]
Limit the output to specific fields. Defaults to all fields.

[--format=<format>]
Render output in a particular format.
Expand All @@ -134,10 +164,13 @@ wp config get [--fields=<fields>] [--constant=<constant>] [--global=<global>] [-
- yaml
---

[--strict]
Enforce strict matching when a filter is provided.

**EXAMPLES**

# List variables and constants defined in wp-config.php file.
$ wp config get --format=table
$ wp config list
+------------------+------------------------------------------------------------------+----------+
| key | value | type |
+------------------+------------------------------------------------------------------+----------+
Expand All @@ -149,6 +182,26 @@ wp config get [--fields=<fields>] [--constant=<constant>] [--global=<global>] [-
| SECURE_AUTH_KEY | iO-z!_m--YH$Tx2tf/&V,YW*13Z_HiRLqi)d?$o-tMdY+82pK$`T.NYW~iTLW;xp | constant |
+------------------+------------------------------------------------------------------+----------+

# List only database user and password from wp-config.php file.
$ wp config list DB_USER DB_PASSWORD --strict
+------------------+-------+----------+
| key | value | type |
+------------------+-------+----------+
| DB_USER | root | constant |
| DB_PASSWORD | root | constant |
+------------------+-------+----------+

# List all salts from wp-config.php file.
$ wp config list _SALT
+------------------+------------------------------------------------------------------+----------+
| key | value | type |
+------------------+------------------------------------------------------------------+----------+
| AUTH_SALT | n:]Xditk+_7>Qi=>BmtZHiH-6/Ecrvl(V5ceeGP:{>?;BT^=[B3-0>,~F5z$(+Q$ | constant |
| SECURE_AUTH_SALT | ?Z/p|XhDw3w}?c.z%|+BAr|(Iv*H%%U+Du&kKR y?cJOYyRVRBeB[2zF-`(>+LCC | constant |
| LOGGED_IN_SALT | +$@(1{b~Z~s}Cs>8Y]6[m6~TnoCDpE>O%e75u}&6kUH!>q:7uM4lxbB6[1pa_X,q | constant |
| NONCE_SALT | _x+F li|QL?0OSQns1_JZ{|Ix3Jleox-71km/gifnyz8kmo=w-;@AE8W,(fP<N}2 | constant |
+------------------+------------------------------------------------------------------+----------+



### wp config path
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"config",
"config create",
"config get",
"config list",
"config path"
]
}
Expand Down
120 changes: 98 additions & 22 deletions features/config-get-field.feature
Original file line number Diff line number Diff line change
@@ -1,58 +1,90 @@
Feature: Get the value of a constant or global defined in wp-config.php file
Feature: Get the value of a constant or variable defined in wp-config.php file

Background:
Given a WP install

Scenario: Get the value of an existing wp-config.php constant
When I try `wp config get --constant=DB_NAME`
When I try `wp config get DB_NAME --type=constant`
Then STDOUT should be:
"""
wp_cli_test
"""
And STDERR should be empty

Scenario: Get the value of an existing wp-config.php global
When I try `wp config get --global=table_prefix`
Scenario: Get the value of an existing wp-config.php constant without explicit type
When I try `wp config get DB_NAME`
Then STDOUT should be:
"""
wp_cli_test
"""
And STDERR should be empty

Scenario: Get the value of an existing wp-config.php variable
When I try `wp config get table_prefix --type=variable`
Then STDOUT should be:
"""
wp_
"""
And STDERR should be empty

Scenario: Get the value of an existing wp-config.php variable without explicit type
When I try `wp config get table_prefix`
Then STDOUT should be:
"""
wp_
"""
And STDERR should be empty

Scenario: Get the value of a non existing wp-config.php key
When I try `wp config get FOO`
Then STDERR should be:
"""
Error: The 'FOO' variable or constant is not defined in the wp-config.php file.
"""
And STDOUT should be empty

Scenario: Get the value of a non existing wp-config.php constant
When I try `wp config get --constant=FOO`
When I try `wp config get FOO --type=constant`
Then STDERR should be:
"""
Error: The 'FOO' constant is not defined in the wp-config.php file.
"""
And STDOUT should be empty

Scenario: Get the value of a non existing wp-config.php global
When I try `wp config get --global=foo`
Scenario: Get the value of a non existing wp-config.php variable
When I try `wp config get foo --type=variable`
Then STDERR should be:
"""
Error: The 'foo' global is not defined in the wp-config.php file.
Error: The 'foo' variable is not defined in the wp-config.php file.
"""
And STDOUT should be empty

Scenario: Get the value of an existing wp-config.php constant with wrong case should yield an error
When I try `wp config get --constant=db_name`
When I try `wp config get db_name --type=constant`
Then STDERR should be:
"""
Error: The 'db_name' constant is not defined in the wp-config.php file.
"""
And STDOUT should be empty

Scenario: Get the value of an existing wp-config.php global with wrong case should yield an error
When I try `wp config get --global=TABLE_PREFIX`
Scenario: Get the value of an existing wp-config.php variable with wrong case should yield an error
When I try `wp config get TABLE_PREFIX --type=variable`
Then STDERR should be:
"""
Error: The 'TABLE_PREFIX' variable is not defined in the wp-config.php file.
"""
And STDOUT should be empty

Scenario: Get the value of an existing wp-config.php key with wrong case should yield an error
When I try `wp config get TABLE_PREFIX`
Then STDERR should be:
"""
Error: The 'TABLE_PREFIX' global is not defined in the wp-config.php file.
Error: The 'TABLE_PREFIX' variable or constant is not defined in the wp-config.php file.
"""
And STDOUT should be empty

Scenario: Get the value of an existing wp-config.php constant with some similarity should yield a helpful error
When I try `wp config get --constant=DB_NOME`
When I try `wp config get DB_NOME --type=constant`
Then STDERR should be:
"""
Error: The 'DB_NOME' constant is not defined in the wp-config.php file.
Expand All @@ -61,34 +93,78 @@ Feature: Get the value of a constant or global defined in wp-config.php file
And STDOUT should be empty

Scenario: Get the value of an existing wp-config.php constant with some similarity should yield a helpful error
When I try `wp config get --global=table_perfix`
When I try `wp config get table_perfix --type=variable`
Then STDERR should be:
"""
Error: The 'table_perfix' global is not defined in the wp-config.php file.
Error: The 'table_perfix' variable is not defined in the wp-config.php file.
Did you mean 'table_prefix'?
"""
And STDOUT should be empty

Scenario: Get the value of an existing wp-config.php key with some similarity should yield a helpful error
When I try `wp config get DB_NOME`
Then STDERR should be:
"""
Error: The 'DB_NOME' variable or constant is not defined in the wp-config.php file.
Did you mean 'DB_NAME'?
"""
And STDOUT should be empty

Scenario: Get the value of an existing wp-config.php constant with remote similarity should yield just an error
When I try `wp config get --constant=DB_NOOOOZLE`
When I try `wp config get DB_NOOOOZLE --type=constant`
Then STDERR should be:
"""
Error: The 'DB_NOOOOZLE' constant is not defined in the wp-config.php file.
"""
And STDOUT should be empty

Scenario: Get the value of an existing wp-config.php global with remote similarity should yield just an error
When I try `wp config get --global=tabre_peffix`
Scenario: Get the value of an existing wp-config.php variable with remote similarity should yield just an error
When I try `wp config get tabre_peffix --type=variable`
Then STDERR should be:
"""
Error: The 'tabre_peffix' global is not defined in the wp-config.php file.
Error: The 'tabre_peffix' variable is not defined in the wp-config.php file.
"""
And STDOUT should be empty

Scenario: Trying to get the value of a constant and a global should yield an error
When I try `wp config get --constant=DB_NAME --global=table_prefix`
Scenario: Get the value of an existing wp-config.php key with remote similarity should yield just an error
When I try `wp config get DB_NOOOOZLE`
Then STDERR should be:
"""
Error: Cannot request the value of a constant and a global at the same time.
Error: The 'DB_NOOOOZLE' variable or constant is not defined in the wp-config.php file.
"""
And STDOUT should be empty

Scenario: Get the value of a key that exists as both a variable and a constant should yield a helpful error
Given a wp-config.php file:
"""
$SOMEKEY = 'value-a';
define( 'SOMEKEY', 'value-b' );
require_once( ABSPATH . 'wp-settings.php' );
"""

When I run `wp config list --format=table`
Then STDOUT should be a table containing rows:
| key | value | type |
| SOMEKEY | value-a | variable |
| SOMEKEY | value-b | constant |
And STDERR should be empty

When I try `wp config get SOMEKEY`
Then STDERR should be:
"""
Error: Found multiple values for 'SOMEKEY' in the wp-config.php file. Use --type=<type> to disambiguate.
"""

When I run `wp config get SOMEKEY --type=variable`
Then STDOUT should be:
"""
value-a
"""
And STDERR should be empty

When I run `wp config get SOMEKEY --type=constant`
Then STDOUT should be:
"""
value-b
"""
And STDERR should be empty
Loading