Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 1 | /* |
Julien Boeuf | 5be92a3 | 2015-08-28 16:28:18 -0700 | [diff] [blame] | 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 19 | /* |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 20 | A command line tool to talk to a grpc server. |
Yuchen Zeng | 2826327 | 2016-07-25 14:38:07 -0700 | [diff] [blame] | 21 | Run `grpc_cli help` command to see its usage information. |
| 22 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 23 | Example of talking to grpc interop server: |
Yuchen Zeng | 2826327 | 2016-07-25 14:38:07 -0700 | [diff] [blame] | 24 | grpc_cli call localhost:50051 UnaryCall "response_size:10" \ |
| 25 | --protofiles=src/proto/grpc/testing/test.proto --enable_ssl=false |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 26 | |
| 27 | Options: |
Yuchen Zeng | 02139a0 | 2016-08-15 11:34:21 -0700 | [diff] [blame] | 28 | 1. --protofiles, use this flag to provide proto files if the server does |
Yuchen Zeng | 2826327 | 2016-07-25 14:38:07 -0700 | [diff] [blame] | 29 | does not have the reflection service. |
| 30 | 2. --proto_path, if your proto file is not under current working directory, |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 31 | use this flag to provide a search root. It should work similar to the |
Yuchen Zeng | 2826327 | 2016-07-25 14:38:07 -0700 | [diff] [blame] | 32 | counterpart in protoc. This option is valid only when protofiles is |
| 33 | provided. |
| 34 | 3. --metadata specifies metadata to be sent to the server, such as: |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 35 | --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2" |
Yuchen Zeng | 2826327 | 2016-07-25 14:38:07 -0700 | [diff] [blame] | 36 | 4. --enable_ssl, whether to use tls. |
| 37 | 5. --use_auth, if set to true, attach a GoogleDefaultCredentials to the call |
Yuchen Zeng | 02139a0 | 2016-08-15 11:34:21 -0700 | [diff] [blame] | 38 | 6. --infile, input filename (defaults to stdin) |
| 39 | 7. --outfile, output filename (defaults to stdout) |
| 40 | 8. --binary_input, use the serialized request as input. The serialized |
| 41 | request can be generated by calling something like: |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 42 | protoc --proto_path=src/proto/grpc/testing/ \ |
| 43 | --encode=grpc.testing.SimpleRequest \ |
| 44 | src/proto/grpc/testing/messages.proto \ |
| 45 | < input.txt > input.bin |
| 46 | If this is used and no proto file is provided in the argument list, the |
| 47 | method string has to be exact in the form of /package.service/method. |
Yuchen Zeng | 02139a0 | 2016-08-15 11:34:21 -0700 | [diff] [blame] | 48 | 9. --binary_output, use binary format response as output, it can |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 49 | be later decoded using protoc: |
| 50 | protoc --proto_path=src/proto/grpc/testing/ \ |
| 51 | --decode=grpc.testing.SimpleResponse \ |
| 52 | src/proto/grpc/testing/messages.proto \ |
| 53 | < output.bin > output.txt |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 54 | */ |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 55 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 56 | #include <fstream> |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 57 | #include <functional> |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 58 | #include <iostream> |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 59 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 60 | #include <gflags/gflags.h> |
Vijay Pai | c90a856 | 2018-03-08 21:20:24 -0800 | [diff] [blame] | 61 | #include <grpcpp/support/config.h> |
Yuchen Zeng | 02139a0 | 2016-08-15 11:34:21 -0700 | [diff] [blame] | 62 | #include "test/cpp/util/cli_credentials.h" |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 63 | #include "test/cpp/util/grpc_tool.h" |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 64 | #include "test/cpp/util/test_config.h" |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 65 | |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 66 | DEFINE_string(outfile, "", "Output file (default is stdout)"); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 67 | |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 68 | static bool SimplePrint(const grpc::string& outfile, |
| 69 | const grpc::string& output) { |
| 70 | if (outfile.empty()) { |
Yuchen Zeng | d37f642 | 2016-09-09 20:05:37 -0700 | [diff] [blame] | 71 | std::cout << output << std::endl; |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 72 | } else { |
Yuchen Zeng | 8d2d70c | 2016-09-16 15:42:57 -0700 | [diff] [blame] | 73 | std::ofstream output_file(outfile, std::ios::app | std::ios::binary); |
Yuchen Zeng | d37f642 | 2016-09-09 20:05:37 -0700 | [diff] [blame] | 74 | output_file << output << std::endl; |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 75 | output_file.close(); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 76 | } |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 77 | return true; |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 78 | } |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 79 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 80 | int main(int argc, char** argv) { |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame] | 81 | grpc::testing::InitTest(&argc, &argv, true); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 82 | |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 83 | return grpc::testing::GrpcToolMainLib( |
Yuchen Zeng | 02139a0 | 2016-08-15 11:34:21 -0700 | [diff] [blame] | 84 | argc, (const char**)argv, grpc::testing::CliCredentials(), |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 85 | std::bind(SimplePrint, FLAGS_outfile, std::placeholders::_1)); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 86 | } |