blob: c1421a5e3b90f2c3eb3f80e3fce317f72f3d16ee [file] [log] [blame]
Brian J Griglak6605beb2022-05-23 17:31:57 -06001#include <algorithm>
Tyler Chatowa79419d2020-08-12 20:12:11 -07002#include <chrono>
Philipp Schrader08537492021-01-23 16:17:55 -08003#include <functional>
Tyler Chatowa79419d2020-08-12 20:12:11 -07004#include <iostream>
milind upadhyaya87957a2021-03-06 20:46:30 -08005#include <optional>
milind upadhyay4272f382021-04-07 18:03:08 -07006#include <string_view>
Tyler Chatowa79419d2020-08-12 20:12:11 -07007#include <unordered_map>
8
Philipp Schrader08537492021-01-23 16:17:55 -08009#include "absl/strings/str_format.h"
James Kuszmaul293b2172021-11-10 16:20:48 -080010#include "absl/strings/str_join.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070011#include "aos/init.h"
12#include "aos/json_to_flatbuffer.h"
milind upadhyaya87957a2021-03-06 20:46:30 -080013#include "aos/time/time.h"
Tyler Chatowa79419d2020-08-12 20:12:11 -070014#include "gflags/gflags.h"
15#include "starter_rpc_lib.h"
16
Austin Schuh8e2dfc62022-08-17 16:36:00 -070017DEFINE_string(config, "aos_config.json", "File path of aos configuration");
James Kuszmaul293b2172021-11-10 16:20:48 -080018// TODO(james): Bash autocompletion for node names.
19DEFINE_string(
20 node, "",
21 "Node to interact with. If empty, just interact with local node.");
22DEFINE_bool(all_nodes, false, "Interact with all nodes.");
Tyler Chatowa79419d2020-08-12 20:12:11 -070023
milind-u08dab882021-10-17 16:24:08 -070024DEFINE_bool(_bash_autocomplete, false,
25 "Internal use: Outputs commands or applications for use with "
26 "autocomplete script.");
27DEFINE_string(_bash_autocomplete_word, "",
28 "Internal use: Current word being autocompleted");
Brian J Griglak6605beb2022-05-23 17:31:57 -060029DEFINE_string(sort, "name",
30 "The name of the column to sort processes by. "
31 "Can be \"name\", \"state\", \"pid\", or \"uptime\".");
milind-u08dab882021-10-17 16:24:08 -070032
Philipp Schrader08537492021-01-23 16:17:55 -080033namespace {
Tyler Chatowa79419d2020-08-12 20:12:11 -070034
milind upadhyaya87957a2021-03-06 20:46:30 -080035namespace chrono = std::chrono;
36
Philipp Schrader08537492021-01-23 16:17:55 -080037static const std::unordered_map<std::string, aos::starter::Command>
38 kCommandConversions{{"start", aos::starter::Command::START},
39 {"stop", aos::starter::Command::STOP},
40 {"restart", aos::starter::Command::RESTART}};
Tyler Chatowa79419d2020-08-12 20:12:11 -070041
James Kuszmaul293b2172021-11-10 16:20:48 -080042std::vector<const aos::Node *> InteractNodes(
43 const aos::Configuration *configuration) {
Austin Schuh43fceaf2021-10-16 14:20:22 -070044 if (!configuration->has_nodes()) {
James Kuszmaul293b2172021-11-10 16:20:48 -080045 return {nullptr};
Austin Schuh43fceaf2021-10-16 14:20:22 -070046 }
47
James Kuszmaul293b2172021-11-10 16:20:48 -080048 if (!FLAGS_node.empty()) {
49 CHECK(!FLAGS_all_nodes) << "Can't specify both --node and --all_nodes.";
50 return {aos::configuration::GetNode(configuration, FLAGS_node)};
51 }
52
53 if (FLAGS_all_nodes) {
54 return aos::configuration::GetNodes(configuration);
55 }
56
57 return {aos::configuration::GetMyNode(configuration)};
Austin Schuh43fceaf2021-10-16 14:20:22 -070058}
59
James Kuszmaul293b2172021-11-10 16:20:48 -080060std::vector<const aos::Node *> InteractNodesForApplication(
61 const aos::Configuration *config, std::string_view application_name) {
62 const std::vector<const aos::Node *> interact_nodes = InteractNodes(config);
63 std::vector<const aos::Node *> application_nodes;
64 std::vector<std::string> debug_node_names;
65 for (const aos::Node *node : interact_nodes) {
66 if (aos::configuration::GetApplication(config, node, application_name) !=
67 nullptr) {
68 application_nodes.push_back(node);
Austin Schuh43fceaf2021-10-16 14:20:22 -070069 }
James Kuszmaul293b2172021-11-10 16:20:48 -080070 if (node != nullptr) {
71 debug_node_names.push_back(node->name()->str());
72 }
Austin Schuh43fceaf2021-10-16 14:20:22 -070073 }
James Kuszmaul293b2172021-11-10 16:20:48 -080074
75 if (application_nodes.empty()) {
76 if (interact_nodes.size() == 1 && interact_nodes[0] == nullptr) {
77 std::cout << "Unknown application " << application_name << std::endl;
78 } else {
79 std::cout << "Unknown application " << application_name
80 << " on any of node(s) "
81 << absl::StrJoin(debug_node_names, ", ") << std::endl;
82 }
83 }
84 return application_nodes;
Austin Schuh43fceaf2021-10-16 14:20:22 -070085}
86
milind upadhyaya87957a2021-03-06 20:46:30 -080087void PrintKey() {
James Kuszmaul293b2172021-11-10 16:20:48 -080088 absl::PrintF("%-30s %-10s %-8s %-6s %-9s\n", "Name", "Node", "State", "PID",
89 "Uptime");
milind upadhyaya87957a2021-03-06 20:46:30 -080090}
91
Brian J Griglak6605beb2022-05-23 17:31:57 -060092std::vector<const aos::starter::ApplicationStatus *> SortApplications(
93 const aos::FlatbufferVector<aos::starter::Status> &status) {
94 std::vector<const aos::starter::ApplicationStatus *> sorted_statuses;
95 for (const aos::starter::ApplicationStatus *app_status :
96 *status.message().statuses()) {
97 sorted_statuses.push_back(app_status);
98 }
99 // If --sort flag not set, then return this unsorted vector as is.
100 if (FLAGS_sort.empty()) {
101 return sorted_statuses;
102 }
103
104 // Convert --sort flag to lowercase for testing below.
105 std::transform(FLAGS_sort.begin(), FLAGS_sort.end(), FLAGS_sort.begin(),
106 tolower);
107
108 // This function is called once for each node being reported upon, so there is
109 // no need to sort on node, it happens implicitly.
110
111 if (FLAGS_sort == "name") {
112 // Sort on name using std::string_view::operator< for lexicographic order.
113 std::sort(sorted_statuses.begin(), sorted_statuses.end(),
114 [](const aos::starter::ApplicationStatus *lhs,
115 const aos::starter::ApplicationStatus *rhs) {
116 return lhs->name()->string_view() < rhs->name()->string_view();
117 });
118 } else if (FLAGS_sort == "state") {
119 // Sort on state first, and then name for apps in same state.
120 // ApplicationStatus::state is an enum, so need to call EnumNameState()
121 // convenience wrapper to convert enum to char*, and then wrap in
122 // std::string_view for lexicographic ordering.
123 std::sort(sorted_statuses.begin(), sorted_statuses.end(),
124 [](const aos::starter::ApplicationStatus *lhs,
125 const aos::starter::ApplicationStatus *rhs) {
126 return (lhs->state() != rhs->state())
127 ? (std::string_view(
128 aos::starter::EnumNameState(lhs->state())) <
129 std::string_view(
130 aos::starter::EnumNameState(rhs->state())))
131 : (lhs->name()->string_view() <
132 rhs->name()->string_view());
133 });
134 } else if (FLAGS_sort == "pid") {
135 // Sort on pid first, and then name for when both apps are not running.
136 // If the app state is STOPPED, then it will not have a pid, so need to test
137 // that first. If only one app is STOPPED, then return Boolean state to put
138 // running apps before stopped.
139 std::sort(sorted_statuses.begin(), sorted_statuses.end(),
140 [](const aos::starter::ApplicationStatus *lhs,
141 const aos::starter::ApplicationStatus *rhs) {
142 if (lhs->state() == aos::starter::State::STOPPED) {
143 if (rhs->state() == aos::starter::State::STOPPED) {
144 return lhs->name()->string_view() <
145 rhs->name()->string_view();
146 } else {
147 return false;
148 }
149 } else {
150 if (rhs->state() == aos::starter::State::STOPPED) {
151 return true;
152 } else {
153 return lhs->pid() < rhs->pid();
154 }
155 }
156 });
157 } else if (FLAGS_sort == "uptime") {
158 // Sort on last_start_time first, and then name for when both apps are not
159 // running, or have exact same start time. Only use last_start_time when app
160 // is not STOPPED. If only one app is STOPPED, then return Boolean state to
161 // put running apps before stopped.
162 std::sort(
163 sorted_statuses.begin(), sorted_statuses.end(),
164 [](const aos::starter::ApplicationStatus *lhs,
165 const aos::starter::ApplicationStatus *rhs) {
166 if (lhs->state() == aos::starter::State::STOPPED) {
167 if (rhs->state() == aos::starter::State::STOPPED) {
168 return lhs->name()->string_view() < rhs->name()->string_view();
169 } else {
170 return false;
171 }
172 } else {
173 if (rhs->state() == aos::starter::State::STOPPED) {
174 return true;
175 } else {
176 return (lhs->last_start_time() == rhs->last_start_time())
177 ? (lhs->name()->string_view() <
178 rhs->name()->string_view())
179 : (lhs->last_start_time() < rhs->last_start_time());
180 }
181 }
182 });
183 } else {
184 std::cerr << "Unknown sort criteria \"" << FLAGS_sort << "\"" << std::endl;
185 exit(1);
186 }
187
188 return sorted_statuses;
189}
190
milind upadhyaya87957a2021-03-06 20:46:30 -0800191void PrintApplicationStatus(const aos::starter::ApplicationStatus *app_status,
James Kuszmaul293b2172021-11-10 16:20:48 -0800192 const aos::monotonic_clock::time_point &time,
193 const aos::Node *node) {
milind upadhyay4272f382021-04-07 18:03:08 -0700194 const auto last_start_time = aos::monotonic_clock::time_point(
195 chrono::nanoseconds(app_status->last_start_time()));
milind upadhyaya87957a2021-03-06 20:46:30 -0800196 const auto time_running =
197 chrono::duration_cast<chrono::seconds>(time - last_start_time);
Austin Schuhf4334002021-10-16 14:19:51 -0700198 if (app_status->state() == aos::starter::State::STOPPED) {
James Kuszmaul293b2172021-11-10 16:20:48 -0800199 absl::PrintF("%-30s %-10s %-8s\n", app_status->name()->string_view(),
200 (node == nullptr) ? "none" : node->name()->string_view(),
Austin Schuhf4334002021-10-16 14:19:51 -0700201 aos::starter::EnumNameState(app_status->state()));
202 } else {
James Kuszmaul293b2172021-11-10 16:20:48 -0800203 absl::PrintF("%-30s %-10s %-8s %-6d %-9s\n",
204 app_status->name()->string_view(),
205 (node == nullptr) ? "none" : node->name()->string_view(),
Austin Schuhf4334002021-10-16 14:19:51 -0700206 aos::starter::EnumNameState(app_status->state()),
Austin Schuh31bbdea2021-10-16 15:51:37 -0700207 app_status->pid(), std::to_string(time_running.count()) + 's');
Austin Schuhf4334002021-10-16 14:19:51 -0700208 }
milind upadhyaya87957a2021-03-06 20:46:30 -0800209}
210
Austin Schuh43fceaf2021-10-16 14:20:22 -0700211// Prints the status for all applications.
212void GetAllStarterStatus(const aos::Configuration *config) {
James Kuszmaul293b2172021-11-10 16:20:48 -0800213 PrintKey();
214 std::vector<const aos::Node *> missing_nodes;
215 for (const aos::Node *node : InteractNodes(config)) {
216 // Print status for all processes.
217 const auto optional_status = aos::starter::GetStarterStatus(config, node);
218 if (optional_status) {
219 const aos::FlatbufferVector<aos::starter::Status> &status =
220 optional_status->second;
221 const aos::monotonic_clock::time_point time = optional_status->first;
Brian J Griglak6605beb2022-05-23 17:31:57 -0600222 const auto &sorted_statuses = SortApplications(status);
James Kuszmaul293b2172021-11-10 16:20:48 -0800223 for (const aos::starter::ApplicationStatus *app_status :
Brian J Griglak6605beb2022-05-23 17:31:57 -0600224 sorted_statuses) {
James Kuszmaul293b2172021-11-10 16:20:48 -0800225 PrintApplicationStatus(app_status, time, node);
226 }
227 } else {
228 missing_nodes.push_back(node);
Philipp Schrader08537492021-01-23 16:17:55 -0800229 }
James Kuszmaul293b2172021-11-10 16:20:48 -0800230 }
231 for (const aos::Node *node : missing_nodes) {
232 if (node == nullptr) {
233 LOG(WARNING) << "No status found.";
234 } else {
235 LOG(WARNING) << "No status found for node "
236 << node->name()->string_view();
237 }
milind-u08dab882021-10-17 16:24:08 -0700238 }
Austin Schuh43fceaf2021-10-16 14:20:22 -0700239}
240
241// Handles the "status" command. Returns true if the help message should be
242// printed.
243bool GetStarterStatus(int argc, char **argv, const aos::Configuration *config) {
244 if (argc == 1) {
245 GetAllStarterStatus(config);
Philipp Schrader08537492021-01-23 16:17:55 -0800246 } else if (argc == 2) {
247 // Print status for the specified process.
milind upadhyay4272f382021-04-07 18:03:08 -0700248 const auto application_name =
249 aos::starter::FindApplication(argv[1], config);
Austin Schuh43fceaf2021-10-16 14:20:22 -0700250 if (application_name == "all") {
251 GetAllStarterStatus(config);
252 return false;
253 }
254
James Kuszmaul293b2172021-11-10 16:20:48 -0800255 const std::vector<const aos::Node *> application_nodes =
256 InteractNodesForApplication(config, application_name);
257 if (application_nodes.empty()) {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700258 return false;
259 }
milind upadhyaya87957a2021-03-06 20:46:30 -0800260 PrintKey();
James Kuszmaul293b2172021-11-10 16:20:48 -0800261 for (const aos::Node *node : application_nodes) {
James Kuszmaule4bb0a22022-01-07 18:14:43 -0800262 auto optional_status =
263 aos::starter::GetStatus(application_name, config, node);
264 if (optional_status.has_value()) {
James Kuszmaul2ca441b2022-01-07 18:16:23 -0800265 PrintApplicationStatus(&optional_status.value().second.message(),
266 optional_status.value().first, node);
James Kuszmaule4bb0a22022-01-07 18:14:43 -0800267 } else {
268 if (node != nullptr) {
269 LOG(ERROR) << "No status available yet for \"" << application_name
270 << "\" on node \"" << node->name()->string_view() << "\".";
271 } else {
272 LOG(ERROR) << "No status available yet for \"" << application_name
273 << "\".";
274 }
275 }
James Kuszmaul293b2172021-11-10 16:20:48 -0800276 }
Philipp Schrader08537492021-01-23 16:17:55 -0800277 } else {
278 LOG(ERROR) << "The \"status\" command requires zero or one arguments.";
279 return true;
280 }
281 return false;
282}
Tyler Chatowa79419d2020-08-12 20:12:11 -0700283
Austin Schuh43fceaf2021-10-16 14:20:22 -0700284// Sends the provided command to all applications. Prints the success text on
285// success, and failure text on failure.
286void InteractWithAll(const aos::Configuration *config,
287 const aos::starter::Command command,
288 std::string_view success_text,
289 std::string_view failure_text) {
James Kuszmaul293b2172021-11-10 16:20:48 -0800290 std::map<const aos::Node *,
291 std::unique_ptr<aos::FlatbufferVector<aos::starter::Status>>>
292 statuses;
293
294 for (const aos::Node *node : InteractNodes(config)) {
295 std::optional<std::pair<aos::monotonic_clock::time_point,
296 const aos::FlatbufferVector<aos::starter::Status>>>
297 optional_status = aos::starter::GetStarterStatus(config, node);
298 if (optional_status.has_value()) {
299 statuses[node] =
300 std::make_unique<aos::FlatbufferVector<aos::starter::Status>>(
301 optional_status.value().second);
302 } else {
303 if (node == nullptr) {
304 LOG(WARNING) << "Starter not running";
305 } else {
306 LOG(WARNING) << "Starter not running on node "
307 << node->name()->string_view();
308 }
309 }
310 }
311
312 if (!statuses.empty()) {
313 std::vector<aos::starter::ApplicationCommand> commands;
Austin Schuh43fceaf2021-10-16 14:20:22 -0700314
315 for (const aos::Application *application : *config->applications()) {
James Kuszmaul293b2172021-11-10 16:20:48 -0800316 const std::string_view application_name =
317 application->name()->string_view();
318 const std::vector<const aos::Node *> application_nodes =
319 InteractNodesForApplication(config, application_name);
320 // Ignore any applications which aren't supposed to be started.
321 if (application_nodes.empty()) {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700322 continue;
323 }
324
James Kuszmaul293b2172021-11-10 16:20:48 -0800325 std::vector<const aos::Node *> running_nodes;
326 if (application->autostart()) {
327 running_nodes = application_nodes;
328 } else {
329 for (const aos::Node *node : application_nodes) {
330 const aos::starter::ApplicationStatus *application_status =
331 aos::starter::FindApplicationStatus(statuses[node]->message(),
332 application_name);
333 if (application_status->state() == aos::starter::State::STOPPED) {
334 if (node == nullptr) {
335 std::cout << "Skipping " << application_name
336 << " because it is STOPPED\n";
337 } else {
338 std::cout << "Skipping " << application_name << " on "
339 << node->name()->string_view()
340 << " because it is STOPPED\n";
341 }
342 continue;
343 } else {
344 running_nodes.push_back(node);
345 }
Austin Schuh43fceaf2021-10-16 14:20:22 -0700346 }
347 }
348
James Kuszmaul293b2172021-11-10 16:20:48 -0800349 if (!running_nodes.empty()) {
350 commands.emplace_back(aos::starter::ApplicationCommand{
351 command, application_name, running_nodes});
352 }
Austin Schuh43fceaf2021-10-16 14:20:22 -0700353 }
354
355 // Restart each running process
356 if (aos::starter::SendCommandBlocking(commands, config,
357 chrono::seconds(5))) {
358 std::cout << success_text << "all \n";
359 } else {
360 std::cout << failure_text << "all \n";
361 }
362 } else {
James Kuszmaul293b2172021-11-10 16:20:48 -0800363 LOG(WARNING) << "None of the starters we care about are running.";
Austin Schuh43fceaf2021-10-16 14:20:22 -0700364 }
365}
366
367// Handles the "start", "stop", and "restart" commands. Returns true if the
368// help message should be printed.
Philipp Schrader08537492021-01-23 16:17:55 -0800369bool InteractWithProgram(int argc, char **argv,
370 const aos::Configuration *config) {
371 const char *command_string = argv[0];
Philipp Schrader08537492021-01-23 16:17:55 -0800372 if (argc != 2) {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700373 LOG(ERROR)
374 << "The \"" << command_string
375 << "\" command requires an application name or 'all' as an argument.";
Philipp Schrader08537492021-01-23 16:17:55 -0800376 return true;
Tyler Chatowa79419d2020-08-12 20:12:11 -0700377 }
378
Philipp Schrader08537492021-01-23 16:17:55 -0800379 const auto command_search = kCommandConversions.find(command_string);
380 CHECK(command_search != kCommandConversions.end())
381 << "Internal error: \"" << command_string
382 << "\" is not in kCommandConversions.";
Philipp Schrader08537492021-01-23 16:17:55 -0800383 const aos::starter::Command command = command_search->second;
Austin Schuh43fceaf2021-10-16 14:20:22 -0700384
385 std::string_view success_text;
386 const std::string failure_text =
387 std::string("Failed to ") + std::string(command_string) + " ";
388 switch (command) {
389 case aos::starter::Command::START:
390 success_text = "Successfully started ";
391 break;
392 case aos::starter::Command::STOP:
393 success_text = "Successfully stopped ";
394 break;
395 case aos::starter::Command::RESTART:
396 success_text = "Successfully restarted ";
397 break;
398 }
399
400 const std::string_view application_name =
401 aos::starter::FindApplication(argv[1], config);
402 if (application_name == "all") {
403 InteractWithAll(config, command, success_text, failure_text);
404 return false;
405 }
James Kuszmaul293b2172021-11-10 16:20:48 -0800406
407 const std::vector<const aos::Node *> application_nodes =
408 InteractNodesForApplication(config, application_name);
409 if (application_nodes.empty()) {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700410 return false;
411 }
412
Philipp Schrader08537492021-01-23 16:17:55 -0800413 if (aos::starter::SendCommandBlocking(command, application_name, config,
James Kuszmaul293b2172021-11-10 16:20:48 -0800414 chrono::seconds(5),
415 application_nodes)) {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700416 std::cout << success_text << application_name << '\n';
Tyler Chatowa79419d2020-08-12 20:12:11 -0700417 } else {
Austin Schuh43fceaf2021-10-16 14:20:22 -0700418 std::cout << failure_text << application_name << '\n';
Jacob Ismael6388db92021-06-28 22:51:24 -0700419 }
420 return false;
421}
422
Austin Schuh33cc4162021-10-16 14:20:28 -0700423bool Help(int /*argc*/, char ** /*argv*/,
424 const aos::Configuration * /*config*/);
425
Philipp Schrader08537492021-01-23 16:17:55 -0800426// This is the set of subcommands we support. Each subcommand accepts argc and
427// argv from its own point of view. So argv[0] is always the name of the
428// subcommand. argv[1] and up are the arguments to the subcommand.
429// The subcommand returns true if there was an error parsing the command line
430// arguments. It returns false when the command line arguments are parsed
431// successfully.
Austin Schuh33cc4162021-10-16 14:20:28 -0700432static const std::vector<
433 std::tuple<std::string,
434 std::function<bool(int argc, char **argv,
435 const aos::Configuration *config)>,
436 std::string_view>>
437 kCommands{
438 {"help", Help, ""},
439 {"status", GetStarterStatus,
440 " [application], Returns the status of the provided application, "
441 "or all applications by default"},
442 {"start", InteractWithProgram,
443 " application, Starts the provided application, "
444 "or all applications if all is provided"},
445 {"stop", InteractWithProgram,
446 " application, Stops the provided application, "
447 "or all applications if all is provided"},
448 {"restart", InteractWithProgram,
449 " application, Restarts the provided application, "
450 "or all applications if all is provided"}};
451
452bool Help(int /*argc*/, char ** /*argv*/,
453 const aos::Configuration * /*config*/) {
454 std::cout << "Valid commands are:" << std::endl;
455 for (auto entry : kCommands) {
456 std::cout << " - " << std::get<0>(entry) << std::get<2>(entry) << std::endl;
457 }
458 return false;
459}
Philipp Schrader08537492021-01-23 16:17:55 -0800460
milind-u08dab882021-10-17 16:24:08 -0700461void Autocomplete(int argc, char **argv, const aos::Configuration *config) {
462 const std::string_view command = (argc >= 2 ? argv[1] : "");
463 const std::string_view app_name = (argc >= 3 ? argv[2] : "");
464
465 std::cout << "COMPREPLY=(";
466 if (FLAGS__bash_autocomplete_word == command) {
467 // Autocomplete the starter command
468 for (const auto &entry : kCommands) {
469 if (std::get<0>(entry).find(command) == 0) {
470 std::cout << '\'' << std::get<0>(entry) << "' ";
471 }
472 }
473 } else {
474 // Autocomplete the app name
475 for (const auto *app : *config->applications()) {
476 if (app->has_name() && app->name()->string_view().find(app_name) == 0) {
477 std::cout << '\'' << app->name()->string_view() << "' ";
478 }
479 }
480
481 // Autocomplete with "all"
milind-u95296dd2021-10-19 07:42:17 -0700482 if (std::string_view("all").find(app_name) == 0) {
milind-u08dab882021-10-17 16:24:08 -0700483 std::cout << "'all'";
484 }
485 }
486 std::cout << ')';
487}
488
Philipp Schrader08537492021-01-23 16:17:55 -0800489} // namespace
490
491int main(int argc, char **argv) {
492 aos::InitGoogle(&argc, &argv);
493
494 aos::FlatbufferDetachedBuffer<aos::Configuration> config =
495 aos::configuration::ReadConfig(FLAGS_config);
496
milind-u08dab882021-10-17 16:24:08 -0700497 if (FLAGS__bash_autocomplete) {
498 Autocomplete(argc, argv, &config.message());
499 return 0;
500 }
501
Philipp Schrader08537492021-01-23 16:17:55 -0800502 bool parsing_failed = false;
503
504 if (argc < 2) {
505 parsing_failed = true;
506 } else {
507 const char *command = argv[1];
Austin Schuh33cc4162021-10-16 14:20:28 -0700508 auto it = std::find_if(
509 kCommands.begin(), kCommands.end(),
510 [command](const std::tuple<
511 std::string,
512 std::function<bool(int argc, char **argv,
513 const aos::Configuration *config)>,
514 std::string_view> &t) { return std::get<0>(t) == command; });
515
Philipp Schrader08537492021-01-23 16:17:55 -0800516 if (it == kCommands.end()) {
517 parsing_failed = true;
518 } else {
Austin Schuh33cc4162021-10-16 14:20:28 -0700519 parsing_failed = std::get<1>(*it)(argc - 1, argv + 1, &config.message());
Philipp Schrader08537492021-01-23 16:17:55 -0800520 }
521 }
522
523 if (parsing_failed) {
Austin Schuh33cc4162021-10-16 14:20:28 -0700524 Help(argc - 1, argv + 1, &config.message());
Philipp Schrader08537492021-01-23 16:17:55 -0800525 return 1;
526 }
527
528 return 0;
Tyler Chatowa79419d2020-08-12 20:12:11 -0700529}