Skip to content

Commit

Permalink
fix: support for linux
Browse files Browse the repository at this point in the history
  • Loading branch information
vansangpfiev committed Sep 5, 2024
1 parent 16af33c commit 30185b9
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
31 changes: 25 additions & 6 deletions engine/commands/cortex_upd_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
namespace commands {

namespace {
const std::string kCortexBinary = "cortex-cpp";
}
const std::string kCortexBinary = "cortex-cpp";
}

CortexUpdCmd::CortexUpdCmd() {}

Expand Down Expand Up @@ -129,18 +129,37 @@ void CortexUpdCmd::Exec() {
std::string temp = ".\\cortex_tmp.exe";
remove(temp.c_str()); // ignore return code

std::string src = ".\\Cortex\\" + kCortexBinary + "\\" + kCortexBinary + ".exe";
std::string src =
".\\cortex\\" + kCortexBinary + "\\" + kCortexBinary + ".exe";
std::string dst = ".\\" + kCortexBinary + ".exe";
// Rename
rename(dst.c_str(), temp.c_str());
// Update
CopyFile(const_cast<char*>(src.c_str()), const_cast<char*>(dst.c_str()),
false);
remove(".\\cortex");
remove(temp.c_str());
#else
std::string src = "./Cortex/" + kCortexBinary + "/" + kCortexBinary;
std::string temp = "./cortex_tmp";
std::string src = "./cortex/" + kCortexBinary + "/" + kCortexBinary;
std::string dst = "./" + kCortexBinary;
std::filesystem::copy_file(src, dst,
std::filesystem::copy_options::overwrite_existing);
if (std::rename(dst.c_str(), temp.c_str())) {
CTL_ERR("Failed to rename from " << dst << " to " << temp);
return;
}
try {
std::filesystem::copy_file(
src, dst, std::filesystem::copy_options::overwrite_existing);
std::filesystem::permissions(dst, std::filesystem::perms::owner_all |
std::filesystem::perms::group_all |
std::filesystem::perms::others_read |
std::filesystem::perms::others_exec);
std::filesystem::remove(temp);
std::filesystem::remove_all("./cortex/");
} catch (const std::exception& e) {
CTL_WRN("Something wrong happened: " << e.what());
return;
}
#endif
CLI_LOG("Update cortex sucessfully");
}
Expand Down
52 changes: 28 additions & 24 deletions engine/controllers/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,47 +167,51 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
app_.add_flag_function("-v", cb, "Cortex version");

std::string cortex_version;
bool check_update = true;
{
auto update_cmd = app_.add_subcommand("update", "Update cortex version");

update_cmd->add_option("-v", cortex_version, "");
update_cmd->callback([&cortex_version] {
update_cmd->callback([&cortex_version, &check_update] {
commands::CortexUpdCmd cuc;
cuc.Exec();
check_update = false;
});
}

CLI11_PARSE(app_, argc, argv);

// Check new update, only check for stable release for now
#ifdef CORTEX_CPP_VERSION
constexpr auto github_host = "https://api.github.com";
std::ostringstream release_path;
release_path << "/repos/janhq/cortex.cpp/releases/latest";
CTL_INF("Engine release path: " << github_host << release_path.str());

httplib::Client cli(github_host);
if (auto res = cli.Get(release_path.str())) {
if (res->status == httplib::StatusCode::OK_200) {
try {
auto json_res = nlohmann::json::parse(res->body);
std::string latest_version = json_res["tag_name"].get<std::string>();
std::string current_version = CORTEX_CPP_VERSION;
if (current_version != latest_version) {
CLI_LOG("\nA new release of cortex is available: "
<< current_version << " -> " << latest_version);
CLI_LOG("To upgrade, run: cortex update");
CLI_LOG(json_res["html_url"].get<std::string>());
if (check_update) {
constexpr auto github_host = "https://api.github.com";
std::ostringstream release_path;
release_path << "/repos/janhq/cortex.cpp/releases/latest";
CTL_INF("Engine release path: " << github_host << release_path.str());

httplib::Client cli(github_host);
if (auto res = cli.Get(release_path.str())) {
if (res->status == httplib::StatusCode::OK_200) {
try {
auto json_res = nlohmann::json::parse(res->body);
std::string latest_version = json_res["tag_name"].get<std::string>();
std::string current_version = CORTEX_CPP_VERSION;
if (current_version != latest_version) {
CLI_LOG("\nA new release of cortex is available: "
<< current_version << " -> " << latest_version);
CLI_LOG("To upgrade, run: cortex update");
CLI_LOG(json_res["html_url"].get<std::string>());
}
} catch (const nlohmann::json::parse_error& e) {
CTL_INF("JSON parse error: " << e.what());
}
} catch (const nlohmann::json::parse_error& e) {
CTL_INF("JSON parse error: " << e.what());
} else {
CTL_INF("HTTP error: " << res->status);
}
} else {
CTL_INF("HTTP error: " << res->status);
auto err = res.error();
CTL_INF("HTTP error: " << httplib::to_string(err));
}
} else {
auto err = res.error();
CTL_INF("HTTP error: " << httplib::to_string(err));
}
#endif

Expand Down
3 changes: 2 additions & 1 deletion engine/utils/logging_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ inline bool log_verbose = false;
LOG_INFO << msg; \
} else { \
std::cout << msg << std::endl; \
}
}

0 comments on commit 30185b9

Please sign in to comment.