From c68640f05cea2cf79bf2703d83da6b76fa6dc5e6 Mon Sep 17 00:00:00 2001
From: Yuchen Zeng <zyc@google.com>
Date: Thu, 7 Jul 2016 14:13:35 -0700
Subject: [PATCH] Read from stdin

Read from stdin if the request text and binary file are not provided
---
 test/cpp/util/grpc_cli.cc | 23 +++++++++++++++++------
 1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/test/cpp/util/grpc_cli.cc b/test/cpp/util/grpc_cli.cc
index 7c8e2cdc63..fdb1a7c2a0 100644
--- a/test/cpp/util/grpc_cli.cc
+++ b/test/cpp/util/grpc_cli.cc
@@ -64,6 +64,7 @@
        < output.bin > output.txt
 */
 
+#include <unistd.h>
 #include <fstream>
 #include <iostream>
 #include <sstream>
@@ -146,7 +147,6 @@ int main(int argc, char** argv) {
   grpc::string serialized_request_proto;
 
   if (argc == 5) {
-    // TODO(yangg) read from stdin as well?
     request_text = argv[4];
   }
 
@@ -164,10 +164,15 @@ int main(int argc, char** argv) {
       grpc::CreateChannel(server_address, creds);
 
   if (request_text.empty() && FLAGS_input_binary_file.empty()) {
-    std::cout << "Missing input. Use text format input or "
-              << "--input_binary_file for serialized request" << std::endl;
-    return 1;
-  } else if (!request_text.empty()) {
+    if (isatty(STDIN_FILENO)) {
+      std::cout << "reading request message from stdin..." << std::endl;
+    }
+    std::stringstream input_stream;
+    input_stream << std::cin.rdbuf();
+    request_text = input_stream.str();
+  }
+
+  if (!request_text.empty()) {
     if (!FLAGS_proto_file.empty()) {
       parser.reset(new grpc::testing::ProtoFileParser(
           FLAGS_proto_path, FLAGS_proto_file, method_name));
@@ -178,6 +183,12 @@ int main(int argc, char** argv) {
     if (parser->HasError()) {
       return 1;
     }
+
+    if (!FLAGS_input_binary_file.empty()) {
+      std::cout
+          << "warning: request given in argv, ignoring --input_binary_file"
+          << std::endl;
+    }
   }
 
   if (parser) {
@@ -226,7 +237,7 @@ int main(int argc, char** argv) {
     }
   } else {
     std::cout << "Rpc failed with status code " << s.error_code()
-              << " error message " << s.error_message() << std::endl;
+              << ", error message: " << s.error_message() << std::endl;
   }
 
   return 0;
-- 
GitLab