Skip to content

Commit 2058cff

Browse files
authored
Merge pull request #212 from wp-cli/copilot/add-package-version-command
Add wp package get command to retrieve single package information
2 parents 1446020 + 5559514 commit 2058cff

4 files changed

Lines changed: 242 additions & 1 deletion

File tree

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,70 @@ There are no optionally available fields.
125125

126126

127127

128+
### wp package get
129+
130+
Gets information about an installed WP-CLI package.
131+
132+
~~~
133+
wp package get <name> [--fields=<fields>] [--format=<format>] [--skip-update-check]
134+
~~~
135+
136+
**OPTIONS**
137+
138+
<name>
139+
Name of the package to get information for.
140+
141+
[--fields=<fields>]
142+
Limit the output to specific fields. Defaults to all fields.
143+
144+
[--format=<format>]
145+
Render output in a particular format.
146+
---
147+
default: table
148+
options:
149+
- table
150+
- csv
151+
- json
152+
- yaml
153+
---
154+
155+
[--skip-update-check]
156+
Skip checking for updates. This is faster and avoids authentication issues with GitHub or Composer repositories.
157+
158+
**AVAILABLE FIELDS**
159+
160+
These fields will be displayed by default for each package:
161+
162+
* name
163+
* authors
164+
* version
165+
* update
166+
* update_version
167+
168+
These fields are optionally available:
169+
170+
* description
171+
172+
**EXAMPLES**
173+
174+
# Get information about an installed package.
175+
$ wp package get wp-cli/scaffold-package-command
176+
+----------------+---------------------------------+
177+
| Field | Value |
178+
+----------------+---------------------------------+
179+
| name | wp-cli/scaffold-package-command |
180+
| authors | Daniel Bachhuber |
181+
| version | dev-main |
182+
| update | available |
183+
| update_version | 2.x-dev |
184+
+----------------+---------------------------------+
185+
186+
# Get the version of a package.
187+
$ wp package get wp-cli/server-command --fields=version --format=json
188+
{"version":"dev-main"}
189+
190+
191+
128192
### wp package install
129193

130194
Installs a WP-CLI package.

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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,69 @@ Feature: Manage WP-CLI packages
231231

232232
When I run `wp package uninstall runcommand/hook`
233233
Then STDERR should be empty
234+
235+
Scenario: Get information about a single package
236+
Given an empty directory
237+
238+
When I try `wp package get runcommand/hook`
239+
Then STDERR should contain:
240+
"""
241+
Error: Package 'runcommand/hook' is not installed.
242+
"""
243+
And the return code should be 1
244+
245+
When I run `wp package install runcommand/hook`
246+
Then STDERR should be empty
247+
248+
When I run `wp package get runcommand/hook`
249+
Then STDOUT should contain:
250+
"""
251+
runcommand/hook
252+
"""
253+
And STDOUT should contain:
254+
"""
255+
version
256+
"""
257+
258+
When I run `wp package get runcommand/hook --fields=name,version`
259+
Then STDOUT should contain:
260+
"""
261+
runcommand/hook
262+
"""
263+
And STDOUT should contain:
264+
"""
265+
version
266+
"""
267+
268+
When I run `wp package get runcommand/hook --fields=version --format=json`
269+
Then STDOUT should contain:
270+
"""
271+
"version"
272+
"""
273+
274+
When I run `wp package get runcommand/hook --format=json`
275+
Then STDOUT should contain:
276+
"""
277+
"name":"runcommand\/hook"
278+
"""
279+
And STDOUT should contain:
280+
"""
281+
"version"
282+
"""
283+
284+
When I run `wp package get runcommand/hook --skip-update-check --fields=name,update,update_version`
285+
Then STDOUT should contain:
286+
"""
287+
runcommand/hook
288+
"""
289+
And STDOUT should contain:
290+
"""
291+
none
292+
"""
293+
And STDOUT should not contain:
294+
"""
295+
available
296+
"""
297+
298+
When I run `wp package uninstall runcommand/hook`
299+
Then STDERR should be empty

src/Package_Command.php

Lines changed: 111 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,10 +507,120 @@ public function path( $args ) {
507507
}
508508

509509
/**
510-
* Updates installed WP-CLI packages to their latest version.
510+
* Gets information about an installed WP-CLI package.
511511
*
512512
* ## OPTIONS
513513
*
514+
* <name>
515+
* : Name of the package to get information for.
516+
*
517+
* [--fields=<fields>]
518+
* : Limit the output to specific fields. Defaults to all fields.
519+
*
520+
* [--format=<format>]
521+
* : Render output in a particular format.
522+
* ---
523+
* default: table
524+
* options:
525+
* - table
526+
* - csv
527+
* - json
528+
* - yaml
529+
* ---
530+
*
531+
* [--skip-update-check]
532+
* : Skip checking for updates. This is faster and avoids authentication issues with GitHub or Composer repositories.
533+
*
534+
* ## AVAILABLE FIELDS
535+
*
536+
* These fields will be displayed by default for each package:
537+
*
538+
* * name
539+
* * authors
540+
* * version
541+
* * update
542+
* * update_version
543+
*
544+
* These fields are optionally available:
545+
*
546+
* * description
547+
*
548+
* ## EXAMPLES
549+
*
550+
* # Get information about an installed package.
551+
* $ wp package get wp-cli/scaffold-package-command
552+
* +----------------+---------------------------------+
553+
* | Field | Value |
554+
* +----------------+---------------------------------+
555+
* | name | wp-cli/scaffold-package-command |
556+
* | authors | Daniel Bachhuber |
557+
* | version | dev-main |
558+
* | update | available |
559+
* | update_version | 2.x-dev |
560+
* +----------------+---------------------------------+
561+
*
562+
* # Get the version of a package.
563+
* $ wp package get wp-cli/server-command --fields=version --format=json
564+
* {"version":"dev-main"}
565+
*/
566+
public function get( $args, $assoc_args ) {
567+
list( $package_name ) = $args;
568+
$this->set_composer_auth_env_var();
569+
570+
$package = $this->get_installed_package_by_name( $package_name );
571+
if ( false === $package ) {
572+
WP_CLI::error( sprintf( "Package '%s' is not installed.", $package_name ) );
573+
}
574+
575+
$skip_update_check = Utils\get_flag_value( $assoc_args, 'skip-update-check', false );
576+
$composer = $this->get_composer();
577+
578+
$package_output = [];
579+
$package_output['name'] = $package->getPrettyName();
580+
$package_output['description'] = $package->getDescription();
581+
$package_output['authors'] = implode( ', ', array_column( (array) $package->getAuthors(), 'name' ) );
582+
$package_output['version'] = $package->getPrettyVersion();
583+
$update = 'none';
584+
$update_version = '';
585+
586+
if ( ! $skip_update_check ) {
587+
try {
588+
$latest = $this->find_latest_package( $package, $composer );
589+
if ( $latest && $latest->getFullPrettyVersion() !== $package->getFullPrettyVersion() ) {
590+
$update = 'available';
591+
$update_version = $latest->getPrettyVersion();
592+
}
593+
} catch ( Exception $e ) {
594+
WP_CLI::warning( $e->getMessage() );
595+
$update = 'error';
596+
$update_version = $update;
597+
}
598+
}
599+
600+
$package_output['update'] = $update;
601+
$package_output['update_version'] = $update_version;
602+
603+
$default_fields = [
604+
'name',
605+
'authors',
606+
'version',
607+
'update',
608+
'update_version',
609+
];
610+
611+
$defaults = [
612+
'fields' => implode( ',', $default_fields ),
613+
'format' => 'table',
614+
];
615+
$assoc_args = array_merge( $defaults, $assoc_args );
616+
617+
$formatter = new \WP_CLI\Formatter( $assoc_args );
618+
$formatter->display_item( $package_output );
619+
}
620+
621+
/**
622+
* Updates installed WP-CLI packages to their latest version.
623+
*
514624
* [<package-name>...]
515625
* : One or more package names to update. If not specified, all packages will be updated.
516626
*

0 commit comments

Comments
 (0)