-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
300 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
#include "config_get_cmd.h" | ||
#include <tabulate/table.hpp> | ||
#include "commands/server_start_cmd.h" | ||
#include "utils/curl_utils.h" | ||
#include "utils/logging_utils.h" | ||
#include "utils/url_parser.h" | ||
|
||
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"}, | ||
}; | ||
|
||
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
namespace commands { | ||
class ConfigGetCmd { | ||
public: | ||
void Exec(const std::string& host, int port); | ||
}; | ||
} // namespace commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "config_upd_cmd.h" | ||
#include "commands/server_start_cmd.h" | ||
#include "utils/curl_utils.h" | ||
#include "utils/logging_utils.h" | ||
#include "utils/string_utils.h" | ||
#include "utils/url_parser.h" | ||
|
||
namespace { | ||
const std::vector<std::string> config_keys{"cors", "allowed_origins"}; | ||
|
||
inline Json::Value NormalizeJson( | ||
const std::unordered_map<std::string, std::string> options) { | ||
Json::Value root; | ||
for (const auto& [key, value] : options) { | ||
if (std::find(config_keys.begin(), config_keys.end(), key) == | ||
config_keys.end()) { | ||
continue; | ||
} | ||
|
||
if (key == "cors") { | ||
if (string_utils::EqualsIgnoreCase("on", value)) { | ||
root["cors"] = true; | ||
} else if (string_utils::EqualsIgnoreCase("off", value)) { | ||
root["cors"] = false; | ||
} | ||
} else if (key == "allowed_origins") { | ||
auto origins = string_utils::SplitBy(value, ","); | ||
Json::Value origin_array(Json::arrayValue); | ||
for (const auto& origin : origins) { | ||
origin_array.append(origin); | ||
} | ||
root[key] = origin_array; | ||
} | ||
} | ||
|
||
CTL_DBG("Normalized config update request: " << root.toStyledString()); | ||
|
||
return root; | ||
} | ||
}; // namespace | ||
|
||
void commands::ConfigUpdCmd::Exec( | ||
const std::string& host, int port, | ||
const std::unordered_map<std::string, std::string>& options) { | ||
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"}, | ||
}; | ||
|
||
auto json = NormalizeJson(options); | ||
if (json.empty()) { | ||
CLI_LOG_ERROR("Invalid configuration options provided"); | ||
return; | ||
} | ||
|
||
auto update_cnf_result = | ||
curl_utils::SimplePatch(url.ToFullPath(), json.toStyledString()); | ||
if (update_cnf_result.has_error()) { | ||
CLI_LOG_ERROR(update_cnf_result.error()); | ||
return; | ||
} | ||
|
||
CLI_LOG("Configuration updated successfully!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
namespace commands { | ||
class ConfigUpdCmd { | ||
public: | ||
void Exec(const std::string& host, int port, | ||
const std::unordered_map<std::string, std::string>& options); | ||
}; | ||
} // namespace commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.