Tyler Chatow | a79419d | 2020-08-12 20:12:11 -0700 | [diff] [blame^] | 1 | #include <chrono> |
| 2 | #include <iostream> |
| 3 | #include <unordered_map> |
| 4 | |
| 5 | #include "aos/init.h" |
| 6 | #include "aos/json_to_flatbuffer.h" |
| 7 | #include "gflags/gflags.h" |
| 8 | #include "starter_rpc_lib.h" |
| 9 | |
| 10 | DEFINE_string(config, "./config.json", "File path of aos configuration"); |
| 11 | |
| 12 | static const std::unordered_map<std::string, aos::starter::Command> kCommands{ |
| 13 | {"start", aos::starter::Command::START}, |
| 14 | {"stop", aos::starter::Command::STOP}, |
| 15 | {"restart", aos::starter::Command::RESTART}}; |
| 16 | |
| 17 | int main(int argc, char **argv) { |
| 18 | aos::InitGoogle(&argc, &argv); |
| 19 | aos::InitNRT(); |
| 20 | |
| 21 | CHECK(argc == 3) << "Invalid number of command arguments"; |
| 22 | |
| 23 | const std::string application_name = argv[1]; |
| 24 | const std::string command_str = argv[2]; |
| 25 | |
| 26 | aos::FlatbufferDetachedBuffer<aos::Configuration> config = |
| 27 | aos::configuration::ReadConfig(FLAGS_config); |
| 28 | |
| 29 | if (command_str == "status") { |
| 30 | auto status = aos::starter::GetStatus(application_name, &config.message()); |
| 31 | std::cout << aos::FlatbufferToJson(&status.message()) << '\n'; |
| 32 | |
| 33 | return 0; |
| 34 | } |
| 35 | |
| 36 | const auto command_search = kCommands.find(command_str); |
| 37 | CHECK(command_search != kCommands.end()) |
| 38 | << "Invalid command \"" << command_str << "\""; |
| 39 | const aos::starter::Command command = command_search->second; |
| 40 | |
| 41 | if (aos::starter::SendCommandBlocking(command, application_name, |
| 42 | &config.message(), |
| 43 | std::chrono::seconds(3))) { |
| 44 | switch (command) { |
| 45 | case aos::starter::Command::START: |
| 46 | std::cout << "Successfully started " << application_name << '\n'; |
| 47 | break; |
| 48 | case aos::starter::Command::STOP: |
| 49 | std::cout << "Successfully stopped " << application_name << '\n'; |
| 50 | break; |
| 51 | case aos::starter::Command::RESTART: |
| 52 | std::cout << "Successfully restarted " << application_name << '\n'; |
| 53 | break; |
| 54 | } |
| 55 | } else { |
| 56 | std::cout << "Failed to " << command_str << ' ' << application_name << '\n'; |
| 57 | } |
| 58 | } |