Skip to content

Commit

Permalink
Adding a best option to clip to copy a password of the best match if …
Browse files Browse the repository at this point in the history
…only one matching entry exists
  • Loading branch information
lerignoux committed Mar 25, 2020
1 parent 71a39c3 commit 97cf956
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/cli/Clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,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(
Expand All @@ -54,12 +60,35 @@ Clip::Clip()
int Clip::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> 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) {
Expand All @@ -69,8 +98,6 @@ int Clip::executeWithDatabase(QSharedPointer<Database> 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;
Expand Down
1 change: 1 addition & 0 deletions src/cli/Clip.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Clip : public DatabaseCommand

static const QCommandLineOption AttributeOption;
static const QCommandLineOption TotpOption;
static const QCommandLineOption BestMatchOption;
};

#endif // KEEPASSXC_CLIP_H

0 comments on commit 97cf956

Please sign in to comment.