Skip to content

Commit

Permalink
Return clear message when no groups exist
Browse files Browse the repository at this point in the history
A clear message existed for stop and status, it's extended for:
- bugreport
- remove
- display
- start
  • Loading branch information
jemoreira committed Sep 23, 2024
1 parent ab3336e commit c5a7103
Show file tree
Hide file tree
Showing 8 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ Result<cvd::Response> 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <string>
#include <vector>

#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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <string>
#include <vector>

#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"
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,10 @@ Result<cvd::Response> 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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ Result<cvd::Response> 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));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Result<cvd::Response> 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();
Expand Down
10 changes: 4 additions & 6 deletions base/cvd/cuttlefish/host/commands/cvd/server_command/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,15 @@ std::string_view TerminalColors::Cyan() const {
return is_tty_ ? kTerminalCyan : "";
}

Result<cvd::Response> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ Result<bool> IsHelpSubcmd(const std::vector<std::string>& args);

// Call this when there is no instance group is running
// The function does not verify that.
Result<cvd::Response> 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.
Expand Down

0 comments on commit c5a7103

Please sign in to comment.