grpc_cli.cc 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2015 gRPC authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. /*
  18. A command line tool to talk to a grpc server.
  19. Run `grpc_cli help` command to see its usage information.
  20. Example of talking to grpc interop server:
  21. grpc_cli call localhost:50051 UnaryCall "response_size:10" \
  22. --protofiles=src/proto/grpc/testing/test.proto \
  23. --channel_creds_type=insecure
  24. Options:
  25. 1. --protofiles, use this flag to provide proto files if the server does
  26. does not have the reflection service.
  27. 2. --proto_path, if your proto file is not under current working directory,
  28. use this flag to provide a search root. It should work similar to the
  29. counterpart in protoc. This option is valid only when protofiles is
  30. provided.
  31. 3. --metadata specifies metadata to be sent to the server, such as:
  32. --metadata="MyHeaderKey1:Value1:MyHeaderKey2:Value2"
  33. 4. --channel_creds_type, whether to use tls, insecure or platform-specific
  34. options.
  35. 5. --use_auth, if set to true, attach a GoogleDefaultCredentials to the call
  36. 6. --infile, input filename (defaults to stdin)
  37. 7. --outfile, output filename (defaults to stdout)
  38. 8. --binary_input, use the serialized request as input. The serialized
  39. request can be generated by calling something like:
  40. protoc --proto_path=src/proto/grpc/testing/ \
  41. --encode=grpc.testing.SimpleRequest \
  42. src/proto/grpc/testing/messages.proto \
  43. < input.txt > input.bin
  44. If this is used and no proto file is provided in the argument list, the
  45. method string has to be exact in the form of /package.service/method.
  46. 9. --binary_output, use binary format response as output, it can
  47. be later decoded using protoc:
  48. protoc --proto_path=src/proto/grpc/testing/ \
  49. --decode=grpc.testing.SimpleResponse \
  50. src/proto/grpc/testing/messages.proto \
  51. < output.bin > output.txt
  52. 10. --default_service_config, optional default service config to use
  53. on the channel. Note that this may be ignored if the name resolver
  54. returns a service config.
  55. 11. --display_peer_address, on CallMethod commands, log the peer socket
  56. address of the connection that each RPC is made on to stderr.
  57. */
  58. #include <fstream>
  59. #include <functional>
  60. #include <iostream>
  61. #include "absl/flags/flag.h"
  62. #include <grpcpp/support/config.h>
  63. #include "test/cpp/util/cli_credentials.h"
  64. #include "test/cpp/util/grpc_tool.h"
  65. #include "test/cpp/util/test_config.h"
  66. ABSL_FLAG(std::string, outfile, "", "Output file (default is stdout)");
  67. static bool SimplePrint(const std::string& outfile, const std::string& output) {
  68. if (outfile.empty()) {
  69. std::cout << output << std::flush;
  70. } else {
  71. std::ofstream output_file(outfile, std::ios::app | std::ios::binary);
  72. output_file << output << std::flush;
  73. output_file.close();
  74. }
  75. return true;
  76. }
  77. int main(int argc, char** argv) {
  78. grpc::testing::InitTest(&argc, &argv, true);
  79. return grpc::testing::GrpcToolMainLib(
  80. argc, const_cast<const char**>(argv), grpc::testing::CliCredentials(),
  81. std::bind(SimplePrint, absl::GetFlag(FLAGS_outfile),
  82. std::placeholders::_1));
  83. }