blob: 28c87956d677c9bcf47fa782d0c93d35fe64a1cb [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.
Yang Gaoced2b892015-03-26 22:59:54 -070036 Example of talking to grpc interop server:
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070037 grpc_cli call localhost:50051 UnaryCall src/proto/grpc/testing/test.proto \
38 "response_size:10" --enable_ssl=false
yang-gdf012d02016-05-18 15:44:06 -070039
40 Options:
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070041 1. --proto_path, if your proto file is not under current working directory,
yang-gdf012d02016-05-18 15:44:06 -070042 use this flag to provide a search root. It should work similar to the
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070043 counterpart in protoc.
44 2. --metadata specifies metadata to be sent to the server, such as:
yang-gdf012d02016-05-18 15:44:06 -070045 --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2"
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070046 3. --enable_ssl, whether to use tls.
47 4. --use_auth, if set to true, attach a GoogleDefaultCredentials to the call
48 3. --input_binary_file, a file containing the serialized request. The file
yang-gdf012d02016-05-18 15:44:06 -070049 can be generated by calling something like:
50 protoc --proto_path=src/proto/grpc/testing/ \
51 --encode=grpc.testing.SimpleRequest \
52 src/proto/grpc/testing/messages.proto \
53 < input.txt > input.bin
54 If this is used and no proto file is provided in the argument list, the
55 method string has to be exact in the form of /package.service/method.
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070056 4. --output_binary_file, a file to write binary format response into, it can
yang-gdf012d02016-05-18 15:44:06 -070057 be later decoded using protoc:
58 protoc --proto_path=src/proto/grpc/testing/ \
59 --decode=grpc.testing.SimpleResponse \
60 src/proto/grpc/testing/messages.proto \
61 < output.bin > output.txt
Yang Gaoced2b892015-03-26 22:59:54 -070062*/
Yang Gaoa5e20d32015-03-25 09:55:20 -070063
Yang Gaoced2b892015-03-26 22:59:54 -070064#include <fstream>
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070065#include <functional>
Yang Gaoced2b892015-03-26 22:59:54 -070066#include <iostream>
Yang Gaoced2b892015-03-26 22:59:54 -070067
Yang Gaoa5e20d32015-03-25 09:55:20 -070068#include <gflags/gflags.h>
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070069#include <grpc++/support/config.h>
70#include "test/cpp/util/grpc_tool.h"
yang-g9e2f90c2015-08-21 15:35:03 -070071#include "test/cpp/util/test_config.h"
Yang Gaoa5e20d32015-03-25 09:55:20 -070072
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070073DEFINE_string(outfile, "", "Output file (default is stdout)");
Yang Gao102eccb2015-06-16 00:43:25 -070074
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070075static bool SimplePrint(const grpc::string& outfile,
76 const grpc::string& output) {
77 if (outfile.empty()) {
78 std::cout << output;
79 } else {
80 std::ofstream output_file(outfile, std::ios::trunc | std::ios::binary);
81 output_file << output;
82 output_file.close();
Yang Gao102eccb2015-06-16 00:43:25 -070083 }
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070084 return true;
Yang Gao102eccb2015-06-16 00:43:25 -070085}
Yang Gaoa5e20d32015-03-25 09:55:20 -070086
Yang Gaoa5e20d32015-03-25 09:55:20 -070087int main(int argc, char** argv) {
Yang Gao103837e2015-04-15 15:23:54 -070088 grpc::testing::InitTest(&argc, &argv, true);
Yang Gaoa5e20d32015-03-25 09:55:20 -070089
Yuchen Zeng29ca79b2016-07-25 12:00:08 -070090 return grpc::testing::GrpcToolMainLib(
91 argc, (const char**)argv,
92 std::bind(SimplePrint, FLAGS_outfile, std::placeholders::_1));
Yang Gaoa5e20d32015-03-25 09:55:20 -070093}