CLI: Replace locate command with search

* Introduce search CLI command to replace locate command. Search can provide the same functionality but in a more fine-grained fashion

* Replace use of Group::locate in code: Use EntrySearcher in clip cli command best-match option. This removes the matching against group hierarchy of an entry which is kind of nonsense as clip expects exactly one match. Matching against groups can be done using search command.

* Remove obsolete Group::locate method
This commit is contained in:
Robin Ebert
2021-08-03 15:37:47 +02:00
committed by Jonathan White
parent ec81d2bc3f
commit e8f2c9d126
12 changed files with 103 additions and 165 deletions

View File

@@ -32,12 +32,12 @@ set(cli_SOURCES
Import.cpp
Info.cpp
List.cpp
Locate.cpp
Merge.cpp
Move.cpp
Open.cpp
Remove.cpp
RemoveGroup.cpp
Search.cpp
Show.cpp)
add_library(cli STATIC ${cli_SOURCES})

View File

@@ -18,6 +18,7 @@
#include "Clip.h"
#include "Utils.h"
#include "core/EntrySearcher.h"
#include "core/Group.h"
#include "core/Tools.h"
@@ -78,16 +79,18 @@ int Clip::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<
QString entryPath;
if (parser->isSet(Clip::BestMatchOption)) {
QStringList results = database->rootGroup()->locate(args.at(1));
EntrySearcher searcher;
const auto& searchTerm = args.at(1);
const auto results = searcher.search(QString("title:%1").arg(searchTerm), database->rootGroup(), true);
if (results.count() > 1) {
err << QObject::tr("Multiple entries matching:") << endl;
for (const QString& result : asConst(results)) {
err << result << endl;
for (const Entry* result : results) {
err << result->path().prepend('/') << endl;
}
return EXIT_FAILURE;
} else {
entryPath = (results.isEmpty()) ? args.at(1) : results[0];
out << QObject::tr("Used matching entry: %1").arg(entryPath) << endl;
entryPath = (results.isEmpty()) ? searchTerm : results[0]->path().prepend('/');
out << QObject::tr("Using matching entry: %1").arg(entryPath) << endl;
}
} else {
entryPath = args.at(1);

View File

@@ -31,12 +31,12 @@
#include "Import.h"
#include "Info.h"
#include "List.h"
#include "Locate.h"
#include "Merge.h"
#include "Move.h"
#include "Open.h"
#include "Remove.h"
#include "RemoveGroup.h"
#include "Search.h"
#include "Show.h"
#include "Utils.h"
@@ -173,7 +173,6 @@ namespace Commands
s_commands.insert(QStringLiteral("estimate"), QSharedPointer<Command>(new Estimate()));
s_commands.insert(QStringLiteral("generate"), QSharedPointer<Command>(new Generate()));
s_commands.insert(QStringLiteral("help"), QSharedPointer<Command>(new Help()));
s_commands.insert(QStringLiteral("locate"), QSharedPointer<Command>(new Locate()));
s_commands.insert(QStringLiteral("ls"), QSharedPointer<Command>(new List()));
s_commands.insert(QStringLiteral("merge"), QSharedPointer<Command>(new Merge()));
s_commands.insert(QStringLiteral("mkdir"), QSharedPointer<Command>(new AddGroup()));
@@ -181,6 +180,7 @@ namespace Commands
s_commands.insert(QStringLiteral("open"), QSharedPointer<Command>(new Open()));
s_commands.insert(QStringLiteral("rm"), QSharedPointer<Command>(new Remove()));
s_commands.insert(QStringLiteral("rmdir"), QSharedPointer<Command>(new RemoveGroup()));
s_commands.insert(QStringLiteral("search"), QSharedPointer<Command>(new Search()));
s_commands.insert(QStringLiteral("show"), QSharedPointer<Command>(new Show()));
if (interactive) {

View File

@@ -15,36 +15,37 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Locate.h"
#include "Search.h"
#include <QCommandLineParser>
#include "Utils.h"
#include "core/EntrySearcher.h"
#include "core/Group.h"
Locate::Locate()
Search::Search()
{
name = QString("locate");
name = QString("search");
description = QObject::tr("Find entries quickly.");
positionalArguments.append({QString("term"), QObject::tr("Search term."), QString("")});
}
int Locate::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
int Search::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
{
auto& out = Utils::STDOUT;
auto& err = Utils::STDERR;
const QStringList args = parser->positionalArguments();
const QString& searchTerm = args.at(1);
QStringList results = database->rootGroup()->locate(searchTerm);
EntrySearcher searcher;
auto results = searcher.search(args.at(1), database->rootGroup(), true);
if (results.isEmpty()) {
err << "No results for that search term." << endl;
return EXIT_FAILURE;
}
for (const QString& result : asConst(results)) {
out << result << endl;
for (const Entry* result : asConst(results)) {
out << result->path().prepend('/') << endl;
}
return EXIT_SUCCESS;
}

View File

@@ -15,17 +15,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef KEEPASSXC_LOCATE_H
#define KEEPASSXC_LOCATE_H
#ifndef KEEPASSXC_SEARCH_H
#define KEEPASSXC_SEARCH_H
#include "DatabaseCommand.h"
class Locate : public DatabaseCommand
class Search : public DatabaseCommand
{
public:
Locate();
Search();
int executeWithDatabase(QSharedPointer<Database> db, QSharedPointer<QCommandLineParser> parser) override;
};
#endif // KEEPASSXC_LOCATE_H
#endif // KEEPASSXC_SEARCH_H