OSDN Git Service

Allow `wp post create` to read from file or STDIN
authorgoldenapples <goldenapplesdesign@gmail.com>
Fri, 15 Feb 2013 00:18:59 +0000 (16:18 -0800)
committergoldenapples <goldenapplesdesign@gmail.com>
Fri, 15 Feb 2013 18:49:30 +0000 (10:49 -0800)
If a filename is passed as the first argument to `wp post create`, that
file will be read for the post's content. If the first argument is '-',
then post content will be read from STDIN.

If an unreadable filename is given, `wp post create` exits with an
error.

php/commands/post.php

index 625f330..fe79507 100644 (file)
@@ -12,9 +12,17 @@ class Post_Command extends \WP_CLI\CommandWithDBObject {
        /**
         * Create a post.
         *
-        * @synopsis --<field>=<value> [--edit] [--porcelain]
+        * @synopsis [<file>] --<field>=<value> [--porcelain]
         */
-       public function create( $_, $assoc_args ) {
+       public function create( $args, $assoc_args ) {
+               if ( ! empty( $args[0] ) ) {
+                       $readfile = ( $args[0] === '-' ) ? 'php://stdin' : $args[0];
+
+                       if ( ! file_exists( $readfile ) || ! is_file( $readfile ) )
+                                \WP_CLI::error( "Unable to read content from $readfile." );
+
+                       $assoc_args['post_content'] = file_get_contents( $readfile );
+               }
                if ( isset( $assoc_args['edit'] ) ) {
 
                        $input = ( isset( $assoc_args['post_content'] ) ) ?
@@ -26,7 +34,6 @@ class Post_Command extends \WP_CLI\CommandWithDBObject {
                                $assoc_args['post_content'] = $input;
 
                }
-
                parent::create( $assoc_args );
        }