diff options
author | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2021-03-18 13:07:39 +0200 |
---|---|---|
committer | Arttu Tarkiainen <arttu.tarkiainen@qt.io> | 2021-04-09 09:19:48 +0300 |
commit | 6c70f89e8e8e312a4a738c180742c0ab41b3ab95 (patch) | |
tree | 4093246a789b25125b074aa6b682a3a60e8c497e /src/libs/installer | |
parent | 8f97bf0ba510a374d4ce3ab040a513c62779454f (diff) |
CLI: Add support for additional filtering of search results
Search command results can now be optionally filtered with
the '--filter-packages'-option, which takes an unspecified
count of key-value pairs that contain the desired package
information element and a regular expression used to find
matches in the element.
Example usage:
'installer --fp "Version=1.0, Description=Some text" search *'
Only the packages which match all given filter rules are
included in the output print.
Task-number: QTIFW-2168
Change-Id: I788b065d95952b988489c36db80b3c859b970f05
Reviewed-by: Katja Marttila <katja.marttila@qt.io>
Diffstat (limited to 'src/libs/installer')
-rw-r--r-- | src/libs/installer/commandlineparser.cpp | 9 | ||||
-rw-r--r-- | src/libs/installer/constants.h | 4 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.cpp | 21 | ||||
-rw-r--r-- | src/libs/installer/packagemanagercore.h | 4 |
4 files changed, 32 insertions, 6 deletions
diff --git a/src/libs/installer/commandlineparser.cpp b/src/libs/installer/commandlineparser.cpp index 59d42eefc..4bfc4ad3a 100644 --- a/src/libs/installer/commandlineparser.cpp +++ b/src/libs/installer/commandlineparser.cpp @@ -59,6 +59,9 @@ CommandLineParser::CommandLineParser() .arg(CommandLineOptions::scListShort, CommandLineOptions::scListLong) + indent + QString::fromLatin1("%1, %2 - search available packages - <regexp>\n") .arg(CommandLineOptions::scSearchShort, CommandLineOptions::scSearchLong) + + indent + indent + QString::fromLatin1("Note: The --%1 option can be used to specify\n") + .arg(CommandLineOptions::scFilterPackagesLong) + + indent + indent + QLatin1String("additional filters for the search operation\n") + indent + QString::fromLatin1("%1, %2 - create offline installer from selected packages - <pkg ...>\n") .arg(CommandLineOptions::scCreateOfflineShort, CommandLineOptions::scCreateOfflineLong) + indent + QString::fromLatin1("%1, %2 - uninstall all packages and remove entire program directory") @@ -161,6 +164,12 @@ CommandLineParser::CommandLineParser() << CommandLineOptions::scCreateLocalRepositoryShort << CommandLineOptions::scCreateLocalRepositoryLong, QLatin1String("Create a local repository inside the installation directory. This option " "has no effect on online installers."))); + addOptionWithContext(QCommandLineOption(QStringList() + << CommandLineOptions::scFilterPackagesShort << CommandLineOptions::scFilterPackagesLong, + QLatin1String("[CLI] Comma separated list of additional key-value pair filters used to query packages with the " + "search command. The keys can be any of the possible package information elements, like " + "\"DisplayName\" and \"Description\"."), + QLatin1String("element=regex,...")), CommandLineOnly); // Message query options addOptionWithContext(QCommandLineOption(QStringList() << CommandLineOptions::scAcceptMessageQueryShort diff --git a/src/libs/installer/constants.h b/src/libs/installer/constants.h index f1105941b..0e16d4c4b 100644 --- a/src/libs/installer/constants.h +++ b/src/libs/installer/constants.h @@ -1,6 +1,6 @@ /************************************************************************** ** -** Copyright (C) 2020 The Qt Company Ltd. +** Copyright (C) 2021 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the Qt Installer Framework. @@ -203,6 +203,8 @@ static const QLatin1String scCreateLocalRepositoryShort("cl"); static const QLatin1String scCreateLocalRepositoryLong("create-local-repository"); static const QLatin1String scNoDefaultInstallationShort("nd"); static const QLatin1String scNoDefaultInstallationLong("no-default-installations"); +static const QLatin1String scFilterPackagesShort("fp"); +static const QLatin1String scFilterPackagesLong("filter-packages"); // Developer options static const QLatin1String scScriptShort("s"); diff --git a/src/libs/installer/packagemanagercore.cpp b/src/libs/installer/packagemanagercore.cpp index c512c23b5..26df043a0 100644 --- a/src/libs/installer/packagemanagercore.cpp +++ b/src/libs/installer/packagemanagercore.cpp @@ -2179,11 +2179,13 @@ ComponentModel *PackageManagerCore::updaterComponentModel() const /*! Lists available packages filtered with \a regexp without GUI. Virtual - components are not listed unless set visible. + components are not listed unless set visible. Optionally, a \a filters + hash containing package information elements and regular expressions + can be used to further filter listed packages. \sa setVirtualComponentsVisible() */ -void PackageManagerCore::listAvailablePackages(const QString ®exp) +void PackageManagerCore::listAvailablePackages(const QString ®exp, const QHash<QString, QString> &filters) { setPackageViewer(); qCDebug(QInstaller::lcInstallerInstallLog) @@ -2209,8 +2211,19 @@ void PackageManagerCore::listAvailablePackages(const QString ®exp) continue; const QModelIndex &idx = model->indexFromComponentName(component->treeName()); - if (idx.isValid() && re.match(name).hasMatch()) - matchedPackages.append(package); + if (idx.isValid() && re.match(name).hasMatch()) { + bool ignoreComponent = false; + for (auto &key : filters.keys()) { + const QString elementValue = component->value(key); + QRegularExpression elementRegexp(filters.value(key)); + if (elementValue.isEmpty() || !elementRegexp.match(elementValue).hasMatch()) { + ignoreComponent = true; + break; + } + } + if (!ignoreComponent) + matchedPackages.append(package); + } } if (matchedPackages.count() == 0) qCDebug(QInstaller::lcInstallerInstallLog) << "No matching packages found."; diff --git a/src/libs/installer/packagemanagercore.h b/src/libs/installer/packagemanagercore.h index 750c03b15..2c54122f5 100644 --- a/src/libs/installer/packagemanagercore.h +++ b/src/libs/installer/packagemanagercore.h @@ -33,6 +33,7 @@ #include "repository.h" #include "qinstallerglobal.h" #include "utils.h" +#include "commandlineparser.h" #include <QtCore/QHash> #include <QtCore/QObject> @@ -242,7 +243,8 @@ public: ComponentModel *defaultComponentModel() const; ComponentModel *updaterComponentModel() const; void listInstalledPackages(const QString ®exp = QString()); - void listAvailablePackages(const QString ®exp); + void listAvailablePackages(const QString ®exp = QString(), + const QHash<QString, QString> &filters = QHash<QString, QString>()); PackageManagerCore::Status updateComponentsSilently(const QStringList &componentsToUpdate); PackageManagerCore::Status installSelectedComponentsSilently(const QStringList& components); PackageManagerCore::Status installDefaultComponentsSilently(); |