Skip to content

Commit 90c622a

Browse files
authored
chore: Remove supported_engines check in CLI (#2129)
* remove checks against supportedEngines * remove supportedEngines check for more commands
1 parent 9ee8b2b commit 90c622a

File tree

2 files changed

+83
-224
lines changed

2 files changed

+83
-224
lines changed

engine/cli/command_line_parser.cc

Lines changed: 82 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ CommandLineParser::CommandLineParser()
5151
dylib_path_manager_{std::make_shared<cortex::DylibPathManager>()},
5252
db_service_{std::make_shared<DatabaseService>()},
5353
engine_service_{std::make_shared<EngineService>(
54-
download_service_, dylib_path_manager_, db_service_)} {
55-
supported_engines_ = engine_service_->GetSupportedEngineNames().value();
56-
}
54+
download_service_, dylib_path_manager_, db_service_)} {}
5755

5856
bool CommandLineParser::SetupCommand(int argc, char** argv) {
5957
app_.usage("Usage:\n" + commands::GetCortexBinary() +
@@ -482,104 +480,141 @@ void CommandLineParser::SetupEngineCommands() {
482480
install_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
483481
" engines install [engine_name] [options]");
484482
install_cmd->group(kSubcommands);
483+
install_cmd
484+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
485+
->required();
486+
install_cmd->add_option("-v, --version", cml_data_.engine_version,
487+
"Engine version to download");
488+
install_cmd->add_option("-s, --source", cml_data_.engine_src,
489+
"Install engine by local path");
490+
install_cmd->add_flag("-m, --menu", cml_data_.show_menu,
491+
"Display menu for engine variant selection");
492+
485493
install_cmd->callback([this, install_cmd] {
486494
if (std::exchange(executed_, true))
487495
return;
488-
if (install_cmd->get_subcommands().empty()) {
489-
CLI_LOG("[engine_name] is required\n");
490-
CLI_LOG(install_cmd->help());
496+
try {
497+
commands::EngineInstallCmd(
498+
engine_service_, cml_data_.config.apiServerHost,
499+
std::stoi(cml_data_.config.apiServerPort), cml_data_.show_menu)
500+
.Exec(cml_data_.engine_name, cml_data_.engine_version,
501+
cml_data_.engine_src);
502+
} catch (const std::exception& e) {
503+
CTL_ERR(e.what());
491504
}
492505
});
493506

494-
for (const auto& engine : supported_engines_) {
495-
EngineInstall(install_cmd, engine, cml_data_.engine_version,
496-
cml_data_.engine_src);
497-
}
498-
499507
auto uninstall_cmd =
500508
engines_cmd->add_subcommand("uninstall", "Uninstall engine");
501509
uninstall_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
502510
" engines uninstall [engine_name] [options]");
511+
uninstall_cmd->group(kSubcommands);
512+
uninstall_cmd
513+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
514+
->required();
503515
uninstall_cmd->callback([this, uninstall_cmd] {
504516
if (std::exchange(executed_, true))
505517
return;
506-
if (uninstall_cmd->get_subcommands().empty()) {
507-
CLI_LOG("[engine_name] is required\n");
508-
CLI_LOG(uninstall_cmd->help());
518+
try {
519+
commands::EngineUninstallCmd().Exec(
520+
cml_data_.config.apiServerHost,
521+
std::stoi(cml_data_.config.apiServerPort), cml_data_.engine_name);
522+
} catch (const std::exception& e) {
523+
CTL_ERR(e.what());
509524
}
510525
});
511-
uninstall_cmd->group(kSubcommands);
512-
for (const auto& engine : supported_engines_) {
513-
EngineUninstall(uninstall_cmd, engine);
514-
}
515526

516527
auto engine_upd_cmd = engines_cmd->add_subcommand("update", "Update engine");
517528
engine_upd_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
518529
" engines update [engine_name]");
530+
engine_upd_cmd->group(kSubcommands);
531+
engine_upd_cmd
532+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
533+
->required();
519534
engine_upd_cmd->callback([this, engine_upd_cmd] {
520535
if (std::exchange(executed_, true))
521536
return;
522-
if (engine_upd_cmd->get_subcommands().empty()) {
523-
CLI_LOG("[engine_name] is required\n");
524-
CLI_LOG(engine_upd_cmd->help());
537+
try {
538+
commands::EngineUpdateCmd().Exec(
539+
cml_data_.config.apiServerHost,
540+
std::stoi(cml_data_.config.apiServerPort), cml_data_.engine_name);
541+
} catch (const std::exception& e) {
542+
CTL_ERR(e.what());
525543
}
526544
});
527-
engine_upd_cmd->group(kSubcommands);
528-
for (const auto& engine : supported_engines_) {
529-
EngineUpdate(engine_upd_cmd, engine);
530-
}
531545

532546
auto engine_use_cmd =
533547
engines_cmd->add_subcommand("use", "Set engine as default");
534548
engine_use_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
535549
" engines use [engine_name]");
550+
engine_use_cmd->group(kSubcommands);
551+
engine_use_cmd
552+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
553+
->required();
536554
engine_use_cmd->callback([this, engine_use_cmd] {
537555
if (std::exchange(executed_, true))
538556
return;
539-
if (engine_use_cmd->get_subcommands().empty()) {
540-
CLI_LOG("[engine_name] is required\n");
541-
CLI_LOG(engine_use_cmd->help());
557+
auto result = commands::EngineUseCmd().Exec(
558+
cml_data_.config.apiServerHost,
559+
std::stoi(cml_data_.config.apiServerPort), cml_data_.engine_name);
560+
if (result.has_error()) {
561+
CTL_ERR(result.error());
562+
} else {
563+
CTL_INF("Engine " << cml_data_.engine_name << " is set as default");
542564
}
543565
});
544-
engine_use_cmd->group(kSubcommands);
545-
for (const auto& engine : supported_engines_) {
546-
EngineUse(engine_use_cmd, engine);
547-
}
548566

549567
auto engine_load_cmd = engines_cmd->add_subcommand("load", "Load engine");
550568
engine_load_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
551569
" engines load [engine_name]");
570+
engine_load_cmd->group(kSubcommands);
571+
engine_load_cmd
572+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
573+
->required();
552574
engine_load_cmd->callback([this, engine_load_cmd] {
553575
if (std::exchange(executed_, true))
554576
return;
555-
if (engine_load_cmd->get_subcommands().empty()) {
556-
CLI_LOG("[engine_name] is required\n");
557-
CLI_LOG(engine_load_cmd->help());
577+
auto result = commands::EngineLoadCmd().Exec(
578+
cml_data_.config.apiServerHost,
579+
std::stoi(cml_data_.config.apiServerPort), cml_data_.engine_name);
580+
if (result.has_error()) {
581+
CTL_ERR(result.error());
558582
}
559583
});
560-
engine_load_cmd->group(kSubcommands);
561-
for (const auto& engine : supported_engines_) {
562-
EngineLoad(engine_load_cmd, engine);
563-
}
564584

565585
auto engine_unload_cmd =
566586
engines_cmd->add_subcommand("unload", "Unload engine");
567587
engine_unload_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
568588
" engines unload [engine_name]");
589+
engine_unload_cmd->group(kSubcommands);
590+
engine_unload_cmd
591+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
592+
->required();
569593
engine_unload_cmd->callback([this, engine_unload_cmd] {
570594
if (std::exchange(executed_, true))
571595
return;
572-
if (engine_unload_cmd->get_subcommands().empty()) {
573-
CLI_LOG("[engine_name] is required\n");
574-
CLI_LOG(engine_unload_cmd->help());
596+
auto result = commands::EngineUnloadCmd().Exec(
597+
cml_data_.config.apiServerHost,
598+
std::stoi(cml_data_.config.apiServerPort), cml_data_.engine_name);
599+
if (result.has_error()) {
600+
CTL_ERR(result.error());
575601
}
576602
});
577-
engine_unload_cmd->group(kSubcommands);
578-
for (const auto& engine : supported_engines_) {
579-
EngineUnload(engine_unload_cmd, engine);
580-
}
581603

582-
EngineGet(engines_cmd);
604+
auto engine_get_cmd = engines_cmd->add_subcommand("get", "Get engine info");
605+
engine_get_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
606+
" engines get [engine_name] [options]");
607+
engine_get_cmd->group(kSubcommands);
608+
engine_get_cmd
609+
->add_option("name", cml_data_.engine_name, "Engine name e.g. llama-cpp")
610+
->required();
611+
engine_get_cmd->callback([this, engine_get_cmd] {
612+
if (std::exchange(executed_, true))
613+
return;
614+
commands::EngineGetCmd().Exec(cml_data_.config.apiServerHost,
615+
std::stoi(cml_data_.config.apiServerPort),
616+
cml_data_.engine_name);
617+
});
583618
}
584619

585620
void CommandLineParser::SetupHardwareCommands() {
@@ -737,167 +772,6 @@ void CommandLineParser::SetupSystemCommands() {
737772
});
738773
}
739774

740-
void CommandLineParser::EngineInstall(CLI::App* parent,
741-
const std::string& engine_name,
742-
std::string& version, std::string& src) {
743-
auto install_engine_cmd = parent->add_subcommand(engine_name, "");
744-
install_engine_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
745-
" engines install " + engine_name + " [options]");
746-
install_engine_cmd->group(kEngineGroup);
747-
748-
install_engine_cmd->add_option("-v, --version", version,
749-
"Engine version to download");
750-
751-
install_engine_cmd->add_option("-s, --source", src,
752-
"Install engine by local path");
753-
754-
install_engine_cmd->add_flag("-m, --menu", cml_data_.show_menu,
755-
"Display menu for engine variant selection");
756-
757-
install_engine_cmd->callback([this, engine_name, &version, &src] {
758-
if (std::exchange(executed_, true))
759-
return;
760-
try {
761-
commands::EngineInstallCmd(
762-
engine_service_, cml_data_.config.apiServerHost,
763-
std::stoi(cml_data_.config.apiServerPort), cml_data_.show_menu)
764-
.Exec(engine_name, version, src);
765-
} catch (const std::exception& e) {
766-
CTL_ERR(e.what());
767-
}
768-
});
769-
}
770-
771-
void CommandLineParser::EngineUninstall(CLI::App* parent,
772-
const std::string& engine_name) {
773-
auto uninstall_engine_cmd = parent->add_subcommand(engine_name, "");
774-
uninstall_engine_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
775-
" engines install " + engine_name + " [options]");
776-
uninstall_engine_cmd->group(kEngineGroup);
777-
778-
uninstall_engine_cmd->callback([this, engine_name] {
779-
if (std::exchange(executed_, true))
780-
return;
781-
try {
782-
commands::EngineUninstallCmd().Exec(
783-
cml_data_.config.apiServerHost,
784-
std::stoi(cml_data_.config.apiServerPort), engine_name);
785-
} catch (const std::exception& e) {
786-
CTL_ERR(e.what());
787-
}
788-
});
789-
}
790-
791-
void CommandLineParser::EngineUpdate(CLI::App* parent,
792-
const std::string& engine_name) {
793-
auto engine_update_cmd = parent->add_subcommand(engine_name, "");
794-
engine_update_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
795-
" engines update " + engine_name);
796-
engine_update_cmd->group(kEngineGroup);
797-
798-
engine_update_cmd->callback([this, engine_name] {
799-
if (std::exchange(executed_, true))
800-
return;
801-
try {
802-
commands::EngineUpdateCmd().Exec(
803-
cml_data_.config.apiServerHost,
804-
std::stoi(cml_data_.config.apiServerPort), engine_name);
805-
} catch (const std::exception& e) {
806-
CTL_ERR(e.what());
807-
}
808-
});
809-
}
810-
811-
void CommandLineParser::EngineUnload(CLI::App* parent,
812-
const std::string& engine_name) {
813-
auto sub_cmd = parent->add_subcommand(engine_name, "");
814-
sub_cmd->usage("Usage:\n" + commands::GetCortexBinary() + " engines unload " +
815-
engine_name);
816-
sub_cmd->group(kEngineGroup);
817-
818-
sub_cmd->callback([this, engine_name] {
819-
if (std::exchange(executed_, true))
820-
return;
821-
auto result = commands::EngineUnloadCmd().Exec(
822-
cml_data_.config.apiServerHost,
823-
std::stoi(cml_data_.config.apiServerPort), engine_name);
824-
if (result.has_error()) {
825-
CTL_ERR(result.error());
826-
}
827-
});
828-
}
829-
830-
void CommandLineParser::EngineLoad(CLI::App* parent,
831-
const std::string& engine_name) {
832-
auto sub_cmd = parent->add_subcommand(engine_name, "");
833-
sub_cmd->usage("Usage:\n" + commands::GetCortexBinary() + " engines load " +
834-
engine_name);
835-
sub_cmd->group(kEngineGroup);
836-
837-
sub_cmd->callback([this, engine_name] {
838-
if (std::exchange(executed_, true))
839-
return;
840-
auto result = commands::EngineLoadCmd().Exec(
841-
cml_data_.config.apiServerHost,
842-
std::stoi(cml_data_.config.apiServerPort), engine_name);
843-
if (result.has_error()) {
844-
CTL_ERR(result.error());
845-
}
846-
});
847-
}
848-
849-
void CommandLineParser::EngineUse(CLI::App* parent,
850-
const std::string& engine_name) {
851-
auto engine_use_cmd = parent->add_subcommand(engine_name, "");
852-
engine_use_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
853-
" engines use " + engine_name);
854-
engine_use_cmd->group(kEngineGroup);
855-
856-
engine_use_cmd->callback([this, engine_name] {
857-
if (std::exchange(executed_, true))
858-
return;
859-
auto result = commands::EngineUseCmd().Exec(
860-
cml_data_.config.apiServerHost,
861-
std::stoi(cml_data_.config.apiServerPort), engine_name);
862-
if (result.has_error()) {
863-
CTL_ERR(result.error());
864-
} else {
865-
CTL_INF("Engine " << engine_name << " is set as default");
866-
}
867-
});
868-
}
869-
870-
void CommandLineParser::EngineGet(CLI::App* parent) {
871-
auto get_cmd = parent->add_subcommand("get", "Get engine info");
872-
get_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
873-
" engines get [engine_name] [options]");
874-
get_cmd->group(kSubcommands);
875-
get_cmd->callback([this, get_cmd] {
876-
if (std::exchange(executed_, true))
877-
return;
878-
if (get_cmd->get_subcommands().empty()) {
879-
CLI_LOG("[engine_name] is required\n");
880-
CLI_LOG(get_cmd->help());
881-
}
882-
});
883-
884-
for (const auto& engine : supported_engines_) {
885-
std::string desc = "Get " + engine + " status";
886-
887-
auto engine_get_cmd = get_cmd->add_subcommand(engine, desc);
888-
engine_get_cmd->usage("Usage:\n" + commands::GetCortexBinary() +
889-
" engines get " + engine + " [options]");
890-
engine_get_cmd->group(kEngineGroup);
891-
engine_get_cmd->callback([this, engine] {
892-
if (std::exchange(executed_, true))
893-
return;
894-
commands::EngineGetCmd().Exec(cml_data_.config.apiServerHost,
895-
std::stoi(cml_data_.config.apiServerPort),
896-
engine);
897-
});
898-
}
899-
}
900-
901775
void CommandLineParser::ModelUpdate(CLI::App* parent) {
902776
auto model_update_cmd =
903777
parent->add_subcommand("update", "Update model configurations");

0 commit comments

Comments
 (0)