xref: /aosp_15_r20/external/grpc-grpc/test/cpp/util/grpc_cli.cc (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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 \
26 //    --channel_creds_type=insecure
27 
28 // Options:
29 //   1. --protofiles, use this flag to provide proto files if the server does
30 //      does not have the reflection service.
31 //   2. --proto_path, if your proto file is not under current working directory,
32 //      use this flag to provide a search root. It should work similar to the
33 //      counterpart in protoc. This option is valid only when protofiles is
34 //      provided.
35 //   3. --metadata specifies metadata to be sent to the server, such as:
36 //      --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2"
37 //   4. --channel_creds_type, whether to use tls, insecure or platform-specific
38 //      options.
39 //   5. --use_auth, if set to true, attach a GoogleDefaultCredentials to the
40 //   call
41 //   6. --infile, input filename (defaults to stdin)
42 //   7. --outfile, output filename (defaults to stdout)
43 //   8. --binary_input, use the serialized request as input. The serialized
44 //      request can be generated by calling something like:
45 //      pr otoc --proto_path=src/proto/grpc/testing/ \
46 //       --encode=grpc.testing.SimpleRequest \
47 //       src/proto/grpc/testing/messages.proto \
48 //       < input.txt > input.bin
49 //      If this is used and no proto file is provided in the argument list, the
50 //      method string has to be exact in the form of /package.service/method.
51 //   9. --binary_output, use binary format response as output, it can
52 //      be later decoded using protoc:
53 //      protoc --proto_path=src/proto/grpc/testing/ \
54 //     --decode=grpc.testing.SimpleResponse \
55 //     src/proto/grpc/testing/messages.proto \
56 //     < output.bin > output.txt
57 //  10. --default_service_config, optional default service config to use
58 //      on the channel. Note that this may be ignored if the name resolver
59 //      returns a service config.
60 //  11. --display_peer_address, on CallMethod commands, log the peer socket
61 //      address of the connection that each RPC is made on to stderr.
62 //
63 
64 #include <fstream>
65 #include <functional>
66 #include <iostream>
67 
68 #include "absl/flags/flag.h"
69 
70 #include <grpcpp/support/config.h>
71 
72 #include "test/cpp/util/cli_credentials.h"
73 #include "test/cpp/util/grpc_tool.h"
74 #include "test/cpp/util/test_config.h"
75 
76 ABSL_FLAG(std::string, outfile, "", "Output file (default is stdout)");
77 
SimplePrint(const std::string & outfile,const std::string & output)78 static bool SimplePrint(const std::string& outfile, const std::string& output) {
79   if (outfile.empty()) {
80     std::cout << output << std::flush;
81   } else {
82     std::ofstream output_file(outfile, std::ios::app | std::ios::binary);
83     output_file << output << std::flush;
84     output_file.close();
85   }
86   return true;
87 }
88 
main(int argc,char ** argv)89 int main(int argc, char** argv) {
90   grpc::testing::InitTest(&argc, &argv, true);
91 
92   return grpc::testing::GrpcToolMainLib(
93       argc, const_cast<const char**>(argv), grpc::testing::CliCredentials(),
94       std::bind(SimplePrint, absl::GetFlag(FLAGS_outfile),
95                 std::placeholders::_1));
96 }
97