• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 */
55 
56 #include <fstream>
57 #include <functional>
58 #include <iostream>
59 
60 #include <gflags/gflags.h>
61 #include <grpcpp/support/config.h>
62 #include "test/cpp/util/cli_credentials.h"
63 #include "test/cpp/util/grpc_tool.h"
64 #include "test/cpp/util/test_config.h"
65 
66 DEFINE_string(outfile, "", "Output file (default is stdout)");
67 
SimplePrint(const grpc::string & outfile,const grpc::string & output)68 static bool SimplePrint(const grpc::string& outfile,
69                         const grpc::string& output) {
70   if (outfile.empty()) {
71     std::cout << output << std::endl;
72   } else {
73     std::ofstream output_file(outfile, std::ios::app | std::ios::binary);
74     output_file << output << std::endl;
75     output_file.close();
76   }
77   return true;
78 }
79 
main(int argc,char ** argv)80 int main(int argc, char** argv) {
81   grpc::testing::InitTest(&argc, &argv, true);
82 
83   return grpc::testing::GrpcToolMainLib(
84       argc, (const char**)argv, grpc::testing::CliCredentials(),
85       std::bind(SimplePrint, FLAGS_outfile, std::placeholders::_1));
86 }
87