Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit cc1313d

Browse files
committed
Add CLI support
1 parent e95ca37 commit cc1313d

File tree

3 files changed

+108
-29
lines changed

3 files changed

+108
-29
lines changed

engine/cli/command_line_parser.cc

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -326,14 +326,8 @@ void CommandLineParser::SetupModelCommands() {
326326
void CommandLineParser::SetupConfigsCommands() {
327327
auto config_cmd =
328328
app_.add_subcommand("config", "Subcommands for managing configurations");
329-
config_cmd->usage(
330-
"Usage:\n" + commands::GetCortexBinary() +
331-
" config status for listing all API server configuration.\n" +
332-
commands::GetCortexBinary() +
333-
" config --cors [on/off] to toggle CORS.\n" +
334-
commands::GetCortexBinary() +
335-
" config --allowed_origins [comma separated origin] to set a list of "
336-
"allowed origin");
329+
config_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
330+
" config [option] [value]");
337331
config_cmd->group(kConfigGroup);
338332
auto config_status_cmd =
339333
config_cmd->add_subcommand("status", "Print all configurations");
@@ -344,18 +338,19 @@ void CommandLineParser::SetupConfigsCommands() {
344338
std::stoi(cml_data_.config.apiServerPort));
345339
});
346340

347-
// TODO: this can be improved
348-
std::vector<std::string> avai_opts{"cors", "allowed_origins"};
349-
std::unordered_map<std::string, std::string> description{
350-
{"cors", "[on/off] Toggling CORS."},
351-
{"allowed_origins",
352-
"Allowed origins for CORS. Comma separated. E.g. "
353-
"http://localhost,https://cortex.so"}};
354-
for (const auto& opt : avai_opts) {
355-
std::string option = "--" + opt;
356-
config_cmd->add_option(option, config_update_opts_[opt], description[opt])
357-
->expected(0, 1)
358-
->default_str("*");
341+
// TODO: recheck the allowed_origins to be able to set to empty
342+
for (const auto& [key, opt] : CONFIGURATIONS) {
343+
std::string option = "--" + opt.name;
344+
auto option_cmd =
345+
config_cmd->add_option(option, config_update_opts_[opt.name], opt.desc)
346+
->group(opt.group)
347+
->default_str(opt.default_value);
348+
349+
if (opt.allow_empty) {
350+
option_cmd->expected(0, 1);
351+
} else {
352+
option_cmd->expected(1);
353+
}
359354
}
360355

361356
config_cmd->callback([this, config_cmd] {

engine/cli/commands/config_upd_cmd.cc

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,39 @@
11
#include "config_upd_cmd.h"
22
#include "commands/server_start_cmd.h"
3+
#include "common/api_server_configuration.h"
34
#include "utils/curl_utils.h"
45
#include "utils/logging_utils.h"
56
#include "utils/string_utils.h"
67
#include "utils/url_parser.h"
78

89
namespace {
9-
const std::vector<std::string> config_keys{"cors", "allowed_origins"};
10-
1110
inline Json::Value NormalizeJson(
1211
const std::unordered_map<std::string, std::string> options) {
1312
Json::Value root;
1413
for (const auto& [key, value] : options) {
15-
if (std::find(config_keys.begin(), config_keys.end(), key) ==
16-
config_keys.end()) {
14+
if (CONFIGURATIONS.find(key) == CONFIGURATIONS.end()) {
1715
continue;
1816
}
17+
auto config = CONFIGURATIONS.at(key);
1918

20-
if (key == "cors") {
19+
if (config.accept_value == "[on|off]") {
2120
if (string_utils::EqualsIgnoreCase("on", value)) {
22-
root["cors"] = true;
21+
root[key] = true;
2322
} else if (string_utils::EqualsIgnoreCase("off", value)) {
24-
root["cors"] = false;
23+
root[key] = false;
2524
}
26-
} else if (key == "allowed_origins") {
25+
} else if (config.accept_value == "comma separated") {
2726
auto origins = string_utils::SplitBy(value, ",");
2827
Json::Value origin_array(Json::arrayValue);
2928
for (const auto& origin : origins) {
3029
origin_array.append(origin);
3130
}
3231
root[key] = origin_array;
32+
} else if (config.accept_value == "string") {
33+
root[key] = value;
34+
} else {
35+
CTL_ERR("Not support configuration type: " << config.accept_value
36+
<< " for config key: " << key);
3337
}
3438
}
3539

@@ -50,13 +54,21 @@ void commands::ConfigUpdCmd::Exec(
5054
}
5155
}
5256

57+
auto non_null_opts = std::unordered_map<std::string, std::string>();
58+
for (const auto& [key, value] : options) {
59+
if (value.empty()) {
60+
continue;
61+
}
62+
non_null_opts[key] = value;
63+
}
64+
5365
auto url = url_parser::Url{
5466
.protocol = "http",
5567
.host = host + ":" + std::to_string(port),
5668
.pathParams = {"v1", "configs"},
5769
};
5870

59-
auto json = NormalizeJson(options);
71+
auto json = NormalizeJson(non_null_opts);
6072
if (json.empty()) {
6173
CLI_LOG_ERROR("Invalid configuration options provided");
6274
return;

engine/common/api_server_configuration.h

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,78 @@ enum class ProxyAuthMethod {
2020
AwsSigV4
2121
};
2222

23+
struct ApiConfigurationMetadata {
24+
std::string name;
25+
std::string desc;
26+
std::string group;
27+
std::string accept_value;
28+
std::string default_value;
29+
30+
bool allow_empty = false;
31+
};
32+
33+
static const std::unordered_map<std::string, ApiConfigurationMetadata>
34+
CONFIGURATIONS = {
35+
{"cors",
36+
ApiConfigurationMetadata{
37+
.name = "cors",
38+
.desc = "Cross-Origin Resource Sharing configuration.",
39+
.group = "CORS",
40+
.accept_value = "[on|off]",
41+
.default_value = "on"}},
42+
{"allowed_origins",
43+
ApiConfigurationMetadata{
44+
.name = "allowed_origins",
45+
.desc = "Allowed origins for CORS. Comma separated. E.g. "
46+
"http://localhost,https://cortex.so",
47+
.group = "CORS",
48+
.accept_value = "comma separated",
49+
.default_value = "*",
50+
.allow_empty = true}},
51+
{"proxy_url", ApiConfigurationMetadata{.name = "proxy_url",
52+
.desc = "Proxy URL",
53+
.group = "Proxy",
54+
.accept_value = "string",
55+
.default_value = ""}},
56+
{"proxy_username", ApiConfigurationMetadata{.name = "proxy_username",
57+
.desc = "Proxy Username",
58+
.group = "Proxy",
59+
.accept_value = "string",
60+
.default_value = ""}},
61+
{"proxy_password", ApiConfigurationMetadata{.name = "proxy_password",
62+
.desc = "Proxy Password",
63+
.group = "Proxy",
64+
.accept_value = "string",
65+
.default_value = ""}},
66+
{"verify_proxy_ssl",
67+
ApiConfigurationMetadata{.name = "verify_proxy_ssl",
68+
.desc = "Verify SSL for proxy",
69+
.group = "Proxy",
70+
.accept_value = "[on|off]",
71+
.default_value = "on"}},
72+
{"verify_proxy_host_ssl",
73+
ApiConfigurationMetadata{.name = "verify_proxy_host_ssl",
74+
.desc = "Verify SSL for proxy",
75+
.group = "Proxy",
76+
.accept_value = "[on|off]",
77+
.default_value = "on"}},
78+
{"no_proxy", ApiConfigurationMetadata{.name = "no_proxy",
79+
.desc = "No proxy for hosts",
80+
.group = "Proxy",
81+
.accept_value = "string",
82+
.default_value = ""}},
83+
{"verify_peer_ssl", ApiConfigurationMetadata{.name = "verify_peer_ssl",
84+
.desc = "Verify peer SSL",
85+
.group = "Proxy",
86+
.accept_value = "[on|off]",
87+
.default_value = "on"}},
88+
{"verify_host_ssl", ApiConfigurationMetadata{.name = "verify_host_ssl",
89+
.desc = "Verify host SSL",
90+
.group = "Proxy",
91+
.accept_value = "[on|off]",
92+
.default_value = "on"}},
93+
};
94+
2395
class ApiServerConfiguration {
2496
public:
2597
ApiServerConfiguration(

0 commit comments

Comments
 (0)