blob: d076afcdad97bd13b447ea09aeb1ea50d2bd3e2f [file] [log] [blame]
Tyler Chatowa79419d2020-08-12 20:12:11 -07001#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
10DEFINE_string(config, "./config.json", "File path of aos configuration");
11
12static 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
17int main(int argc, char **argv) {
18 aos::InitGoogle(&argc, &argv);
Tyler Chatowa79419d2020-08-12 20:12:11 -070019
20 CHECK(argc == 3) << "Invalid number of command arguments";
21
22 const std::string application_name = argv[1];
23 const std::string command_str = argv[2];
24
25 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
26 aos::configuration::ReadConfig(FLAGS_config);
27
28 if (command_str == "status") {
29 auto status = aos::starter::GetStatus(application_name, &config.message());
30 std::cout << aos::FlatbufferToJson(&status.message()) << '\n';
31
32 return 0;
33 }
34
35 const auto command_search = kCommands.find(command_str);
36 CHECK(command_search != kCommands.end())
37 << "Invalid command \"" << command_str << "\"";
38 const aos::starter::Command command = command_search->second;
39
40 if (aos::starter::SendCommandBlocking(command, application_name,
41 &config.message(),
42 std::chrono::seconds(3))) {
43 switch (command) {
44 case aos::starter::Command::START:
45 std::cout << "Successfully started " << application_name << '\n';
46 break;
47 case aos::starter::Command::STOP:
48 std::cout << "Successfully stopped " << application_name << '\n';
49 break;
50 case aos::starter::Command::RESTART:
51 std::cout << "Successfully restarted " << application_name << '\n';
52 break;
53 }
54 } else {
55 std::cout << "Failed to " << command_str << ' ' << application_name << '\n';
56 }
57}