blob: fe248601eebd6211a713a5d186b7de0661bdc697 [file] [log] [blame]
Yang Gaoa5e20d32015-03-25 09:55:20 -07001/*
Julien Boeuf5be92a32015-08-28 16:28:18 -07002
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Yang Gaoa5e20d32015-03-25 09:55:20 -07004 * 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 Gaoced2b892015-03-26 22:59:54 -070034/*
yang-gdf012d02016-05-18 15:44:06 -070035 A command line tool to talk to a grpc server.
Yuchen Zeng28263272016-07-25 14:38:07 -070036 Run `grpc_cli help` command to see its usage information.
37
Yang Gaoced2b892015-03-26 22:59:54 -070038 Example of talking to grpc interop server:
Yuchen Zeng28263272016-07-25 14:38:07 -070039 grpc_cli call localhost:50051 UnaryCall "response_size:10" \
40 --protofiles=src/proto/grpc/testing/test.proto --enable_ssl=false
yang-gdf012d02016-05-18 15:44:06 -070041
42 Options:
Yuchen Zeng02139a02016-08-15 11:34:21 -070043 1. --protofiles, use this flag to provide proto files if the server does
Yuchen Zeng28263272016-07-25 14:38:07 -070044 does not have the reflection service.
45 2. --proto_path, if your proto file is not under current working directory,
yang-gdf012d02016-05-18 15:44:06 -070046 use this flag to provide a search root. It should work similar to the
Yuchen Zeng28263272016-07-25 14:38:07 -070047 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-gdf012d02016-05-18 15:44:06 -070050 --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2"
Yuchen Zeng28263272016-07-25 14:38:07 -070051 4. --enable_ssl, whether to use tls.
52 5. --use_auth, if set to true, attach a GoogleDefaultCredentials to the call
Yuchen Zeng02139a02016-08-15 11:34:21 -070053 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-gdf012d02016-05-18 15:44:06 -070057 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 Zeng02139a02016-08-15 11:34:21 -070063 9. --binary_output, use binary format response as output, it can
yang-gdf012d02016-05-18 15:44:06 -070064 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 Gaoced2b892015-03-26 22:59:54 -070069*/
Yang Gaoa5e20d32015-03-25 09:55:20 -070070
Yang Gaoced2b892015-03-26 22:59:54 -070071#include <fstream>
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070072#include <functional>
Yang Gaoced2b892015-03-26 22:59:54 -070073#include <iostream>
Yang Gaoced2b892015-03-26 22:59:54 -070074
Yang Gaoa5e20d32015-03-25 09:55:20 -070075#include <gflags/gflags.h>
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070076#include <grpc++/support/config.h>
Yuchen Zeng02139a02016-08-15 11:34:21 -070077#include "test/cpp/util/cli_credentials.h"
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070078#include "test/cpp/util/grpc_tool.h"
yang-g9e2f90c2015-08-21 15:35:03 -070079#include "test/cpp/util/test_config.h"
Yang Gaoa5e20d32015-03-25 09:55:20 -070080
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070081DEFINE_string(outfile, "", "Output file (default is stdout)");
Yang Gao102eccb2015-06-16 00:43:25 -070082
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070083static 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 Gao102eccb2015-06-16 00:43:25 -070091 }
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070092 return true;
Yang Gao102eccb2015-06-16 00:43:25 -070093}
Yang Gaoa5e20d32015-03-25 09:55:20 -070094
Yang Gaoa5e20d32015-03-25 09:55:20 -070095int main(int argc, char** argv) {
Yang Gao103837e2015-04-15 15:23:54 -070096 grpc::testing::InitTest(&argc, &argv, true);
Yang Gaoa5e20d32015-03-25 09:55:20 -070097
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070098 return grpc::testing::GrpcToolMainLib(
Yuchen Zeng02139a02016-08-15 11:34:21 -070099 argc, (const char**)argv, grpc::testing::CliCredentials(),
Yuchen Zeng29ca79b2016-07-25 12:00:08 -0700100 std::bind(SimplePrint, FLAGS_outfile, std::placeholders::_1));
Yang Gaoa5e20d32015-03-25 09:55:20 -0700101}