Skip to content
Open
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
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,70 @@ There are no optionally available fields.



### wp package get

Gets information about an installed WP-CLI package.

~~~
wp package get <name> [--fields=<fields>] [--format=<format>] [--skip-update-check]
~~~

**OPTIONS**

<name>
Name of the package to get information for.

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

[--format=<format>]
Render output in a particular format.
---
default: table
options:
- table
- csv
- json
- yaml
---

[--skip-update-check]
Skip checking for updates. This is faster and avoids authentication issues with GitHub or Composer repositories.

**AVAILABLE FIELDS**

These fields will be displayed by default for each package:

* name
* authors
* version
* update
* update_version

These fields are optionally available:

* description

**EXAMPLES**

# Get information about an installed package.
$ wp package get wp-cli/scaffold-package-command *
+----------------+---------------------------------+
| Field | Value |
+----------------+---------------------------------+
| name | wp-cli/scaffold-package-command |
| authors | Daniel Bachhuber |
| version | dev-main |
| update | available |
| update_version | 2.x-dev |
+----------------+---------------------------------+

# Get the version of a package.
$ wp package get wp-cli/server-command --fields=version --format=json
{"version":"dev-main"}



### wp package install

Installs a WP-CLI package.
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"commands": [
"package",
"package browse",
"package get",
"package install",
"package list",
"package update",
Expand Down
49 changes: 49 additions & 0 deletions features/package.feature
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,52 @@ Feature: Manage WP-CLI packages

When I run `wp package uninstall runcommand/hook`
Then STDERR should be empty

Scenario: Get information about a single package
Given an empty directory

When I try `wp package get runcommand/hook`
Then STDERR should contain:
"""
Error: Package 'runcommand/hook' is not installed.
"""
And the return code should be 1

When I run `wp package install runcommand/hook`
Then STDERR should be empty

When I run `wp package get runcommand/hook`
Then STDOUT should contain:
"""
runcommand/hook
"""
And STDOUT should contain:
"""
version
"""

When I run `wp package get runcommand/hook --fields=name,version`
Then STDOUT should contain:
"""
runcommand/hook
"""
And STDOUT should contain:
"""
version
"""

When I run `wp package get runcommand/hook --fields=version --format=json`
Then STDOUT should contain:
"""
"version"
"""

When I run `wp package get runcommand/hook --format=json`
Then STDOUT should contain:
"""
"name":"runcommand\/hook"
"""
And STDOUT should contain:
"""
"version"
"""
112 changes: 112 additions & 0 deletions src/Package_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,118 @@ public function path( $args ) {
WP_CLI::line( $packages_dir );
}

/**
* Gets information about an installed WP-CLI package.
*
* ## OPTIONS
*
* <name>
* : Name of the package to get information for.
*
* [--fields=<fields>]
* : Limit the output to specific fields. Defaults to all fields.
*
* [--format=<format>]
* : Render output in a particular format.
* ---
* default: table
* options:
* - table
* - csv
* - json
* - yaml
* ---
*
* [--skip-update-check]
* : Skip checking for updates. This is faster and avoids authentication issues with GitHub or Composer repositories.
*
* ## AVAILABLE FIELDS
*
* These fields will be displayed by default for each package:
*
* * name
* * authors
* * version
* * update
* * update_version
*
* These fields are optionally available:
*
* * description
*
* ## EXAMPLES
*
* # Get information about an installed package.
* $ wp package get wp-cli/scaffold-package-command
* +----------------+---------------------------------+
* | Field | Value |
* +----------------+---------------------------------+
* | name | wp-cli/scaffold-package-command |
* | authors | Daniel Bachhuber |
* | version | dev-main |
* | update | available |
* | update_version | 2.x-dev |
* +----------------+---------------------------------+
*
* # Get the version of a package.
* $ wp package get wp-cli/server-command --fields=version --format=json
* {"version":"dev-main"}
*/
public function get( $args, $assoc_args ) {
list( $package_name ) = $args;
$this->set_composer_auth_env_var();

$package = $this->get_installed_package_by_name( $package_name );
if ( false === $package ) {
WP_CLI::error( sprintf( "Package '%s' is not installed.", $package_name ) );
}

$skip_update_check = Utils\get_flag_value( $assoc_args, 'skip-update-check', false );
$composer = $this->get_composer();

$package_output = [];
$package_output['name'] = $package->getPrettyName();
$package_output['description'] = $package->getDescription();
$package_output['authors'] = implode( ', ', array_column( (array) $package->getAuthors(), 'name' ) );
$package_output['version'] = $package->getPrettyVersion();
$update = 'none';
$update_version = '';

if ( ! $skip_update_check ) {
try {
$latest = $this->find_latest_package( $package, $composer, null );
if ( $latest && $latest->getFullPrettyVersion() !== $package->getFullPrettyVersion() ) {
$update = 'available';
$update_version = $latest->getPrettyVersion();
}
} catch ( Exception $e ) {
WP_CLI::warning( $e->getMessage() );
$update = 'error';
$update_version = $update;
}
}

$package_output['update'] = $update;
$package_output['update_version'] = $update_version;

$default_fields = [
'name',
'authors',
'version',
'update',
'update_version',
];

$defaults = [
'fields' => implode( ',', $default_fields ),
'format' => 'table',
];
$assoc_args = array_merge( $defaults, $assoc_args );

$formatter = new \WP_CLI\Formatter( $assoc_args );
$formatter->display_item( $package_output );
}

/**
* Updates all installed WP-CLI packages to their latest version.
*
Expand Down