From 4ad5943d3cab0c5fce2865a935b21c389a5a8e16 Mon Sep 17 00:00:00 2001 From: Laurent Erignoux Date: Sat, 1 Feb 2020 16:11:20 +0800 Subject: [PATCH] Add a best option to CLI command clip (#4489) The best option copy the password from the best match if only one matching entry exists. --- src/cli/Clip.cpp | 32 +++++++++++++++++++++++++++++--- src/cli/Clip.h | 1 + 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/cli/Clip.cpp b/src/cli/Clip.cpp index ccb7c0e534..00447574a2 100644 --- a/src/cli/Clip.cpp +++ b/src/cli/Clip.cpp @@ -25,6 +25,7 @@ #include "cli/Utils.h" #include "core/Database.h" #include "core/Entry.h" +#include "core/Global.h" #include "core/Group.h" const QCommandLineOption Clip::AttributeOption = QCommandLineOption( @@ -39,12 +40,18 @@ const QCommandLineOption Clip::TotpOption = << "totp", QObject::tr("Copy the current TOTP to the clipboard (equivalent to \"-a totp\").")); +const QCommandLineOption Clip::BestMatchOption = QCommandLineOption( + QStringList() << "b" + << "best-match", + QObject::tr("try to find the unique entry matching, will fail and display the list of matches otherwise.")); + Clip::Clip() { name = QString("clip"); description = QObject::tr("Copy an entry's attribute to the clipboard."); options.append(Clip::AttributeOption); options.append(Clip::TotpOption); + options.append(Clip::BestMatchOption); positionalArguments.append( {QString("entry"), QObject::tr("Path of the entry to clip.", "clip = copy to clipboard"), QString("")}); optionalArguments.append( @@ -54,12 +61,33 @@ Clip::Clip() int Clip::executeWithDatabase(QSharedPointer database, QSharedPointer parser) { const QStringList args = parser->positionalArguments(); - const QString& entryPath = args.at(1); + QString bestEntryPath; + QString timeout; if (args.size() == 3) { timeout = args.at(2); } TextStream errorTextStream(Utils::STDERR); + TextStream outputTextStream(parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, + QIODevice::WriteOnly); + + if (parser->isSet(Clip::BestMatchOption)) { + QStringList results = database->rootGroup()->locate(args.at(1)); + if (results.count() > 1) { + errorTextStream << QObject::tr("Multiple entries matching:") << endl; + for (const QString& result : asConst(results)) { + errorTextStream << result << endl; + } + return EXIT_FAILURE; + } else { + bestEntryPath = (results.isEmpty()) ? args.at(1) : results[0]; + outputTextStream << QObject::tr("Matching \"%1\" entry used.").arg(bestEntryPath) << endl; + } + } else { + bestEntryPath = args.at(1); + } + + const QString& entryPath = bestEntryPath; int timeoutSeconds = 0; if (!timeout.isEmpty() && timeout.toInt() <= 0) { @@ -69,8 +97,6 @@ int Clip::executeWithDatabase(QSharedPointer database, QSharedPointer< timeoutSeconds = timeout.toInt(); } - TextStream outputTextStream(parser->isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT, - QIODevice::WriteOnly); Entry* entry = database->rootGroup()->findEntryByPath(entryPath); if (!entry) { errorTextStream << QObject::tr("Entry %1 not found.").arg(entryPath) << endl; diff --git a/src/cli/Clip.h b/src/cli/Clip.h index 291e63295a..a8afb69511 100644 --- a/src/cli/Clip.h +++ b/src/cli/Clip.h @@ -29,6 +29,7 @@ class Clip : public DatabaseCommand static const QCommandLineOption AttributeOption; static const QCommandLineOption TotpOption; + static const QCommandLineOption BestMatchOption; }; #endif // KEEPASSXC_CLIP_H