Skip to content

Commit 42e3337

Browse files
Copilotswissspidy
andcommitted
Add wp package get command implementation
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 8cf6b15 commit 42e3337

3 files changed

Lines changed: 153 additions & 0 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"commands": [
3939
"package",
4040
"package browse",
41+
"package get",
4142
"package install",
4243
"package list",
4344
"package update",

features/package.feature

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,52 @@ Feature: Manage WP-CLI packages
208208
"""
209209
{NO_SUCH_PACKAGE_COMPOSER_JSON}
210210
"""
211+
212+
Scenario: Get information about a single package
213+
Given an empty directory
214+
215+
When I try `wp package get runcommand/hook`
216+
Then STDERR should contain:
217+
"""
218+
Error: Package 'runcommand/hook' is not installed.
219+
"""
220+
And the return code should be 1
221+
222+
When I run `wp package install runcommand/hook`
223+
Then STDERR should be empty
224+
225+
When I run `wp package get runcommand/hook`
226+
Then STDOUT should contain:
227+
"""
228+
runcommand/hook
229+
"""
230+
And STDOUT should contain:
231+
"""
232+
version
233+
"""
234+
235+
When I run `wp package get runcommand/hook --fields=name,version`
236+
Then STDOUT should contain:
237+
"""
238+
runcommand/hook
239+
"""
240+
And STDOUT should contain:
241+
"""
242+
version
243+
"""
244+
245+
When I run `wp package get runcommand/hook --fields=version --format=json`
246+
Then STDOUT should contain:
247+
"""
248+
"version"
249+
"""
250+
251+
When I run `wp package get runcommand/hook --format=json`
252+
Then STDOUT should contain:
253+
"""
254+
"name":"runcommand/hook"
255+
"""
256+
And STDOUT should contain:
257+
"""
258+
"version"
259+
"""

src/Package_Command.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,109 @@ public function path( $args ) {
500500
WP_CLI::line( $packages_dir );
501501
}
502502

503+
/**
504+
* Gets information about an installed WP-CLI package.
505+
*
506+
* ## OPTIONS
507+
*
508+
* <name>
509+
* : Name of the package to get information for.
510+
*
511+
* [--fields=<fields>]
512+
* : Limit the output to specific fields. Defaults to all fields.
513+
*
514+
* [--format=<format>]
515+
* : Render output in a particular format.
516+
* ---
517+
* default: table
518+
* options:
519+
* - table
520+
* - csv
521+
* - json
522+
* - yaml
523+
* ---
524+
*
525+
* ## AVAILABLE FIELDS
526+
*
527+
* These fields will be displayed by default for each package:
528+
*
529+
* * name
530+
* * authors
531+
* * version
532+
* * update
533+
* * update_version
534+
*
535+
* These fields are optionally available:
536+
*
537+
* * description
538+
*
539+
* ## EXAMPLES
540+
*
541+
* # Get information about an installed package.
542+
* $ wp package get wp-cli/server-command
543+
* +---------+------------------+
544+
* | Field | Value |
545+
* +---------+------------------+
546+
* | name | wp-cli/server-command |
547+
* | authors | Daniel Bachhuber |
548+
* | version | dev-main |
549+
* +---------+------------------+
550+
*
551+
* # Get the version of a package.
552+
* $ wp package get wp-cli/server-command --fields=version --format=json
553+
* {"version":"dev-main"}
554+
*/
555+
public function get( $args, $assoc_args ) {
556+
list( $package_name ) = $args;
557+
$this->set_composer_auth_env_var();
558+
559+
$package = $this->get_installed_package_by_name( $package_name );
560+
if ( false === $package ) {
561+
WP_CLI::error( sprintf( "Package '%s' is not installed.", $package_name ) );
562+
}
563+
564+
$composer = $this->get_composer();
565+
566+
$package_output = [];
567+
$package_output['name'] = $package->getPrettyName();
568+
$package_output['description'] = $package->getDescription();
569+
$package_output['authors'] = implode( ', ', array_column( (array) $package->getAuthors(), 'name' ) );
570+
$package_output['version'] = $package->getPrettyVersion();
571+
$update = 'none';
572+
$update_version = '';
573+
574+
try {
575+
$latest = $this->find_latest_package( $package, $composer, null );
576+
if ( $latest && $latest->getFullPrettyVersion() !== $package->getFullPrettyVersion() ) {
577+
$update = 'available';
578+
$update_version = $latest->getPrettyVersion();
579+
}
580+
} catch ( Exception $e ) {
581+
WP_CLI::warning( $e->getMessage() );
582+
$update = 'error';
583+
$update_version = $update;
584+
}
585+
586+
$package_output['update'] = $update;
587+
$package_output['update_version'] = $update_version;
588+
589+
$default_fields = [
590+
'name',
591+
'authors',
592+
'version',
593+
'update',
594+
'update_version',
595+
];
596+
597+
$defaults = [
598+
'fields' => implode( ',', $default_fields ),
599+
'format' => 'table',
600+
];
601+
$assoc_args = array_merge( $defaults, $assoc_args );
602+
603+
Utils\format_items( $assoc_args['format'], [ $package_output ], $assoc_args['fields'] );
604+
}
605+
503606
/**
504607
* Updates all installed WP-CLI packages to their latest version.
505608
*

0 commit comments

Comments
 (0)