forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_get_cmd.cc
More file actions
49 lines (44 loc) · 1.34 KB
/
config_get_cmd.cc
File metadata and controls
49 lines (44 loc) · 1.34 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
#include "config_get_cmd.h"
#include "commands/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
void commands::ConfigGetCmd::Exec(const std::string& host, int port) {
// 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;
}
}
auto url = url_parser::Url{
/* .protocol = */ "http",
/* .host = */ host + ":" + std::to_string(port),
/* .pathParams = */ {"v1", "configs"},
/* .queries = */ {},
};
auto get_config_result = curl_utils::SimpleGetJson(url.ToFullPath());
if (get_config_result.has_error()) {
CLI_LOG_ERROR(
"Failed to get configurations: " << get_config_result.error());
return;
}
auto json_value = get_config_result.value();
tabulate::Table table;
table.add_row({"Config name", "Value"});
for (const auto& key : json_value.getMemberNames()) {
if (json_value[key].isArray()) {
for (const auto& value : json_value[key]) {
table.add_row({key, value.asString()});
}
} else {
table.add_row({key, json_value[key].asString()});
}
}
std::cout << table << std::endl;
return;
}