Read from stdin

Read from stdin if the request text and binary file are not provided
pull/7269/head
Yuchen Zeng 9 years ago
parent 39070fe3a5
commit c68640f05c
  1. 23
      test/cpp/util/grpc_cli.cc

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

Loading…
Cancel
Save