From c5a71039a3b8b0f64eee35943769cad7cc45119b Mon Sep 17 00:00:00 2001 From: "Jorge E. Moreira" Date: Fri, 20 Sep 2024 15:14:50 -0700 Subject: [PATCH] Return clear message when no groups exist A clear message existed for stop and status, it's extended for: - bugreport - remove - display - start --- .../host/commands/cvd/server_command/bugreport.cpp | 3 +++ .../host/commands/cvd/server_command/display.cpp | 1 - .../host/commands/cvd/server_command/remove.cpp | 4 +++- .../host/commands/cvd/server_command/start.cpp | 4 ++++ .../host/commands/cvd/server_command/status.cpp | 2 +- .../host/commands/cvd/server_command/stop.cpp | 2 +- .../host/commands/cvd/server_command/utils.cpp | 10 ++++------ .../host/commands/cvd/server_command/utils.h | 2 +- 8 files changed, 17 insertions(+), 11 deletions(-) diff --git a/base/cvd/cuttlefish/host/commands/cvd/server_command/bugreport.cpp b/base/cvd/cuttlefish/host/commands/cvd/server_command/bugreport.cpp index ac3d23e1b0..33fe3f79cc 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/server_command/bugreport.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/server_command/bugreport.cpp @@ -88,6 +88,9 @@ Result CvdBugreportCommandHandler::Handle( std::string android_host_out; std::string home = CF_EXPECT(SystemWideUserHome()); if (!CF_EXPECT(IsHelpSubcmd(cmd_args))) { + if (!CF_EXPECT(instance_manager_.HasInstanceGroups())) { + return NoGroupResponse(request); + } auto instance_group = CF_EXPECT(SelectGroup(instance_manager_, request)); android_host_out = instance_group.HostArtifactsPath(); home = instance_group.HomeDir(); diff --git a/base/cvd/cuttlefish/host/commands/cvd/server_command/display.cpp b/base/cvd/cuttlefish/host/commands/cvd/server_command/display.cpp index f9112d5910..da9d05de77 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/server_command/display.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/server_command/display.cpp @@ -23,7 +23,6 @@ #include #include -#include "common/libs/fs/shared_buf.h" #include "common/libs/utils/contains.h" #include "common/libs/utils/subprocess.h" #include "common/libs/utils/users.h" diff --git a/base/cvd/cuttlefish/host/commands/cvd/server_command/remove.cpp b/base/cvd/cuttlefish/host/commands/cvd/server_command/remove.cpp index bbdcd5e47d..6b45448d12 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/server_command/remove.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/server_command/remove.cpp @@ -20,7 +20,6 @@ #include #include -#include "common/libs/fs/shared_buf.h" #include "common/libs/utils/contains.h" #include "common/libs/utils/result.h" #include "host/commands/cvd/group_selector.h" @@ -78,6 +77,9 @@ class RemoveCvdCommandHandler : public CvdServerHandler { return Success(); } + if (!CF_EXPECT(instance_manager_.HasInstanceGroups())) { + return NoGroupResponse(request); + } auto group = CF_EXPECT(SelectGroup(instance_manager_, request)); auto stop_res = StopGroup(group, request); diff --git a/base/cvd/cuttlefish/host/commands/cvd/server_command/start.cpp b/base/cvd/cuttlefish/host/commands/cvd/server_command/start.cpp index 048fc33118..e95f84e93d 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/server_command/start.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/server_command/start.cpp @@ -599,6 +599,10 @@ Result CvdStartCommandHandler::Handle( return ResponseFromSiginfo(infop); } + if (!CF_EXPECT(instance_manager_.HasInstanceGroups())) { + return NoGroupResponse(request); + } + CF_EXPECT(ConsumeDaemonModeFlag(subcmd_args)); subcmd_args.push_back("--daemon=true"); diff --git a/base/cvd/cuttlefish/host/commands/cvd/server_command/status.cpp b/base/cvd/cuttlefish/host/commands/cvd/server_command/status.cpp index c350432df8..d6a0626a0b 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/server_command/status.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/server_command/status.cpp @@ -174,7 +174,7 @@ Result CvdStatusCommandHandler::Handle( const bool has_print = CF_EXPECT(HasPrint(cmd_args)); if (!CF_EXPECT(instance_manager_.HasInstanceGroups())) { - return CF_EXPECT(NoGroupResponse(request)); + return NoGroupResponse(request); } RequestWithStdio new_request = CF_EXPECT(ProcessInstanceNameFlag(request)); diff --git a/base/cvd/cuttlefish/host/commands/cvd/server_command/stop.cpp b/base/cvd/cuttlefish/host/commands/cvd/server_command/stop.cpp index a37b5bbbcd..bd81d0483a 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/server_command/stop.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/server_command/stop.cpp @@ -121,7 +121,7 @@ Result CvdStopCommandHandler::Handle( } if (!CF_EXPECT(instance_manager_.HasInstanceGroups())) { - return CF_EXPECT(NoGroupResponse(request)); + return NoGroupResponse(request); } cvd_common::Envs envs = request.Envs(); const auto selector_args = request.SelectorArgs(); diff --git a/base/cvd/cuttlefish/host/commands/cvd/server_command/utils.cpp b/base/cvd/cuttlefish/host/commands/cvd/server_command/utils.cpp index 4437b1b7d1..fe02dfa1a2 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/server_command/utils.cpp +++ b/base/cvd/cuttlefish/host/commands/cvd/server_command/utils.cpp @@ -219,17 +219,15 @@ std::string_view TerminalColors::Cyan() const { return is_tty_ ? kTerminalCyan : ""; } -Result NoGroupResponse(const RequestWithStdio& request) { +cvd::Response NoGroupResponse(const RequestWithStdio& request) { cvd::Response response; response.mutable_command_response(); response.mutable_status()->set_code(cvd::Status::OK); - const uid_t uid = getuid(); TerminalColors colors(isatty(1)); std::string notice = fmt::format( - "Command `{}{}{}` is not applicable:\n {}{}{} (uid: '{}{}{}')", - colors.Red(), fmt::join(request.Message().command_request().args(), " "), - colors.Reset(), colors.BoldRed(), "no device", colors.Reset(), - colors.Cyan(), uid, colors.Reset()); + "Command `{}{}{}` is not applicable: {}{}{}", colors.Red(), + fmt::join(request.Message().command_request().args(), " "), + colors.Reset(), colors.BoldRed(), "no device", colors.Reset()); request.Out() << notice << "\n"; response.mutable_status()->set_message(notice); diff --git a/base/cvd/cuttlefish/host/commands/cvd/server_command/utils.h b/base/cvd/cuttlefish/host/commands/cvd/server_command/utils.h index 640a3ecae4..bc094d9861 100644 --- a/base/cvd/cuttlefish/host/commands/cvd/server_command/utils.h +++ b/base/cvd/cuttlefish/host/commands/cvd/server_command/utils.h @@ -75,7 +75,7 @@ Result IsHelpSubcmd(const std::vector& args); // Call this when there is no instance group is running // The function does not verify that. -Result NoGroupResponse(const RequestWithStdio& request); +cvd::Response NoGroupResponse(const RequestWithStdio& request); // Call this when there is more than one group, which the selector flags are // not sufficients to choose one from. The function does not verify that.