forked from janhq/cortex.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel_source_list_cmd.cc
More file actions
57 lines (51 loc) · 1.5 KB
/
model_source_list_cmd.cc
File metadata and controls
57 lines (51 loc) · 1.5 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
#include "model_source_list_cmd.h"
#include <json/reader.h>
#include <json/value.h>
#include <iostream>
#include <vector>
#include "server_start_cmd.h"
#include "utils/curl_utils.h"
#include "utils/json_helper.h"
#include "utils/logging_utils.h"
#include "utils/string_utils.h"
#include "utils/url_parser.h"
// clang-format off
#include <tabulate/table.hpp>
// clang-format on
namespace commands {
bool ModelSourceListCmd::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 false;
}
}
tabulate::Table table;
table.add_row({"#", "Model Source"});
auto url = url_parser::Url{
/* .protocol = */ "http",
/* .host = */ host + ":" + std::to_string(port),
/* .pathParams = */ {"v1", "models", "sources"},
/* .queries = */ {},
};
auto result = curl_utils::SimpleGetJson(url.ToFullPath());
if (result.has_error()) {
CTL_ERR(result.error());
return false;
}
table.format().font_color(tabulate::Color::green);
int count = 0;
if (!result.value()["data"].isNull()) {
for (auto const& v : result.value()["data"]) {
auto model_source = v.asString();
count += 1;
std::vector<std::string> row = {std::to_string(count), model_source};
table.add_row({row.begin(), row.end()});
}
}
std::cout << table << std::endl;
return true;
}
}; // namespace commands