blob: 10e13d929bb2cf294780157ae4a39349cefb2e9e [file] [log] [blame]
Yang Gaoa5e20d32015-03-25 09:55:20 -07001/*
Julien Boeuf5be92a32015-08-28 16:28:18 -07002
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Yang Gaoa5e20d32015-03-25 09:55:20 -07004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Gaoa5e20d32015-03-25 09:55:20 -07008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Yang Gaoa5e20d32015-03-25 09:55:20 -070010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Gaoa5e20d32015-03-25 09:55:20 -070016 *
17 */
18
Yang Gaoced2b892015-03-26 22:59:54 -070019/*
yang-gdf012d02016-05-18 15:44:06 -070020 A command line tool to talk to a grpc server.
Yuchen Zeng28263272016-07-25 14:38:07 -070021 Run `grpc_cli help` command to see its usage information.
22
Yang Gaoced2b892015-03-26 22:59:54 -070023 Example of talking to grpc interop server:
Yuchen Zeng28263272016-07-25 14:38:07 -070024 grpc_cli call localhost:50051 UnaryCall "response_size:10" \
25 --protofiles=src/proto/grpc/testing/test.proto --enable_ssl=false
yang-gdf012d02016-05-18 15:44:06 -070026
27 Options:
Yuchen Zeng02139a02016-08-15 11:34:21 -070028 1. --protofiles, use this flag to provide proto files if the server does
Yuchen Zeng28263272016-07-25 14:38:07 -070029 does not have the reflection service.
30 2. --proto_path, if your proto file is not under current working directory,
yang-gdf012d02016-05-18 15:44:06 -070031 use this flag to provide a search root. It should work similar to the
Yuchen Zeng28263272016-07-25 14:38:07 -070032 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-gdf012d02016-05-18 15:44:06 -070035 --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2"
Yuchen Zeng28263272016-07-25 14:38:07 -070036 4. --enable_ssl, whether to use tls.
37 5. --use_auth, if set to true, attach a GoogleDefaultCredentials to the call
Yuchen Zeng02139a02016-08-15 11:34:21 -070038 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-gdf012d02016-05-18 15:44:06 -070042 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 Zeng02139a02016-08-15 11:34:21 -070048 9. --binary_output, use binary format response as output, it can
yang-gdf012d02016-05-18 15:44:06 -070049 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 Gaoced2b892015-03-26 22:59:54 -070054*/
Yang Gaoa5e20d32015-03-25 09:55:20 -070055
Yang Gaoced2b892015-03-26 22:59:54 -070056#include <fstream>
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070057#include <functional>
Yang Gaoced2b892015-03-26 22:59:54 -070058#include <iostream>
Yang Gaoced2b892015-03-26 22:59:54 -070059
Yang Gaoa5e20d32015-03-25 09:55:20 -070060#include <gflags/gflags.h>
Vijay Paic90a8562018-03-08 21:20:24 -080061#include <grpcpp/support/config.h>
Yuchen Zeng02139a02016-08-15 11:34:21 -070062#include "test/cpp/util/cli_credentials.h"
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070063#include "test/cpp/util/grpc_tool.h"
yang-g9e2f90c2015-08-21 15:35:03 -070064#include "test/cpp/util/test_config.h"
Yang Gaoa5e20d32015-03-25 09:55:20 -070065
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070066DEFINE_string(outfile, "", "Output file (default is stdout)");
Yang Gao102eccb2015-06-16 00:43:25 -070067
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070068static bool SimplePrint(const grpc::string& outfile,
69 const grpc::string& output) {
70 if (outfile.empty()) {
Yuchen Zengd37f6422016-09-09 20:05:37 -070071 std::cout << output << std::endl;
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070072 } else {
Yuchen Zeng8d2d70c2016-09-16 15:42:57 -070073 std::ofstream output_file(outfile, std::ios::app | std::ios::binary);
Yuchen Zengd37f6422016-09-09 20:05:37 -070074 output_file << output << std::endl;
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070075 output_file.close();
Yang Gao102eccb2015-06-16 00:43:25 -070076 }
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070077 return true;
Yang Gao102eccb2015-06-16 00:43:25 -070078}
Yang Gaoa5e20d32015-03-25 09:55:20 -070079
Yang Gaoa5e20d32015-03-25 09:55:20 -070080int main(int argc, char** argv) {
Yang Gao103837e2015-04-15 15:23:54 -070081 grpc::testing::InitTest(&argc, &argv, true);
Yang Gaoa5e20d32015-03-25 09:55:20 -070082
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070083 return grpc::testing::GrpcToolMainLib(
Yuchen Zeng02139a02016-08-15 11:34:21 -070084 argc, (const char**)argv, grpc::testing::CliCredentials(),
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070085 std::bind(SimplePrint, FLAGS_outfile, std::placeholders::_1));
Yang Gaoa5e20d32015-03-25 09:55:20 -070086}