Skip to content

Commit

Permalink
fix ci
Browse files Browse the repository at this point in the history
  • Loading branch information
namchuai committed Nov 1, 2024
1 parent d91e90e commit e0f4880
Show file tree
Hide file tree
Showing 28 changed files with 866 additions and 196 deletions.
94 changes: 88 additions & 6 deletions engine/cli/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "commands/engine_install_cmd.h"
#include "commands/engine_list_cmd.h"
#include "commands/engine_uninstall_cmd.h"
#include "commands/engine_update_cmd.h"
#include "commands/engine_use_cmd.h"
#include "commands/model_del_cmd.h"
#include "commands/model_get_cmd.h"
#include "commands/model_import_cmd.h"
Expand Down Expand Up @@ -340,7 +342,7 @@ void CommandLineParser::SetupEngineCommands() {
for (auto& engine : engine_service_.kSupportEngines) {
std::string engine_name{engine};
EngineInstall(install_cmd, engine_name, cml_data_.engine_version,
cml_data_.engine_src);
cml_data_.engine_src, cml_data_.show_menu);
}

auto uninstall_cmd =
Expand All @@ -361,6 +363,41 @@ void CommandLineParser::SetupEngineCommands() {
EngineUninstall(uninstall_cmd, engine_name);
}

auto engine_upd_cmd = engines_cmd->add_subcommand("update", "Update engine");
engine_upd_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
" engines update [engine_name]");
engine_upd_cmd->callback([this, engine_upd_cmd] {
if (std::exchange(executed_, true))
return;
if (engine_upd_cmd->get_subcommands().empty()) {
CLI_LOG("[engine_name] is required\n");
CLI_LOG(engine_upd_cmd->help());
}
});
engine_upd_cmd->group(kSubcommands);
for (auto& engine : engine_service_.kSupportEngines) {
std::string engine_name{engine};
EngineUpdate(engine_upd_cmd, engine_name);
}

auto engine_use_cmd =
engines_cmd->add_subcommand("use", "Set engine as default");
engine_use_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
" engines use [engine_name]");
engine_use_cmd->callback([this, engine_use_cmd] {
if (std::exchange(executed_, true))
return;
if (engine_use_cmd->get_subcommands().empty()) {
CLI_LOG("[engine_name] is required\n");
CLI_LOG(engine_use_cmd->help());
}
});
engine_use_cmd->group(kSubcommands);
for (auto& engine : engine_service_.kSupportEngines) {
std::string engine_name{engine};
EngineUse(engine_use_cmd, engine_name);
}

EngineGet(engines_cmd);
}

Expand Down Expand Up @@ -428,7 +465,8 @@ void CommandLineParser::SetupSystemCommands() {

void CommandLineParser::EngineInstall(CLI::App* parent,
const std::string& engine_name,
std::string& version, std::string& src) {
std::string& version, std::string& src,
bool show_menu) {
auto install_engine_cmd = parent->add_subcommand(engine_name, "");
install_engine_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
" engines install " + engine_name + " [options]");
Expand All @@ -440,13 +478,16 @@ void CommandLineParser::EngineInstall(CLI::App* parent,
install_engine_cmd->add_option("-s, --source", src,
"Install engine by local path");

install_engine_cmd->callback([this, engine_name, &version, &src] {
install_engine_cmd->add_option("-m, --menu", show_menu,
"Display menu for engine variant selection");

install_engine_cmd->callback([this, engine_name, &version, &src, show_menu] {
if (std::exchange(executed_, true))
return;
try {
commands::EngineInstallCmd(download_service_,
cml_data_.config.apiServerHost,
std::stoi(cml_data_.config.apiServerPort))
commands::EngineInstallCmd(
download_service_, cml_data_.config.apiServerHost,
std::stoi(cml_data_.config.apiServerPort), show_menu)
.Exec(engine_name, version, src);
} catch (const std::exception& e) {
CTL_ERR(e.what());
Expand Down Expand Up @@ -474,6 +515,47 @@ void CommandLineParser::EngineUninstall(CLI::App* parent,
});
}

void CommandLineParser::EngineUpdate(CLI::App* parent,
const std::string& engine_name) {
auto engine_update_cmd = parent->add_subcommand(engine_name, "");
engine_update_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
" engines update " + engine_name);
engine_update_cmd->group(kEngineGroup);

engine_update_cmd->callback([this, engine_name] {
if (std::exchange(executed_, true))
return;
try {
commands::EngineUpdateCmd().Exec(
cml_data_.config.apiServerHost,
std::stoi(cml_data_.config.apiServerPort), engine_name);
} catch (const std::exception& e) {
CTL_ERR(e.what());
}
});
}

void CommandLineParser::EngineUse(CLI::App* parent,
const std::string& engine_name) {
auto engine_use_cmd = parent->add_subcommand(engine_name, "");
engine_use_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
" engines use " + engine_name);
engine_use_cmd->group(kEngineGroup);

engine_use_cmd->callback([this, engine_name] {
if (std::exchange(executed_, true))
return;
auto result = commands::EngineUseCmd().Exec(
cml_data_.config.apiServerHost,
std::stoi(cml_data_.config.apiServerPort), engine_name);
if (result.has_error()) {
CTL_ERR(result.error());
} else {
CTL_INF("Engine " << engine_name << " is set as default");
}
});
}

void CommandLineParser::EngineGet(CLI::App* parent) {
auto get_cmd = parent->add_subcommand("get", "Get engine info");
get_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
Expand Down
8 changes: 7 additions & 1 deletion engine/cli/command_line_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,16 @@ class CommandLineParser {
void SetupSystemCommands();

void EngineInstall(CLI::App* parent, const std::string& engine_name,
std::string& version, std::string& src);
std::string& version, std::string& src, bool show_menu);

void EngineUninstall(CLI::App* parent, const std::string& engine_name);

void EngineUpdate(CLI::App* parent, const std::string& engine_name);

void EngineGet(CLI::App* parent);

void EngineUse(CLI::App* parent, const std::string& engine_name);

void ModelUpdate(CLI::App* parent);

CLI::App app_;
Expand All @@ -50,6 +55,7 @@ class CommandLineParser {
bool display_engine = false;
bool display_version = false;
std::string filter = "";
bool show_menu = false;

int port;
config_yaml_utils::CortexConfig config;
Expand Down
53 changes: 30 additions & 23 deletions engine/cli/commands/engine_get_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#include <json/reader.h>
#include <json/value.h>
#include <iostream>

#include "httplib.h"
#include "server_start_cmd.h"
#include "services/engine_service.h"
#include "utils/curl_utils.h"
#include "utils/logging_utils.h"
#include "utils/url_parser.h"

// clang-format off
#include <tabulate/table.hpp>
Expand All @@ -25,30 +26,36 @@ void EngineGetCmd::Exec(const std::string& host, int port,
}

tabulate::Table table;
table.add_row({"Name", "Supported Formats", "Version", "Variant", "Status"});
httplib::Client cli(host + ":" + std::to_string(port));
auto res = cli.Get("/v1/engines/" + engine_name);
if (res) {
if (res->status == httplib::StatusCode::OK_200) {
Json::Value v;
Json::Reader reader;
reader.parse(res->body, v);

table.add_row({v["name"].asString(), v["format"].asString(),
v["version"].asString(), v["variant"].asString(),
v["status"].asString()});

} else {
CLI_LOG_ERROR(
"Failed to get engine list with status code: " << res->status);
return;
}
} else {
auto err = res.error();
CLI_LOG_ERROR("HTTP error: " << httplib::to_string(err));
table.add_row({"#", "Name", "Version", "Variant", "Status"});

auto url = url_parser::Url{
.protocol = "http",
.host = host + ":" + std::to_string(port),
.pathParams = {"v1", "engines", engine_name},
};
auto result = curl_utils::SimpleGetJson(url.ToFullPath());
if (result.has_error()) {
CTL_ERR(result.error());
return;
}

std::vector<EngineVariantResponse> output;
auto installed_variants = result.value();
for (const auto& variant : installed_variants) {
output.push_back(EngineVariantResponse{
.name = variant["name"].asString(),
.version = variant["version"].asString(),
.engine = engine_name,
});
}

int count = 0;
for (auto const& v : output) {
count += 1;
table.add_row(
{std::to_string(count), v.engine, v.version, v.name, "Installed"});
}

std::cout << table << std::endl;
}
}; // namespace commands
Loading

0 comments on commit e0f4880

Please sign in to comment.