1 /*
2
3 * Copyright 2015 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 /*
20 A command line tool to talk to a grpc server.
21 Run `grpc_cli help` command to see its usage information.
22
23 Example of talking to grpc interop server:
24 grpc_cli call localhost:50051 UnaryCall "response_size:10" \
25 --protofiles=src/proto/grpc/testing/test.proto --enable_ssl=false
26
27 Options:
28 1. --protofiles, use this flag to provide proto files if the server does
29 does not have the reflection service.
30 2. --proto_path, if your proto file is not under current working directory,
31 use this flag to provide a search root. It should work similar to the
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:
35 --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2"
36 4. --enable_ssl, whether to use tls.
37 5. --use_auth, if set to true, attach a GoogleDefaultCredentials to the call
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:
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.
48 9. --binary_output, use binary format response as output, it can
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
54 10. --default_service_config, optional default service config to use
55 on the channel. Note that this may be ignored if the name resolver
56 returns a service config.
57 */
58
59 #include <fstream>
60 #include <functional>
61 #include <iostream>
62
63 #include <gflags/gflags.h>
64 #include <grpcpp/support/config.h>
65 #include "test/cpp/util/cli_credentials.h"
66 #include "test/cpp/util/grpc_tool.h"
67 #include "test/cpp/util/test_config.h"
68
69 DEFINE_string(outfile, "", "Output file (default is stdout)");
70
SimplePrint(const std::string & outfile,const std::string & output)71 static bool SimplePrint(const std::string& outfile, const std::string& output) {
72 if (outfile.empty()) {
73 std::cout << output << std::flush;
74 } else {
75 std::ofstream output_file(outfile, std::ios::app | std::ios::binary);
76 output_file << output << std::flush;
77 output_file.close();
78 }
79 return true;
80 }
81
main(int argc,char ** argv)82 int main(int argc, char** argv) {
83 grpc::testing::InitTest(&argc, &argv, true);
84
85 return grpc::testing::GrpcToolMainLib(
86 argc, (const char**)argv, grpc::testing::CliCredentials(),
87 std::bind(SimplePrint, FLAGS_outfile, std::placeholders::_1));
88 }
89