forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_get_cmd.cc
More file actions
68 lines (60 loc) · 1.92 KB
/
engine_get_cmd.cc
File metadata and controls
68 lines (60 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "engine_get_cmd.h"
#include <json/reader.h>
#include <json/value.h>
#include <iostream>
#include "common/engine_servicei.h"
#include "server_start_cmd.h"
#include "utils/curl_utils.h"
#include "utils/logging_utils.h"
#include "utils/url_parser.h"
// clang-format off
#include <tabulate/table.hpp>
// clang-format on
namespace commands {
void EngineGetCmd::Exec(const std::string& host, int port,
const std::string& engine_name) const {
// Start server if server is not started yet
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return;
}
}
tabulate::Table table;
table.add_row({"#", "Name", "Version", "Variant", "Status"});
auto url = url_parser::Url{/* .protocol = */ "http",
/* .host = */ host + ":" + std::to_string(port),
/* .pathParams = */ {"v1", "engines", engine_name},
/* .queries = */ {}};
auto result = curl_utils::SimpleGetJson(url.ToFullPath());
if (result.has_error()) {
// TODO: refactor this
Json::Value root;
Json::Reader reader;
if (!reader.parse(result.error(), root)) {
CLI_LOG(result.error());
return;
}
CLI_LOG(root["message"].asString());
return;
}
std::vector<EngineVariantResponse> output;
auto installed_variants = result.value();
for (const auto& variant : installed_variants) {
output.push_back(EngineVariantResponse{
/* .name = */ variant["name"].asString(),
/* .version = */ variant["version"].asString(),
/* .engine = */ engine_name,
/* .type = */ "",
});
}
int count = 0;
for (auto const& v : output) {
count += 1;
table.add_row(
{std::to_string(count), v.engine, v.version, v.name, "Installed"});
}
std::cout << table << std::endl;
}
}; // namespace commands