Line data Source code
1 : // Copyright 2024 Accenture.
2 :
3 : // NOLINTBEGIN(cppcoreguidelines-pro-type-vararg): Logger/StringWriter API is variadic by design.
4 :
5 : #include "console/AsyncConsole.h"
6 :
7 : #include "console/SyncCommandWrapper.h"
8 : #include "logger/ConsoleLogger.h"
9 :
10 : #include <etl/singleton_base.h>
11 : #include <etl/string.h>
12 : #include <util/command/HelpCommand.h>
13 : #include <util/command/ParentCommand.h>
14 : #include <util/format/SharedStringWriter.h>
15 :
16 : namespace
17 : {
18 1 : ::util::command::ParentCommand& getParentCommand()
19 : {
20 1 : static ::util::command::ParentCommand parentCommand("root", "root");
21 1 : return parentCommand;
22 : }
23 : } // namespace
24 :
25 : namespace console
26 : {
27 : using ::util::logger::CONSOLE;
28 : using ::util::logger::Logger;
29 :
30 0 : AsyncConsole::AsyncConsole()
31 0 : : ::etl::singleton_base<AsyncConsole>(*this), fpOutputStream(nullptr), fOnLineProcessed()
32 : {
33 0 : static ::util::command::HelpCommand helpCommand(getParentCommand());
34 0 : static ::console::SyncCommandWrapper syncCommandWrapper(helpCommand);
35 0 : }
36 :
37 1 : void AsyncConsole::addCommand(::util::command::ICommand& command)
38 : {
39 1 : getParentCommand().addCommand(command);
40 1 : }
41 :
42 0 : void AsyncConsole::commandExecuted(::util::command::ICommand::ExecuteResult result)
43 : {
44 0 : AsyncConsole::instance().terminate(result);
45 0 : }
46 :
47 0 : void AsyncConsole::onLineReceived(
48 : ::util::stream::ISharedOutputStream& outputStream,
49 : ::etl::istring const& line,
50 : OnLineProcessed const& onLineProcessed)
51 : {
52 0 : auto const cmd = ::util::string::ConstString(line.c_str(), line.length());
53 0 : Logger::info(CONSOLE, "Received console command \"%.*s\"", cmd.length(), cmd.data());
54 :
55 0 : fOnLineProcessed = onLineProcessed;
56 0 : fpOutputStream = &outputStream;
57 :
58 : ::util::command::ICommand::ExecuteResult const result
59 0 : = getParentCommand().execute(cmd, &outputStream);
60 :
61 0 : if (result.getResult() == ::util::command::ICommand::Result::OK)
62 : { // in this case, we terminate since commandExecuted is called
63 0 : return;
64 : }
65 :
66 0 : Logger::info(CONSOLE, "Console command failed");
67 :
68 0 : ::util::format::SharedStringWriter sharedStringWriter(outputStream);
69 0 : sharedStringWriter.printf("error\n");
70 :
71 0 : onLineProcessed();
72 0 : }
73 :
74 0 : void AsyncConsole::terminate(::util::command::ICommand::ExecuteResult result)
75 : {
76 0 : if (fpOutputStream != nullptr)
77 : {
78 0 : ::util::format::StringWriter stringWriter(fpOutputStream->startOutput(nullptr));
79 :
80 0 : switch (result.getResult())
81 : {
82 0 : case ::util::command::ICommand::Result::OK:
83 : {
84 0 : Logger::info(CONSOLE, "Console command succeeded");
85 0 : stringWriter.printf("ok\n");
86 0 : break;
87 : }
88 0 : default:
89 : {
90 0 : Logger::info(CONSOLE, "Console command failed");
91 0 : stringWriter.printf("error\n");
92 0 : break;
93 : }
94 : }
95 0 : fpOutputStream = nullptr;
96 : }
97 :
98 0 : OnLineProcessed onLineProcessed = fOnLineProcessed;
99 0 : fOnLineProcessed = OnLineProcessed();
100 0 : onLineProcessed();
101 0 : }
102 :
103 : } /* namespace console */
104 :
105 : // NOLINTEND(cppcoreguidelines-pro-type-vararg)
|