This repository was archived by the owner on Jul 4, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 181
feat: cortex update command #1095
Merged
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e950285
feat: cortex update command
vansangpfiev de3cfdd
fix: clean
vansangpfiev 16af33c
feat: check version each time run cli
vansangpfiev 30185b9
fix: support for linux
vansangpfiev c9ffefe
fix: add todo
vansangpfiev 00bc507
fix: comments
vansangpfiev b5cbae4
fix: macos
vansangpfiev cb71b4d
Merge branch 'dev' of github.com:janhq/nitro into feat/cortex-update-cmd
vansangpfiev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,167 @@ | ||
| // clang-format off | ||
| #include "utils/cortex_utils.h" | ||
| // clang-format on | ||
| #include "cortex_upd_cmd.h" | ||
| #include "httplib.h" | ||
| #include "nlohmann/json.hpp" | ||
| #include "services/download_service.h" | ||
| #include "utils/archive_utils.h" | ||
| #include "utils/logging_utils.h" | ||
| #include "utils/system_info_utils.h" | ||
| #if defined(_WIN32) || defined(__linux__) | ||
| #include "utils/file_manager_utils.h" | ||
| #endif | ||
|
|
||
| namespace commands { | ||
|
|
||
| namespace { | ||
| const std::string kCortexBinary = "cortex-cpp"; | ||
| } | ||
|
|
||
| CortexUpdCmd::CortexUpdCmd() {} | ||
|
|
||
| void CortexUpdCmd::Exec(std::string v) { | ||
| // Check if the architecture and OS are supported | ||
| auto system_info = system_info_utils::GetSystemInfo(); | ||
| if (system_info.arch == system_info_utils::kUnsupported || | ||
| system_info.os == system_info_utils::kUnsupported) { | ||
| CTL_ERR("Unsupported OS or architecture: " << system_info.os << ", " | ||
| << system_info.arch); | ||
| return; | ||
| } | ||
| CTL_INF("OS: " << system_info.os << ", Arch: " << system_info.arch); | ||
|
|
||
| // Download file | ||
| constexpr auto github_host = "https://api.github.com"; | ||
| // std::string version = v.empty() ? "latest" : std::move(v); | ||
| // TODO(sang): support download with version | ||
| std::string version = "latest"; | ||
| std::ostringstream release_path; | ||
| release_path << "/repos/janhq/cortex.cpp/releases/" << version; | ||
| 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 jsonResponse = nlohmann::json::parse(res->body); | ||
| auto assets = jsonResponse["assets"]; | ||
| auto os_arch{system_info.os + "-" + system_info.arch}; | ||
|
|
||
| std::string matched_variant = ""; | ||
| for (auto& asset : assets) { | ||
| auto asset_name = asset["name"].get<std::string>(); | ||
| if (asset_name.find("cortex-cpp") != std::string::npos && | ||
| asset_name.find(os_arch) != std::string::npos) { | ||
| matched_variant = asset_name; | ||
| break; | ||
| } | ||
| CTL_INF(asset_name); | ||
| } | ||
| if (matched_variant.empty()) { | ||
| CTL_ERR("No variant found for " << os_arch); | ||
| return; | ||
| } | ||
| CTL_INF("Matched variant: " << matched_variant); | ||
|
|
||
| for (auto& asset : assets) { | ||
| auto asset_name = asset["name"].get<std::string>(); | ||
| if (asset_name == matched_variant) { | ||
| std::string host{"https://github.com"}; | ||
|
|
||
| auto full_url = asset["browser_download_url"].get<std::string>(); | ||
| std::string path = full_url.substr(host.length()); | ||
|
|
||
| auto fileName = asset["name"].get<std::string>(); | ||
| CTL_INF("URL: " << full_url); | ||
|
|
||
| auto download_task = DownloadTask{.id = "cortex", | ||
| .type = DownloadType::Cortex, | ||
| .error = std::nullopt, | ||
| .items = {DownloadItem{ | ||
| .id = "cortex", | ||
| .host = host, | ||
| .fileName = fileName, | ||
| .type = DownloadType::Cortex, | ||
| .path = path, | ||
| }}}; | ||
|
|
||
| DownloadService download_service; | ||
| download_service.AddDownloadTask( | ||
| download_task, | ||
| [this](const std::string& absolute_path, bool unused) { | ||
| // try to unzip the downloaded file | ||
| std::filesystem::path download_path{absolute_path}; | ||
| CTL_INF("Downloaded engine path: " << download_path.string()); | ||
|
|
||
| std::filesystem::path extract_path = | ||
| download_path.parent_path().parent_path(); | ||
|
|
||
| archive_utils::ExtractArchive(download_path.string(), | ||
| extract_path.string()); | ||
|
|
||
| // remove the downloaded file | ||
| // TODO(any) Could not delete file on Windows because it is currently hold by httplib(?) | ||
| // Not sure about other platforms | ||
| try { | ||
| std::filesystem::remove(absolute_path); | ||
| } catch (const std::exception& e) { | ||
| CTL_WRN("Could not delete file: " << e.what()); | ||
| } | ||
| CTL_INF("Finished!"); | ||
| }); | ||
| break; | ||
| } | ||
| } | ||
| } catch (const nlohmann::json::parse_error& e) { | ||
| std::cerr << "JSON parse error: " << e.what() << std::endl; | ||
| return; | ||
| } | ||
| } else { | ||
| CTL_ERR("HTTP error: " << res->status); | ||
| return; | ||
| } | ||
| } else { | ||
| auto err = res.error(); | ||
| CTL_ERR("HTTP error: " << httplib::to_string(err)); | ||
| return; | ||
| } | ||
| #if defined(_WIN32) | ||
| std::string temp = ".\\cortex_tmp.exe"; | ||
| remove(temp.c_str()); // ignore return code | ||
|
|
||
| std::string src = | ||
| ".\\cortex\\" + kCortexBinary + "\\" + kCortexBinary + ".exe"; | ||
| std::string dst = ".\\" + kCortexBinary + ".exe"; | ||
vansangpfiev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // 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 temp = "./cortex_tmp"; | ||
| std::string src = "./cortex/" + kCortexBinary + "/" + kCortexBinary; | ||
| std::string dst = "./" + kCortexBinary; | ||
| 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"); | ||
| } | ||
| } // namespace commands | ||
This file contains hidden or 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,15 @@ | ||
| #pragma once | ||
| #include <string> | ||
| #include <optional> | ||
|
|
||
| namespace commands { | ||
|
|
||
| class CortexUpdCmd{ | ||
| public: | ||
| CortexUpdCmd(); | ||
| void Exec(std::string version); | ||
|
|
||
| private: | ||
vansangpfiev marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }; | ||
|
|
||
| } // namespace commands | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -29,4 +29,5 @@ inline bool log_verbose = false; | |
| LOG_INFO << msg; \ | ||
| } else { \ | ||
| std::cout << msg << std::endl; \ | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.