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 | |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 34 | /* |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 35 | A command line tool to talk to a grpc server. |
Yuchen Zeng | 2826327 | 2016-07-25 14:38:07 -0700 | [diff] [blame] | 36 | Run `grpc_cli help` command to see its usage information. |
| 37 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 38 | Example of talking to grpc interop server: |
Yuchen Zeng | 2826327 | 2016-07-25 14:38:07 -0700 | [diff] [blame] | 39 | grpc_cli call localhost:50051 UnaryCall "response_size:10" \ |
| 40 | --protofiles=src/proto/grpc/testing/test.proto --enable_ssl=false |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 41 | |
| 42 | Options: |
Yuchen Zeng | 02139a0 | 2016-08-15 11:34:21 -0700 | [diff] [blame^] | 43 | 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] | 44 | does not have the reflection service. |
| 45 | 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] | 46 | 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] | 47 | counterpart in protoc. This option is valid only when protofiles is |
| 48 | provided. |
| 49 | 3. --metadata specifies metadata to be sent to the server, such as: |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 50 | --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2" |
Yuchen Zeng | 2826327 | 2016-07-25 14:38:07 -0700 | [diff] [blame] | 51 | 4. --enable_ssl, whether to use tls. |
| 52 | 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^] | 53 | 6. --infile, input filename (defaults to stdin) |
| 54 | 7. --outfile, output filename (defaults to stdout) |
| 55 | 8. --binary_input, use the serialized request as input. The serialized |
| 56 | request can be generated by calling something like: |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 57 | protoc --proto_path=src/proto/grpc/testing/ \ |
| 58 | --encode=grpc.testing.SimpleRequest \ |
| 59 | src/proto/grpc/testing/messages.proto \ |
| 60 | < input.txt > input.bin |
| 61 | If this is used and no proto file is provided in the argument list, the |
| 62 | 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^] | 63 | 9. --binary_output, use binary format response as output, it can |
yang-g | df012d0 | 2016-05-18 15:44:06 -0700 | [diff] [blame] | 64 | be later decoded using protoc: |
| 65 | protoc --proto_path=src/proto/grpc/testing/ \ |
| 66 | --decode=grpc.testing.SimpleResponse \ |
| 67 | src/proto/grpc/testing/messages.proto \ |
| 68 | < output.bin > output.txt |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 69 | */ |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 70 | |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 71 | #include <fstream> |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 72 | #include <functional> |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 73 | #include <iostream> |
Yang Gao | ced2b89 | 2015-03-26 22:59:54 -0700 | [diff] [blame] | 74 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 75 | #include <gflags/gflags.h> |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 76 | #include <grpc++/support/config.h> |
Yuchen Zeng | 02139a0 | 2016-08-15 11:34:21 -0700 | [diff] [blame^] | 77 | #include "test/cpp/util/cli_credentials.h" |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 78 | #include "test/cpp/util/grpc_tool.h" |
yang-g | 9e2f90c | 2015-08-21 15:35:03 -0700 | [diff] [blame] | 79 | #include "test/cpp/util/test_config.h" |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 80 | |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 81 | DEFINE_string(outfile, "", "Output file (default is stdout)"); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 82 | |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 83 | static bool SimplePrint(const grpc::string& outfile, |
| 84 | const grpc::string& output) { |
| 85 | if (outfile.empty()) { |
| 86 | std::cout << output; |
| 87 | } else { |
| 88 | std::ofstream output_file(outfile, std::ios::trunc | std::ios::binary); |
| 89 | output_file << output; |
| 90 | output_file.close(); |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 91 | } |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 92 | return true; |
Yang Gao | 102eccb | 2015-06-16 00:43:25 -0700 | [diff] [blame] | 93 | } |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 94 | |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 95 | int main(int argc, char** argv) { |
Yang Gao | 103837e | 2015-04-15 15:23:54 -0700 | [diff] [blame] | 96 | grpc::testing::InitTest(&argc, &argv, true); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 97 | |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 98 | return grpc::testing::GrpcToolMainLib( |
Yuchen Zeng | 02139a0 | 2016-08-15 11:34:21 -0700 | [diff] [blame^] | 99 | argc, (const char**)argv, grpc::testing::CliCredentials(), |
Yuchen Zeng | 29ca79b | 2016-07-25 12:00:08 -0700 | [diff] [blame] | 100 | std::bind(SimplePrint, FLAGS_outfile, std::placeholders::_1)); |
Yang Gao | a5e20d3 | 2015-03-25 09:55:20 -0700 | [diff] [blame] | 101 | } |