diff --git a/README.md b/README.md index 120790a2..c0db44a3 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,70 @@ There are no optionally available fields. +### wp package get + +Gets information about an installed WP-CLI package. + +~~~ +wp package get [--fields=] [--format=] [--skip-update-check] +~~~ + +**OPTIONS** + + + Name of the package to get information for. + + [--fields=] + Limit the output to specific fields. Defaults to all fields. + + [--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. diff --git a/composer.json b/composer.json index 9b53625d..944f7dcf 100644 --- a/composer.json +++ b/composer.json @@ -38,6 +38,7 @@ "commands": [ "package", "package browse", + "package get", "package install", "package list", "package update", diff --git a/features/package.feature b/features/package.feature index 28af2053..8f56065d 100644 --- a/features/package.feature +++ b/features/package.feature @@ -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" + """ diff --git a/src/Package_Command.php b/src/Package_Command.php index e6f8273c..bae5c31e 100644 --- a/src/Package_Command.php +++ b/src/Package_Command.php @@ -511,6 +511,118 @@ public function path( $args ) { WP_CLI::line( $packages_dir ); } + /** + * Gets information about an installed WP-CLI package. + * + * ## OPTIONS + * + * + * : Name of the package to get information for. + * + * [--fields=] + * : Limit the output to specific fields. Defaults to all fields. + * + * [--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. *