Allow executable name input in starter_cmd
Tab-complete gives the executable name, so this will allow you
to not have to mamually type in the app name
Change-Id: I34295a4035608315be7afbaf79c188ee629ada74
diff --git a/aos/starter/starter_cmd.cc b/aos/starter/starter_cmd.cc
index 8f9e125..e9b6ed5 100644
--- a/aos/starter/starter_cmd.cc
+++ b/aos/starter/starter_cmd.cc
@@ -2,6 +2,7 @@
#include <functional>
#include <iostream>
#include <optional>
+#include <string_view>
#include <unordered_map>
#include "absl/strings/str_format.h"
@@ -23,13 +24,14 @@
{"restart", aos::starter::Command::RESTART}};
void PrintKey() {
- absl::PrintF("%-30s %-30s %s\n\n", "Name", "Time since last started", "State");
+ absl::PrintF("%-30s %-30s %s\n\n", "Name", "Time since last started",
+ "State");
}
void PrintApplicationStatus(const aos::starter::ApplicationStatus *app_status,
- const aos::monotonic_clock::time_point &time) {
- const auto last_start_time =
- aos::monotonic_clock::time_point(chrono::nanoseconds(app_status->last_start_time()));
+ const aos::monotonic_clock::time_point &time) {
+ const auto last_start_time = aos::monotonic_clock::time_point(
+ chrono::nanoseconds(app_status->last_start_time()));
const auto time_running =
chrono::duration_cast<chrono::seconds>(time - last_start_time);
absl::PrintF("%-30s %-30s %s\n", app_status->name()->string_view(),
@@ -54,7 +56,8 @@
}
} else if (argc == 2) {
// Print status for the specified process.
- const char *application_name = argv[1];
+ const auto application_name =
+ aos::starter::FindApplication(argv[1], config);
auto status = aos::starter::GetStatus(application_name, config);
PrintKey();
PrintApplicationStatus(&status.message(), aos::monotonic_clock::now());
@@ -68,7 +71,6 @@
bool InteractWithProgram(int argc, char **argv,
const aos::Configuration *config) {
const char *command_string = argv[0];
-
if (argc != 2) {
LOG(ERROR) << "The \"" << command_string
<< "\" command requires an application name as an argument.";
@@ -81,8 +83,7 @@
<< "\" is not in kCommandConversions.";
const aos::starter::Command command = command_search->second;
- const char *application_name = argv[1];
-
+ const auto application_name = aos::starter::FindApplication(argv[1], config);
if (aos::starter::SendCommandBlocking(command, application_name, config,
chrono::seconds(3))) {
switch (command) {
@@ -143,7 +144,7 @@
if (parsing_failed) {
LOG(ERROR) << "Parsing failed. Valid commands are:";
- for (auto entry: kCommands) {
+ for (auto entry : kCommands) {
LOG(ERROR) << " - " << entry.first;
}
return 1;
diff --git a/aos/starter/starter_rpc_lib.cc b/aos/starter/starter_rpc_lib.cc
index 20f6f5a..67b3fb2 100644
--- a/aos/starter/starter_rpc_lib.cc
+++ b/aos/starter/starter_rpc_lib.cc
@@ -26,6 +26,19 @@
return *search;
}
+std::string_view FindApplication(const std::string_view &name,
+ const aos::Configuration *config) {
+ std::string_view app_name = name;
+ for (const auto app : *config->applications()) {
+ if (app->executable_name() != nullptr &&
+ app->executable_name()->string_view() == name) {
+ app_name = app->name()->string_view();
+ break;
+ }
+ }
+ return app_name;
+}
+
bool SendCommandBlocking(aos::starter::Command command, std::string_view name,
const aos::Configuration *config,
std::chrono::milliseconds timeout) {
@@ -127,15 +140,15 @@
aos::starter::ApplicationStatus>::Empty();
}
-std::optional<const aos::FlatbufferVector<aos::starter::Status>> GetStarterStatus(
- const aos::Configuration *config) {
+std::optional<const aos::FlatbufferVector<aos::starter::Status>>
+GetStarterStatus(const aos::Configuration *config) {
ShmEventLoop event_loop(config);
event_loop.SkipAosLog();
auto status_fetcher = event_loop.MakeFetcher<aos::starter::Status>("/aos");
status_fetcher.Fetch();
- return (status_fetcher ? std::make_optional(status_fetcher.CopyFlatBuffer()) :
- std::nullopt);
+ return (status_fetcher ? std::make_optional(status_fetcher.CopyFlatBuffer())
+ : std::nullopt);
}
} // namespace starter
diff --git a/aos/starter/starter_rpc_lib.h b/aos/starter/starter_rpc_lib.h
index 24c757e..7d6de873 100644
--- a/aos/starter/starter_rpc_lib.h
+++ b/aos/starter/starter_rpc_lib.h
@@ -16,6 +16,11 @@
const aos::starter::ApplicationStatus *FindApplicationStatus(
const aos::starter::Status &status, std::string_view name);
+// Checks if the name is an executable name and if it is, it returns that
+// application's name, otherwise returns name as given
+std::string_view FindApplication(const std::string_view &name,
+ const aos::Configuration *config);
+
// Sends the given command to the application with the name name. Creates a
// temporary event loop from the provided config for sending the command and
// receiving back status messages. Returns true if the command executed
@@ -33,8 +38,8 @@
// Fetches the entire status message of starter. Creates a temporary event loop
// from the provided config for fetching.
-std::optional<const aos::FlatbufferVector<aos::starter::Status>> GetStarterStatus(
- const aos::Configuration *config);
+std::optional<const aos::FlatbufferVector<aos::starter::Status>>
+GetStarterStatus(const aos::Configuration *config);
} // namespace starter
} // namespace aos