Skip to content

Commit

Permalink
feat: search supports specifying repo names
Browse files Browse the repository at this point in the history
Supports specifying the repository to search through the --repo option.
  • Loading branch information
ice909 committed Mar 3, 2025
1 parent 9cc864b commit b373ad7
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 50 deletions.
4 changes: 4 additions & 0 deletions api/schema/v1.json
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,10 @@
"id": {
"type": "string",
"description": "id of package manager search"
},
"repo": {
"type": "string",
"description": "search packages in specified repo."
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions api/schema/v1.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,9 @@ $defs:
id:
type: string
description: id of package manager search
repo:
type: string
description: search packages in specified repo.
PackageManager1JobInfo:
type: object
description: Get the job result using an ID
Expand Down
3 changes: 3 additions & 0 deletions apps/ll-cli/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ ll-cli search . --type=runtime)"));
->type_name("TYPE")
->capture_default_str()
->check(validatorString);
cliSearch->add_option("--repo", options.searchRepo, _("Search packages in specified repo."))
->type_name("REPO")
->check(validatorString);
cliSearch->add_flag("--dev", options.showDevel, _("include develop application in result"));
cliSearch->add_flag("--all", options.showAll, _("Show all results"));

Expand Down
4 changes: 4 additions & 0 deletions libs/api/src/linglong/api/types/v1/Generators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,11 +849,15 @@ j["RemoteRef"] = x.remoteRef;

inline void from_json(const json & j, PackageManager1SearchParameters& x) {
x.id = j.at("id").get<std::string>();
x.repo = get_stack_optional<std::string>(j, "repo");
}

inline void to_json(json & j, const PackageManager1SearchParameters & x) {
j = json::object();
j["id"] = x.id;
if (x.repo) {
j["repo"] = x.repo;
}
}

inline void from_json(const json & j, PackageManager1SearchResult& x) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ struct PackageManager1SearchParameters {
* id of package manager search
*/
std::string id;
/**
* search packages in specified repo.
*/
std::optional<std::string> repo;
};
}
}
Expand Down
96 changes: 48 additions & 48 deletions libs/linglong/src/linglong/cli/cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
#include <nlohmann/json.hpp>

Check warning on line 37 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <nlohmann/json.hpp> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include <QCryptographicHash>

Check warning on line 39 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QCryptographicHash> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusReply>

Check warning on line 40 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QDBusReply> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QEventLoop>

Check warning on line 41 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QEventLoop> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QFileInfo>

Check warning on line 42 in libs/linglong/src/linglong/cli/cli.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QFileInfo> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <QDBusReply>

#include <charconv>
#include <cstdlib>
Expand Down Expand Up @@ -1389,6 +1389,7 @@ int Cli::search([[maybe_unused]] CLI::App *subcommand)

auto params = api::types::v1::PackageManager1SearchParameters{
.id = options.appid,
.repo = options.searchRepo,
};

auto pendingReply = this->pkgMan.Search(utils::serialize::toQVariantMap(params));
Expand Down Expand Up @@ -1420,58 +1421,57 @@ int Cli::search([[maybe_unused]] CLI::App *subcommand)
}

QEventLoop loop;
QObject::connect(
&this->pkgMan,
&api::dbus::v1::PackageManager::SearchFinished,
[&](const QString &jobID, const QVariantMap &data) {
// Note: once an error occurs, remember to return after exiting the loop.
if (result->id->c_str() != jobID) {
return;
}
auto result =
utils::serialize::fromQVariantMap<api::types::v1::PackageManager1SearchResult>(data);
if (!result) {
this->printer.printErr(result.error());
loop.exit(-1);
return;
}
// Note: should check return code of PackageManager1SearchResult
if (result->code != 0) {
this->printer.printErr(
LINGLONG_ERRV("\n" + QString::fromStdString(result->message), result->code));
loop.exit(result->code);
return;
}

connect(&this->pkgMan,
&api::dbus::v1::PackageManager::SearchFinished,
[&](const QString &jobID, const QVariantMap &data) {
// Note: once an error occurs, remember to return after exiting the loop.
if (result->id->c_str() != jobID) {
return;
}
auto result =
utils::serialize::fromQVariantMap<api::types::v1::PackageManager1SearchResult>(
data);
if (!result) {
this->printer.printErr(result.error());
loop.exit(-1);
return;
}
// Note: should check return code of PackageManager1SearchResult
if (result->code != 0) {
this->printer.printErr(
LINGLONG_ERRV("\n" + QString::fromStdString(result->message), result->code));
loop.exit(result->code);
return;
}

if (!result->packages) {
this->printer.printPackages({});
loop.exit(0);
return;
}
if (!result->packages) {
this->printer.printPackages({});
loop.exit(0);
return;
}

auto pkgs = std::move(result->packages).value();
if (!options.showDevel) {
auto it = std::remove_if(pkgs.begin(),
pkgs.end(),
[](const api::types::v1::PackageInfoV2 &info) {
return info.packageInfoV2Module == "develop";
});
pkgs.erase(it, pkgs.end());
}
auto pkgs = std::move(result->packages).value();
if (!options.showDevel) {
auto it = std::remove_if(pkgs.begin(),
pkgs.end(),
[](const api::types::v1::PackageInfoV2 &info) {
return info.packageInfoV2Module == "develop";
});
pkgs.erase(it, pkgs.end());
}

if (!options.type.empty()) {
filterPackageInfosFromType(pkgs, options.type);
}
if (!options.type.empty()) {
filterPackageInfosFromType(pkgs, options.type);
}

// default only the latest version is displayed
if (!options.showAll) {
filterPackageInfosFromVersion(pkgs);
}
// default only the latest version is displayed
if (!options.showAll) {
filterPackageInfosFromVersion(pkgs);
}

this->printer.printPackages(pkgs);
loop.exit(0);
});
this->printer.printPackages(pkgs);
loop.exit(0);
});
return loop.exec();
}

Expand Down
1 change: 1 addition & 0 deletions libs/linglong/src/linglong/cli/cli.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ struct CliOptions
std::string module;
std::string type;
RepoOptions repoOptions;
std::optional<std::string> searchRepo;
std::vector<std::string> commands;
bool showDevel;
bool showAll;
Expand Down
18 changes: 16 additions & 2 deletions libs/linglong/src/linglong/package_manager/package_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1835,8 +1835,22 @@ auto PackageManager::Search(const QVariantMap &parameters) noexcept -> QVariantM
}
auto jobID = QUuid::createUuid().toString();
auto ref = *fuzzyRef;
m_search_queue.runTask([this, jobID, ref]() {
auto pkgInfos = this->repo.listRemote(ref);
linglong::api::types::v1::Repo repo{};
if (paras->repo.has_value()) {
auto repoConfig = this->repo.getConfig();
auto it = std::find_if(repoConfig.repos.begin(),
repoConfig.repos.end(),
[&paras](const linglong::api::types::v1::Repo &repo) {
return repo.alias.value_or(repo.name) == paras->repo.value();
});

if (it == repoConfig.repos.end()) {
return toDBusReply(-1, QString("repo %1 not found").arg(paras->repo.value().c_str()));
}
repo = *it;
}
m_search_queue.runTask([this, jobID, ref, repo = std::move(repo)]() {
auto pkgInfos = this->repo.listRemote(ref, repo);
if (!pkgInfos.has_value()) {
qWarning() << "list remote failed: " << pkgInfos.error().message();
Q_EMIT this->SearchFinished(jobID, toDBusReply(pkgInfos));
Expand Down

0 comments on commit b373ad7

Please sign in to comment.