|
1 | 1 | #include "engine_list_cmd.h" |
| 2 | +#include "httplib.h" |
| 3 | +#include "json/json.h" |
| 4 | +#include "server_start_cmd.h" |
| 5 | +#include "utils/logging_utils.h" |
| 6 | +// clang-format off |
2 | 7 | #include <tabulate/table.hpp> |
| 8 | +// clang-format on |
3 | 9 |
|
4 | 10 | namespace commands { |
5 | 11 |
|
6 | | -bool EngineListCmd::Exec() { |
7 | | - auto status_list = engine_service_.GetEngineInfoList(); |
| 12 | +bool EngineListCmd::Exec(const std::string& host, int port) { |
| 13 | + // Start server if server is not started yet |
| 14 | + if (!commands::IsServerAlive(host, port)) { |
| 15 | + CLI_LOG("Starting server ..."); |
| 16 | + commands::ServerStartCmd ssc; |
| 17 | + if (!ssc.Exec(host, port)) { |
| 18 | + return false; |
| 19 | + } |
| 20 | + } |
8 | 21 |
|
9 | 22 | tabulate::Table table; |
10 | 23 | table.add_row( |
11 | 24 | {"#", "Name", "Supported Formats", "Version", "Variant", "Status"}); |
12 | | - for (int i = 0; i < status_list.size(); i++) { |
13 | | - auto engine_status = status_list[i]; |
14 | | - std::string index = std::to_string(i + 1); |
15 | | - auto variant = engine_status.variant.value_or(""); |
16 | | - auto version = engine_status.version.value_or(""); |
17 | | - table.add_row({index, engine_status.product_name, engine_status.format, |
18 | | - version, variant, engine_status.status}); |
| 25 | + |
| 26 | + httplib::Client cli(host + ":" + std::to_string(port)); |
| 27 | + auto res = cli.Get("/v1/engines"); |
| 28 | + if (res) { |
| 29 | + if (res->status == httplib::StatusCode::OK_200) { |
| 30 | + int count = 0; |
| 31 | + // CLI_LOG(res->body); |
| 32 | + Json::Value body; |
| 33 | + Json::Reader reader; |
| 34 | + reader.parse(res->body, body); |
| 35 | + if (!body["data"].isNull()) { |
| 36 | + for (auto const& v : body["data"]) { |
| 37 | + count += 1; |
| 38 | + table.add_row({std::to_string(count), v["name"].asString(), |
| 39 | + v["format"].asString(), v["version"].asString(), |
| 40 | + v["variant"].asString(), v["status"].asString()}); |
| 41 | + } |
| 42 | + } |
| 43 | + } else { |
| 44 | + CTL_ERR("Failed to get engine list with status code: " << res->status); |
| 45 | + return false; |
| 46 | + } |
| 47 | + } else { |
| 48 | + auto err = res.error(); |
| 49 | + CTL_ERR("HTTP error: " << httplib::to_string(err)); |
| 50 | + return false; |
19 | 51 | } |
20 | 52 |
|
21 | 53 | std::cout << table << std::endl; |
|
0 commit comments