1 /*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "host/commands/cvd/server_command/fleet.h"
18
19 #include <iostream>
20 #include <sstream>
21
22 #include "common/libs/fs/shared_buf.h"
23 #include "host/commands/cvd/server_command/server_handler.h"
24 #include "host/commands/cvd/server_command/utils.h"
25
26 namespace cuttlefish {
27
28 /*
29 * Prints out help message for cvd reset
30 *
31 * cvd reset is a feature implemented by the client. However, the user may run
32 * cvd help reset. The cvd help parsing will be done on the server side, and
33 * forwarded to the cvd help handler. The cvd help handler again will forward
34 * it to supposedly cvd reset handler. The cvd reset handler will only receive
35 * "cvd reset --help."
36 *
37 * For, say, "cvd reset" or even "cvd reset --help"," the parsing will be done
38 * on the client side, and handled by the client.
39 *
40 */
41 class CvdResetCommandHandler : public CvdServerHandler {
42 public:
INJECT(CvdResetCommandHandler ())43 INJECT(CvdResetCommandHandler()) {}
44
CanHandle(const RequestWithStdio & request) const45 Result<bool> CanHandle(const RequestWithStdio& request) const {
46 auto invocation = ParseInvocation(request.Message());
47 return invocation.command == kResetSubcmd;
48 }
Handle(const RequestWithStdio & request)49 Result<cvd::Response> Handle(const RequestWithStdio& request) override {
50 CF_EXPECT(CanHandle(request));
51 cvd::Response response;
52 response.mutable_command_response();
53 response.mutable_status()->set_code(cvd::Status::OK);
54 std::stringstream guide_message;
55 guide_message << "\"cvd reset\" is implemented on the client side."
56 << " Try:" << std::endl;
57 guide_message << " cvd reset --help" << std::endl;
58 const auto guide_message_str = guide_message.str();
59 CF_EXPECT_EQ(WriteAll(request.Err(), guide_message_str),
60 guide_message_str.size());
61 return response;
62 }
Interrupt()63 Result<void> Interrupt() override { return CF_ERR("Can't interrupt"); }
CmdList() const64 cvd_common::Args CmdList() const override { return {kResetSubcmd}; }
65
66 private:
67 static constexpr char kResetSubcmd[] = "reset";
68 };
69
CvdResetComponent()70 fruit::Component<> CvdResetComponent() {
71 return fruit::createComponent()
72 .addMultibinding<CvdServerHandler, CvdResetCommandHandler>();
73 }
74
75 } // namespace cuttlefish
76